SQL Server 2008 allows for the creation of indexes
only on the XML data type. It’s used to optimize XQuery queries, which
index all tags, values, and paths over the XML instances in the XML data
type column. SQL Server provides two key types of indexing on the XML
data type CREATE PRIMARY XML INDEX and CREATE XML INDEX.
Warning
The first index on the XML type column must be the Primary XML index.
The CREATE PRIMARY XML
INDEX removes the need for SQL Server to shred your XML data during
every query. It should be used when you store large XML documents in
each row of a table. You cannot create a primary XML index on a non-XML column, and you can only create one primary XML index on a given XML column. Figure 1 is the syntax for creating a CREATE PRIMARY XML INDEX.
Figure 1. CREATE PRIMARY XML INDEX Syntax
CREATE PRIMARY XML INDEX [index_name] ON table_name (xml_column_name)
|
XML instances are
stored in XML-type columns as large binary objects (BLOBS). These XML
instances can be large, and the stored binary representation of XML data
type instances can be up to 2GB.
|
In Figure 2, we have created a table and inserted some values into the table.
Figure 2. Sample XML Data
CREATE TABLE [dbo].[XML_Table]( [pk] [int] IDENTITY(1,1) NOT NULL, [customerName] [varchar](255) NULL, [customerPhone] [varchar](255) NULL, [customerAddress] [xml] NULL, CONSTRAINT [PK_XML_Table] PRIMARY KEY CLUSTERED ( [pk] ASC )) INSERT INTO XML_Table ( [customerName], [customerPhone], [customerAddress] ) VALUES ( /* customerName - VARCHAR(255) */ 'Monica Banning', /* customerPhone - VARCHAR(255) */ '555-8746', '<customer><address1>123 Main Street</address1><city>Newark</city> <state>DE</state><zip>14785</zip> </customer>' )
INSERT INTO XML_Table ( [customerName], [customerPhone], [customerAddress] ) VALUES ( /* customerName - VARCHAR(255) */ 'Jennifer Liddle', /* customerPhone - VARCHAR(255) */ '555-2497', '<customer><address1>45 Andrew Street</address1><city>Clifton </city><state>AH</state><zip>18783</zip> </customer>')
|
To create the primary key for this table, we will use the code shown in Figure 3.
Figure 3. Create Primary XML Index
CREATE PRIMARY XML INDEX [PK_XML_Data_customerAddress] ON XML_Table (customerAddress)
|
Secondary XML indexes are also created on an XML data type column. There are three types of secondary XML indexes. See Table 1 for more details.
Table 1. Secondary XML Index Types
Secondary Index | Description |
---|
PATH | XML index helps with queries that use XML path expressions. |
VALUE | XML index helps with queries that search for values anywhere in the XML document. |
PROPERY | XML index helps with queries that retrieve particular object properties from within an XML document. |
To create a secondary XML index, you must use the CREATE XML INDEX statement. Figure 4 shows the syntax for the CREATE XML INDEX.
Figure 4. CREATE XML INDEX Syntax
CREATE XML INDEX index_name ON table_name (xml_column_name) [USING XML INDEX xml_index_name [FOR {VALUE|PATH|PROPERTY}]
|
Using the table we created in Figure 5, we will create a secondary XML index on the customerAddress column (see Figure 5).
Figure 5. CREATE XML INDEX Usage
CREATE XML INDEX [SD_XML_Data_customerAddress] ON XML_Table (customerAddress) USING XML INDEX [PK_XML_Data_customerAddress] FOR VALUE
|
Along with creating primary and secondary indexes on XML data type columns, you can also modify these indexes. The ALTER INDEX Transact-SQL DDL statement can be used to modify existing XML indexes. In Figure 6, we will modify our secondary index, DB_XML_Data_customerAddress, to turn ALLOW_ROW_LOCKS OFF.
Figure 6. ALTER INDEX Usage
ALTER INDEX [SD_XML_Data_customerAddress] ON XML_Table SET(ALLOW_ROW_LOCKS = OFF)
|
By default, XML indexes are ENABLED by default, but you can DISABLE an XML index. To do that, you set the XML index to DISABLE. You DISABLE
an index when you want to preserve the index definition but you do not
want to have the index available for use within the database engine. If
you want to remove the index definition from the database engine, you
need to drop the index instead of disabling it. We will DISABLE the secondary instance we created (see Figure 7).
Figure 7. Using ALTER INDEX to DISABLE an INDEX
ALTER INDEX [SD_XML_Data_customerAddress] on XML_Table DISABLE
|
Of course, you can drop XML indexes. You use the DROP INDEX Transact-SQL DDL statement. If you drop the primary XML index, any secondary indexes that are present are also dropped. In Figure 8, we will drop the secondary index.
Figure 8. Using DROP INDEX to DROP an XML Index
DROP INDEX [SD_XML_Data_customerAddress] ON XML_Table