/*
Each time an XQuery expression is used in SQL Server, SQL Server creates a nodes tree for each query and has to read it, which has overhead implications. XML indexes can help improve performance by creating a pre-defined node tree. Indexes contain details of:
- nodes
- values
- paths
*/
-- A Primary XML index provides a persisted node tree in an internal format that is used to speed access to elements and attributes within the XML. It requires a clusted primary key on the table in the first place.
CREATE PRIMARY XML INDEX XML_Order_Items
ON Sales.Order (Items);
/*
A Secondary XML index is one that supports XQuery expressions. They can only be created after a Primary XML index has been created. You can create three types of secondary indexes to help resolve specific XQuery expressions rapidly. You can have multiple secondary XML indexes, including having one for Property, one for Value, and one for Path:
- PROPERTY - optimized for retreiving multiple values in query method calls
- VALUE - optimized for retrieving single values in value method calls
- PATH - typeically used by the exist method
*/
CREATE XML INDEX XML_Order_Items_Path
ON Sales.Order (Items)
USING XML INDEX XML_Order_Items FOR PATH;
GO
CREATE XML INDEX XML_Order_Items_Value
ON Sales.Order (Items)
USING XML INDEX XML_Order_Items FOR VALUE;
GO
CREATE XML INDEX XML_Order_Items_Property
ON Sales.Order (Items)
USING XML INDEX XML_Order_Items FOR PROPERTY;
GO