Understanding Compensation


XQuery contains expressions, functions, and operators that cannot be directly translated into SQL. For example, fn:tokenize( ) has no SQL equivalent. When an XQuery expression cannot be translated to SQL, DataDirect XQuery "compensates" the expression; that is, it executes the expression in the DataDirect XQuery Engine using data retrieved from the database. Compensation provides full-featured XQuery functionality, but it is is often slower than executing an expression in the database.

Sometimes, the same result can be obtained by using an expression that does not require compensation. For example, suppose you need to perform string comparisons with data that contains trailing spaces. You could use the XQuery function normalize-space(), which removes leading and trailing spaces:

for $h in collection('stocks.dbo.historical')/historical
where normalize-space($h/ticker) = 'AMZN'
return $h 

However, the normalize-space() function is compensated, which means that the where clause is evaluated in the DataDirect XQuery engine rather than in the database, which slows performance. As shown in the following example, the most efficient solution is to use the function rtrim(), which is available for XML documents and all supported databases:

for $h in collection('historical')/historical
where ddtek:rtrim($h/ticker) = 'AMZN'
return $h 

See XQuery Support for details about which expressions, functions, and operators are compensated.