Changes

Jump to navigation Jump to search
1,943 bytes added ,  12:29, 13 July 2020
Created page with "This article is part of the XQuery Portal. It lists some optimizations that are performed by BaseX to speed up queries and reduce memory consumption. =Introduction..."
This article is part of the [[XQuery|XQuery Portal]]. It lists some optimizations that are performed by BaseX to speed up queries and reduce memory consumption.

=Introduction=

An XQuery expression is evaluated in multiple steps:

# At parse time, the query string – an XQuery main module – is transformed to a tree representation, the ''abstract syntax tree'' (AST).
# At compile time, the syntax tree is decorated with additional information (type information, expression properties); expressions are relocated, simplified, or pre-evaluated.
# At evaluation time, the resulting expression tree is processed.
# The results are returned to the user. Some expression (such as simple loops) can be evaluated in iterative manner, whereas others (such as sort operations) need to be fully evaluated before the first result is available.

Each of the steps allows for numerous optimizations, some of which are described in this article.

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.

==Pre-Evaluation==

Parts of the query that are static and would be executed multiple times can already be evaluated at compile time:

<syntaxhighlight lang="xquery">
for $i in 1 to 10
return 2 * 3

(: will be rewritten to :)
for $i in 1 to 10
return 6
</syntaxhighlight>

==Variable Inlining==

The value of a variable can be ''inlined'': The variables references are replaced by the expression that is bound to the variable. The resulting expression can often be simplified:

<syntaxhighlight lang="xquery">
declare variable $INFO := true();

let $nodes := //nodes
where $INFO
return 'Results: ' || count($nodes)

(: will be rewritten to :)
let $nodes := //nodes
where true()
return 'Results: ' || count($nodes)

(: will be rewritten to :)
let $nodes := //nodes
return 'Results: ' || count($nodes)
</syntaxhighlight>
Bureaucrats, editor, reviewer, Administrators
13,554

edits

Navigation menu