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 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. This provides full-featured XQuery functionality; however, compensation 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. For this example, the most efficient solution is to use the function rtrim(), which is available for all supported databases and XML documents DataDirect XQuery supports:

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.