XQuery Optimizations

From BaseX Documentation
Revision as of 11:29, 13 July 2020 by CG (talk | contribs) (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...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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

An XQuery expression is evaluated in multiple steps:

  1. At parse time, the query string – an XQuery main module – is transformed to a tree representation, the abstract syntax tree (AST).
  2. At compile time, the syntax tree is decorated with additional information (type information, expression properties); expressions are relocated, simplified, or pre-evaluated.
  3. At evaluation time, the resulting expression tree is processed.
  4. 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, you can use -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>