Changes

Jump to navigation Jump to search
204 bytes removed ,  18:32, 1 December 2023
m
Text replacement - "<syntaxhighlight lang="xquery">" to "<pre lang='xquery'>"
The name index is e.g. applied to discard location steps that will never yield results:
<syntaxhighlight pre lang="'xquery"'>
(: will be rewritten to an empty sequence :)
/non-existing-name
* Descendant steps will be rewritten to multiple child steps. Child steps are evaluated faster, as fewer nodes have to be traversed:
<syntaxhighlight pre lang="'xquery"'>
doc('factbook.xml')//province,
(: ...will be rewritten to... :)
* The {{Code|fn:count}} function will be pre-evaluated by looking up the number in the index:
<syntaxhighlight pre lang="'xquery"'>
count(doc('factbook')//country)
</syntaxhighlight>
* The distinct values of elements or attributes can be looked up in the index as well:
<syntaxhighlight pre lang="'xquery"'>
distinct-values(db:get('factbook')//religions)
</syntaxhighlight>
With XQuery, index structures can be created and dropped via {{Function|Database|db:optimize}}:
<syntaxhighlight pre lang="'xquery"'>
(: Optimize specified database, create full-text index for texts of the specified elements :)
db:optimize(
This index references text nodes of documents. It will be utilized to accelerate string comparisons in path expressions. The following queries will all be rewritten for index access:
<syntaxhighlight pre lang="'xquery"'>
(: example 1 :)
//*[text() = 'Germany'],
The {{Option|UPDINDEX}} option can be enabled to keep this index up-to-date:
<syntaxhighlight pre lang="'xquery"'>
db:optimize(
'mydb',
The text index also supports range queries based on string comparisons:
<syntaxhighlight pre lang="'xquery"'>
(: example 1 :)
db:get('Library')//Medium[Year >= '2011' and Year <= '2016'],
Similar to the text index, this index speeds up string and range comparisons on attribute values. Additionally, the XQuery function {{Code|fn:id}} takes advantage of the index whenever possible. The following queries will all be rewritten for index access:
<syntaxhighlight pre lang="'xquery"'>
(: 1st example :)
//country[@car_code = 'J'],
In many XML dialects, such as HTML or DITA, multiple tokens are stored in attribute values. The token index can be created to speed up the retrieval of these tokens. The XQuery functions {{Code|fn:contains-token}}, {{Code|fn:tokenize}} and {{Code|fn:idref}} are rewritten for index access whenever possible. If a token index exists, it will, e.g., be utilized for the following queries:
<syntaxhighlight pre lang="'xquery"'>
(: 1st example :)
//div[contains-token(@class, 'row')],
If the full-text index exists, the following queries will all be rewritten for index access:
<syntaxhighlight pre lang="'xquery"'>
(: 1st example :)
//country[name/text() contains text 'and'],
; XQuery
<syntaxhighlight pre lang="'xquery"'>
db:create('factbook', 'http://files.basex.org/xml/factbook.xml', '',
map { 'attrinclude': 'id,name' })
* In the query below, 10 databases will be addressed. If it is known in advance that these databases contain an up-to-date text index, the index rewriting can be enforced as follows:
<syntaxhighlight pre lang="'xquery"'>
(# db:enforceindex #) {
for $n in 1 to 10
* The following query contains two predicates that may both be rewritten for index access. If the automatically chosen rewriting is known not to be optimal, another index rewriting can enforced by surrounding the specific expression with the pragma:
<syntaxhighlight pre lang="'xquery"'>
db:get('factbook')//country
[(# db:enforceindex #) {
The option can also be assigned to predicates with dynamic values. In the following example, the comparison of the first comparison will be rewritten for index access. Without the pragma expression, the second comparison is preferred and chosen for the rewriting because the statically known string allows for an exact cost estimation:
<syntaxhighlight pre lang="'xquery"'>
for $name in ('Germany', 'Italy')
for $country in db:get('factbook')//country
With XQuery, it is comparatively easy to create your own, custom index structures. The following query demonstrates how you can create a {{Code|factbook-index}} database, which contains all texts of the original database in lower case:
<syntaxhighlight pre lang="'xquery"'>
let $db := 'factbook'
In the following query, a text string is searched, and the text nodes of the original database are retrieved:
<syntaxhighlight pre lang="'xquery"'>
let $db := 'factbook'
let $text := 'italian'
Bureaucrats, editor, reviewer, Administrators
13,554

edits

Navigation menu