Hi James,
regarding your specific query, I’m afraid it’s probably too nested in order to simply say how it’s gonna be evaluated, so I would suggest to simply run the query, see what happens and check out the optimization steps int the query info. In general, I can confirm that the evaluation of a filter expression with a positional predicate will be stopped as soon as all results in the specified range have been returned. The following query demonstrates this behavior (it would run quite a while if the iteration were not interrupted):
(1 to 100000000000000000)[position() = 1 to 10]
Next, you may get better results if you move expressions out of a predicate if they may need to be evaluated multiple times:
let $key := $entityMetadata/property[@key = 'true']/@name return ($context//* [name() = $type] [data((*|@*)[name() = $key]) = $entityId] )[1]
In the given case, it might be unnecessary, as you want to stop evaluation after the first hit anyway.
I agree that each XQuery implementation may find different strategies to evaluate this query – and I’d like to add that it‘s often not possible to say what a specific implementation does, as optimization strategies change over time, and may also be dependent on the input data, available index structures, etc. For example, if multiple predicates are specified, we are trying to find and evaluate the cheapest predicate earlier than others, and this estimation also depends on the database statistics.
In a nutshell: you will often succeed in speeding up your queries by rewriting them. On the other hand, it’s sometimes impossible to say in advance if a query will be evaluated faster than another.
Hope this helps, Christian ___________________________
On Tue, May 21, 2013 at 9:42 PM, James Wright james.jw@hotmail.com wrote:
Hey,
In the BaseX implementation this query:
($context//*[name() = $type and data((*|@*)[name() = $entityMetadata/property[@key = 'true']/@name]) = $entityId])[1]
Will the query execute completely and then a 'subset' of 1 will be extracted, or will it only execute until it find the '1st' item that matches the query? Maybe this is a general XQuery question but I figured it was implementation specific.
- James
BaseX-Talk mailing list BaseX-Talk@mailman.uni-konstanz.de https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk