Database Reference
In-Depth Information
Figure 11-8. String comparison within XQuery
As with the value() method, atomization of nodes rule applies to the exist() method. It is also better to move
the node path to the outside of the predicate part, referencing it with the current node ' . ' symbol when dealing with
untyped XML. This helps to avoid type casting, which introduces an additional UDX operator to the execution plan.
Note
UDX operators implement XQuery/Xpath operations in SQL Server.
The code shown in Listing 11-13 executes three queries. The first query references the element within the
predicate, and it performs atomization of node, which leads to an additional call to the table-valued XML Reader
function. The second query does not perform atomization of nodes, although it performs comparison casting of the
values to xs:int . This adds the UDX operator to the execution plan. The last query compares values as strings, which
is the most efficient method. Again, keep in mind that string comparison uses unicode, case-sensitive comparison
rules. Figure 11-9 shows the execution plans for all three queries.
Listing 11-13. Atomization of nodes and type casting
declare
@X xml = '<Order OrderNum="Order1"><OrderId>1</OrderId></Order>'
select 'Atomization of nodes'
where @X.exist('/Order[OrderId=1]') = 1
select 'No text() function'
where @X.exist('/Order/OrderId[.=1]') = 1
select 'With text() function'
where @X.exist('/Order/OrderId/text()[.=1]') = 1
 
Search WWH ::




Custom Search