Hi Teena (cc @basex-talk),
first of all, thanks for giving us details on your data and queries. To complete this: do you have a complete version of your query that shows us what are the values of $context, $project and $scopeName? As an example, I’m wondering what are the ancestor nodes of the <scopes> elements.
declare function foo:get-node-v1($context as element()?, $project as element()?, $scopeName as xs:string) as element()? { if (exists($project) = true()) then ( $project/scopes/scope[@name=$scopeName]/item ) else () };
The following query is equivalent, by the way (this is the beauty of XPath):
declare function foo:get-node-v1($context as element()?, $project as element()?, $scopeName as xs:string) as element()? { { $project/scopes/scope[@name=$scopeName]/item };
If all your data is stored in the same database, it may be faster to open the database within the function. One more optimization could be to directly access the text index:
declare function foo:get-node-v1($context as element()?, $project as element()?, $scopeName as xs:string) as element()? { { db:attribute("name-of-your-db", $scopeName, "name")/parent::scope };
You could also check out the latest stable snapshot of BaseX 7.8 [1], which inlines functions and may thus do this automatically.
When we call the first function in another function and use it for further computation, it is always slower by 40 seconds (when compared to second variation of the function). What could be causing this?
Difficult to say; we probably need to see your complete function call.
Are we still referencing to baseX document when we do following instead of getting a local copy? let $items := foo:get-node-v1($context, $project, $scopeName)
Yes, the returned nodes still reference nodes in the database.
Hope this helps, Christian