Changes

Jump to navigation Jump to search
1,801 bytes added ,  09:20, 8 April 2021
This article is part of the [[XQuery|XQuery Portal]]. Optimizations are presented that speed up the execution time and reduce memory consumption.
The article text will be further regularly extended with each version of BaseXfurther examples.
=Introduction=
Subsequent rewritings might result in query plans that differ a lot from the original query. As this might complicate debugging, you can disable function inling during development by setting {{Option|INLINELIMIT}} to {{Code|0}}.
 
==Loop Unrolling==
 
{{Mark|Introduced with Version 9.6:}}
 
Loops with few iterations are ''unrolled'' by the XQuery compiler to enable further optimizations:
 
<syntaxhighlight lang="xquery">
(1 to 2) ! (. * 2)
 
(: rewritten to :)
1 ! (. * 2), 2 ! (. * 2)
 
(: further rewritten to :)
1 * 2, 2 * 2
 
(: further rewritten to :)
2, 4
</syntaxhighlight>
 
Folds are unrolled, too:
 
<syntaxhighlight lang="xquery">
let $f := function($a, $b) { $a * $b }
return fold-left(2 to 5, 1, $f)
 
(: rewritten to :)
let $f := function($a, $b) { $a * $b }
return $f($f($f($f(1, 2), 3), 4), 5)
</syntaxhighlight>
 
The standard unroll limit is <code>5</code>. It can be adjusted with the {{Option|UNROLLLIMIT}} option, e.g. via a pragma:
 
<syntaxhighlight lang="xquery">
(# db:unrolllimit 10 #) {
for $i in 1 to 10
return db:open('db' || $i)//*[text() = 'abc']
}
 
(: rewritten to :)
db:open('db1')//*[text() = 'abc'],
db:open('db2')//*[text() = 'abc'],
...
db:open('db10')//*[text() = 'abc'],
</syntaxhighlight>
 
The last example indicates that index rewritings might be triggered by unrolling loops with paths on database nodes.
 
The following expressions can be unrolled:
 
* Simple map expressions
* Simple FLWOR expressions
* Filter expressions
* [[Higher-Order Functions#fn:fold-left|fn:fold-left]], [[Higher-Order Functions#fn:fold-right|fn:fold-right]], {{Function|Higher-Order Functions|fn:fold-left1}}
 
Care should be taken if a higher value is selected, as memory consumption and compile time will increase.
==Paths==
(: equivalent queries, with identical syntax trees :)
doc('addressbook.xml')//city,
doc('addressbook.xml')/descendant-or-self::node()/child::city
(: rewritten to :)
<syntaxhighlight lang="xquery">
declare variable $name external := 'city';
db:open('addressbook')//descendant::*[name() = $name]
(: rewritten to :)
db:open('addressbook')//descendant::city
</syntaxhighlight>
* {{Code|where}} clauses are rewritten to predicates.
* {{Code|if}} expressions in the return clause are rewritten to {{Code|where}} clauses.
 Since {{Version|9.4}}, the * The last {{Code|for}} clause is merged into the {{Code|return}} clause and rewritten to a [[XQuery_3.0|Simple_Map_Operator|simple map]] expression.
Various of these rewriting are demonstrated in the following example:
| Distributivity
|- valign="top"
| <code>$a and or not($a)</code>
| <code>true()</code>
| Tertium non datur
for $p in $auction/site/people/person
let $a :=
for $t in $auction/site/closedauctionsclosed_auctions/closedauctionclosed_auction
where $t/buyer/@person = $p/@id
return $t
return <item person='"{ $p/name/text() }'">{ count($a) }</item>,
(: rewritten to :)
}</item>
</syntaxhighlight>
 
If the accessed database is not known at compile time, or if you want to give a predicate preference to another one, you can [[Indexes#Enforce Rewritings|enforce index rewritings]].
=Evaluation-Time Optimizations=
==Comparisons==
In many cases, the amount of data to be processed is only known after the query has been compiled. Moreover, the data that is looped through expressions may change. In those cases, the best optimizations needs to be chosen at runtime.
If sequences of items are compared against each other, a dynamic hash index will be generated, and the total number of comparisons can be significantly reduced. In the following example, <code>count($input1) * count($input2)</code> comparisons would need to be made without the intermediate index structure:
=Changelog=
 
;Version 9.6
* Added: {{Option|UNROLLLIMIT}}
Introduced with Version 9.4.
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu