Using Comparisons
When DataDirect XQuery encounters comparisons in where clauses or in predicate expressions and an operand is bound to data in an XML data source, performance can be significantly improved if this operand is known by DataDirect XQuery to be a single item.
Consider the following query:
for $request in doc('file:///c:/in/request.xml')/request let $ticker := $request/performance/ticker, $start := $request/performance/start, $end := $request/performance/end for $h in collection('historical')/historical where $h/ticker = $ticker return $hDataDirect XQuery does not know how many ticker, start, or end elements may occur in the XML source, so it restricts its rewrites in case there are more than one of each of these elements.
Using value comparisons – for example, eq, as shown in the following code – instead of general comparisons makes this query run faster:
for $request in doc('file:///c:/in/request.xml')/request let $ticker := $request/performance/ticker, $start := $request/performance/start, $end := $request/performance/end for $h in collection('historical')/historical where $h/ticker eq $ticker return $hHowever, this does not work for all data types because eq is restrictive in the types it accepts and does less implicit casting.
Generally, using value comparisons (eq, ne, lt, le, gt, ge) instead of general comparisons (=, !=, <, <=, >, >=) improves performance. When using general comparison against sequences, the result of the expression is true if any combination of the items contained in the sequences satisfies the comparison. Value comparison only applies to operands that are single items, and the expression returns true if the single items compared with the value comparison operator (for example, eq) match. If one of the two operands is not a single item, the use of eq raises an error. In the preceding example, the query behavior perceived by the user does not differ when using = or eq because $h/ticker and $ticker are always single items. But, typically, using eq instead of = significantly improves performance.