Changes

Jump to navigation Jump to search
2,187 bytes added ,  12:58, 13 July 2020
no edit summary
This article is part of the [[XQuery|XQuery Portal]]. It lists some optimizations Optimizations are presented that are performed by BaseX to speed up queries the execution time and reduce memory consumption. The article will be incrementally enhanced with more examples.
=Introduction=
If you run a query on [[Command-Line_Options#Standalone|command-line]], you can use {{Code|-V}} to output detailed query information. In the [[GUI]], you can enable the Info View panel.
 
=Compile-Time Optimizations=
==Pre-Evaluation==
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}}.
 
==Static Typing==
 
If the type of a value is known at compile time, type checks can be removed. In the example below, the static information that {{Code|$i}} will always reference items of type {{Code|xs:integer}} can be utilized to simplify the expression:
 
<syntaxhighlight lang="xquery">
for $i in 1 to 5
return typeswitch($i)
case xs:numeric return 'number'
default return 'string'
 
(: will be rewritten to :)
for $i in 1 to 5
return 'number'
</syntaxhighlight>
 
==FLWOR Rewritings==
 
FLWOR expressions are central to XQuery and the most complex constructs the language offers. Numerous optimizations have been realized to improve the execution time:
 
* Nested FLWOR expressions are flattened.
* {{Code|for}} clauses with single items are rewritten to {{Code|let}} clauses.
* {{Code|let}} clauses that are iterated multiple times are lifted up.
* Expressions of {{Code|let}} clauses are inlined.
* Unused variables are removed.
* {{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 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:
 
<syntaxhighlight lang="xquery">
for $a in 1 to 5
for $b in 2
where $a > 3
let $c := $a + $b
return $c</syntaxhighlight>
 
(: for is rewritten to let :)
for $a in 1 to 5
let $b := 2
where $a > 3
let $c := $a + $b
return $c</syntaxhighlight>
 
(: let is lifted up :)
let $b := 2
for $a in 1 to 5
where $a > 3
let $c := $a + $b
return $c</syntaxhighlight>
 
(: where is rewritten to predicate :)
let $b := 2
for $a in 1 to 5[. > 3]
let $c := $a + $b
return $c</syntaxhighlight>
 
(: $b is inlined :)
for $a in 1 to 5[. > 3]
let $c := $a + 2
return $c</syntaxhighlight>
 
(: $c is inlined :)
for $a in 1 to 5[. > 3]
return $a + 2
 
(: for clause is merged into return clause and rewritten to a simple map :)
(1 to 5)[. > 3] ! (. + 2)
</syntaxhighlight>
 
=Changelog=
 
Introduced with Version 9.4.
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu