I have recently been researching Selective Xml Indexing and have been trying to figure out how to index a .query
method
If I have the below query
WITH XMLNAMESPACES (Default 'http://www.w3.org/2001/XMLSchema' )
SELECT
,a.value('@Number' , 'INT') As Number
,c.query('.') as FormattedXml
From
dbo.XmlTable As X
Cross Apply x.XmlColumn.nodes('Root') aa(a)
Cross Apply a.nodes('Block') bb(b)
I can write a selective Index as below if I am following it right:
CREATE SELECTIVE XML INDEX xsi_XmlTable
ON dbo.XmlTable( XmlColumn )
WITH XMLNAMESPACES ( Default 'http://www.w3.org/2001/XMLSchema' )
FOR
(
root = '/Root' AS XQUERY 'node()'
, rootNumber = '/Root/@Number' AS SQL INT SINGLETON
)
GO
Can I include the c.query in the index? Thanks
Best Answer
A selective index will not be used when using
query
to retrieve the data.From Selective XML Indexes (SXI) - Supported XML Features
However testing shows that the index is used when you rewrite the query using nodes. These queries returns the same but the second is faster because it uses the the selective XML index.
The selective XML index used in the second query is defined as..
I used a table of my own just to be able to show what the difference in this case is.