programming4us
programming4us
DATABASE

SQL Server 2008 : Explaining XML - XML Indexes

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
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)

New & Noteworthy...: XML Data Type

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 IndexDescription
PATHXML index helps with queries that use XML path expressions.
VALUEXML index helps with queries that search for values anywhere in the XML document.
PROPERYXML 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
Other  
 
Top 10
Free Mobile And Desktop Apps For Accessing Restricted Websites
MASERATI QUATTROPORTE; DIESEL : Lure of Italian limos
TOYOTA CAMRY 2; 2.5 : Camry now more comely
KIA SORENTO 2.2CRDi : Fuel-sipping slugger
How To Setup, Password Protect & Encrypt Wireless Internet Connection
Emulate And Run iPad Apps On Windows, Mac OS X & Linux With iPadian
Backup & Restore Game Progress From Any Game With SaveGameProgress
Generate A Facebook Timeline Cover Using A Free App
New App for Women ‘Remix’ Offers Fashion Advice & Style Tips
SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
- Messages forwarded by Outlook rule go nowhere
- Create and Deploy Windows 7 Image
- How do I check to see if my exchange 2003 is an open relay? (not using a open relay tester tool online, but on the console)
- Creating and using an unencrypted cookie in ASP.NET
- Directories
- Poor Performance on Sharepoint 2010 Server
- SBS 2008 ~ The e-mail alias already exists...
- Public to Private IP - DNS Changes
- Send Email from Winform application
- How to create a .mdb file from ms sql server database.......
programming4us programming4us
programming4us
 
 
programming4us