Hi Paul,
thanks for trying 8.0. I have just uploaded yet another snapshot that optimizes descendant-or-self axes and union & list expressions; e.g.:
//(sea, river) -> (/descendant::sea | /descendant::river )
There are various other query optimizations that will be available in 8.0. But as you already observed, this is probably not the bottleneck in your query.
The offending line ( let $to := //*[@id=$toId][1] ) takes about 20 msecs per hit
It seems as if the index structures are not utilized here (you can open the InfoView in the GUI in order to see what's going on). You will probably get much better performance by using parentheses around the path expression:
(//*[@id=$toId])[1]
Please note that the two expressions are not equivalent: The second one will only give you 1 result whereas the first one may give you more than one result, because it's equivalent to:
/descendant-or-self::node()/child::*[@id=$toId][1]
The reason is that the two predicates belongs to the child step and not the full path expression.
let $start := (current-dateTime() - xs:dateTime('1970-01-01T00:00:00-00:00')) div xs:dayTimeDuration('PT0.001S')
Due to the functional nature of XQuery, all calls of current-dateTime() will give you the same result during the execution of a query. But there is (at least) one way out: You can try prof:current-ns() instead [1].
By the way, here is one more variant of your query, which explicitly accesses the index structures (however, this version of BaseX-specific and not that nice to read anymore):
for $source in collection("Facts")/ (descendant::sea | descendant::river | descendant::lake) return element water { element {$source/local-name()} {data($source/@name)}, for $to in (db:text('Facts', $source/to/@water)/ (parent::sea | parent::river | parent::lake))[1] return element streamsTo { attribute {$to/local-name()} {data($to/@name)} } }
Hope this helps; feel free to ask for more details, Christian