I have a question regarding XML indexes.
We have a table OVERLAY_ITEM_SEARCH_XXX with almost 5 million rows. The table has 2 columns – an identity column (bigint) and a SEARCH_VALUES column (XML). The XML column has a primary XML index and secondary XML PATH index.
The SEARCH_VALUES XML column contains a number of fields under the ATTRIBUTES node, e.g. SECTION_REF, but these are variable, as they are derived from user configuration.
The elements c121 and o870 represent client_id = 121 and overlay_id = 870. There are several clients and overlays within the database.
So a typical SEARCH_VALUES XML value for a particular row may look like this:
<c101>
<o777>
<SECTION_REF>1/2/567</SECTION_REF>
<NAME>FRED</NAME>
<POSTCODE>W1 21A</POSTCODE>
</o777>
</c101>
Performing the following 2 queries, for which no rows are found
1. A TOP 1 query
selectTOP 1 * from overlay.OVERLAY_ITEM_SEARCH_XXX s
where s.SEARCH_VALUES.exist('c121/o870/ATTRIBUTES/SECTION_REF/text() [contains(.,"J1")]') = 1
2. A SELECT * query
select*from overlay.OVERLAY_ITEM_SEARCH_XXX s
where s.SEARCH_VALUES.exist('c121/o870/ATTRIBUTES/SECTION_REF/text() [contains(.,"J1")]') = 1
The ‘SELECT *’ query utilises the secondary XML index and runs in 9s, whereas the ‘TOP 1’ query doesn’t use the secondary XML index an takes 100s.
Why does the TOP n query not utilise the secondary XML index? N.B. This is from the actual query plan.
It seems that any query we try other than "select *" fails to use the index, e.g. IF EXISTS(SELECT *...)
Does anyone have any similar feedback from using XML indexes on large tables?
Note that the search for the searchable value (e.g. in the example above ‘J1’), needs to be a ‘contains’ type search.
This is using SQL Server 2012 SP2
Any help/comments would be appreciated.