from:http://blog.csdn.net/tjvictor/article/details/4370771
XML indexes in SQL Server fall into two categories: primary XML indexes and secondary XML indexes. The secondary XML index is divided into: PATH secondary XML index, VALUE Secondary XML index, property auxiliary XML index.
An example of the syntax for creating an XML index is as follows:
[C-sharp]View PlainCopy
- CREATE TABLE XMLTable (Id int primary key, xmlcol XML);
- Go
- --xml Primary Index
- Create primary XML index Ipxml_xmltable_xmlcol on XMLTable (Xmlcol);
- --xml Path Secondary Index
- Create XML index Ixml_xmltable_xmlcol_path on XMLTable (Xmlcol)
- Using XML index Ipxml_xmltable_xmlcol for path
- --xml Property Secondary Index
- Create XML index Ixml_xmltable_xmlcol_property on XMLTable (Xmlcol)
- Using XML index Ipxml_xmltable_xmlcol for property
- --xml Content Secondary Index
- Create XML index Ixml_xmltable_xmlcol_value on XMLTable (Xmlcol)
- Using XML index Ipxml_xmltable_xmlcol for value
It is important to note that the table that establishes the XML index must have a primary key.
The benefit of indexing is to improve query efficiency, and the downside is to increase storage space. The following examples illustrate:
1. First build the test table, execute the following SQL statement in SSMs CREATE TABLE XMLTable (Id int primary key, xmlcol XML);
2. The following procedure is to add 600,000 data to the table to facilitate test performance. As for why add a program instead of an INSERT statement, see my other blog: SQL Server two ways to bulk insert data http://blog.csdn.net/tjvictor/archive/2009/07/18/4360030.aspx
[C-sharp]View PlainCopy
- static void Main (string[] args)
- {
- DataTable dt = GetTableSchema ();
- For (int count = 1; count <= 600000; count++)
- {
- DataRow r = dt. NewRow ();
- R[0] = count;
- R[1] = Getpropertyxml ();
- Dt. Rows.Add (R);
- }
- BULKTODB (DT);
- Console.WriteLine ("finished");
- Console.ReadLine ();
- }
- Public static void Bulktodb (DataTable dt)
- {
- SqlConnection sqlconn = new SqlConnection (
- configurationmanager.connectionstrings["ConnStr1"]. ConnectionString);
- SqlBulkCopy SqlBulkCopy = new SqlBulkCopy (sqlconn);
- sqlbulkcopy.bulkcopytimeout = 0;
- sqlbulkcopy.batchsize = dt. Rows.Count;
- Sqlbulkcopy.destinationtablename = "XMLTable";
- Try
- {
- Sqlconn.open ();
- if (dt ! = null && dt. Rows.Count! = 0)
- {
- Sqlbulkcopy.writetoserver (DT);
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- Sqlconn.close ();
- }
- }
- Public static DataTable GetTableSchema ()
- {
- DataTable dt = new DataTable ();
- Dt. Columns.addrange (new datacolumn[]{
- New DataColumn ("Id",typeof (int)),
- New DataColumn ("Xmlcol",typeof (string))});
- return DT;
- }
- Public static int getrandrange (int start, int end)
- {
- Random random = new Random (Guid.NewGuid (). GetHashCode ());
- return random. Next (start, end);
- }
- Public static string Getpropertyxml ()
- {
- StringBuilder buffer = new StringBuilder ();
- Buffer. Appendline ("<TJVICTOR>");
- For (int count = 0; count < Getrandrange (1, ten); count++)
- {
- int basenum = getrandrange (1, 100);
- Buffer. Appendline (string. Format ("<item{0} v=/" property{0}/">Value{0}</Item{0}>", Basenum));
- }
- Buffer. Appendline ("</TJVICTOR>");
- return buffer. ToString ();
- }
3. Execute a query statement, paying attention to its execution time and execution plan:
Select Id from XMLTable
where Xmlcol.exist ('/tjvictor/item3 ') =1
Because the machine configuration is different, the execution time will not be exactly the same, here only the execution plan, for reference:
All time is spent on table valued function, and still clustered index scan.
4. Add an index to the XML field for this table.
[C-sharp]View PlainCopy
- --xml Primary Index
- Create primary XML index Ipxml_xmltable_xmlcol on XMLTable (Xmlcol);
- --xml Path Secondary Index
- Create XML index Ixml_xmltable_xmlcol_path on XMLTable (Xmlcol)
- Using XML index Ipxml_xmltable_xmlcol for path
- --xml Property Secondary Index
- Create XML index Ixml_xmltable_xmlcol_property on XMLTable (Xmlcol)
- Using XML index Ipxml_xmltable_xmlcol for property
- --xml Content Secondary Index
- Create XML index Ixml_xmltable_xmlcol_value on XMLTable (Xmlcol)
- Using XML index Ipxml_xmltable_xmlcol for value
Note: Because we already have 600,000 data in our table, the index time will be very long, and will consume a lot of memory and disk, I spent 10 minutes or so, accounted for 1G of memory, and 1.3G disk. Please pay attention to your hard disk space when you build the index, or modify the previously inserted data program, less insert some data.
5. Re-execute the above SQL statement:
Select Id from XMLTable
where Xmlcol.exist ('/tjvictor/item3 ') =1
You will find that the results are instantaneous, the following is the execution plan, using the XML index seek.
Summary: After the establishment of XML index, query efficiency will greatly improve, after my test, xml.exist the most efficient execution, basically improve a data level, other statements such as Xml.query,xml.value, query speed increased by about one times, but the overall is not too ideal. But also found that the XML index too much space, such as the above 600,000 records, the space occupancy ratio is as follows:
Name rows reserved data index_size unused
XMLTable 600000 1479688 kb 160952 KB 1318184 KB 552 kb
For basic usage methods such as Xml.exist,xml.query,xml.value, please refer to the following article:
Http://blog.csdn.net/tjvictor/archive/2009/07/21/4368511.aspx
If you want to reprint, please specify the original from CSDN tjvictor column of this article:
Http://blog.csdn.net/tjvictor/archive/2009/07/22/4370771.aspx
How to index XML fields in SQL Server 2008