How to index XML fields in SQL Server 2008

Source: Internet
Author: User
Tags bulk insert

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
  1. CREATE TABLE XMLTable (Id int primary key, xmlcol XML);
  2. Go
  3. --xml Primary Index
  4. Create primary XML index Ipxml_xmltable_xmlcol on XMLTable (Xmlcol);
  5. --xml Path Secondary Index
  6. Create XML index Ixml_xmltable_xmlcol_path on XMLTable (Xmlcol)
  7. Using XML index Ipxml_xmltable_xmlcol for path
  8. --xml Property Secondary Index
  9. Create XML index Ixml_xmltable_xmlcol_property on XMLTable (Xmlcol)
  10. Using XML index Ipxml_xmltable_xmlcol for property
  11. --xml Content Secondary Index
  12. Create XML index Ixml_xmltable_xmlcol_value on XMLTable (Xmlcol)
  13. 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
  1. static void Main (string[] args)
  2. {
  3. DataTable dt = GetTableSchema ();
  4. For (int count = 1; count <= 600000; count++)
  5. {
  6. DataRow r = dt. NewRow ();
  7. R[0] = count;
  8. R[1] = Getpropertyxml ();
  9. Dt. Rows.Add (R);
  10. }
  11. BULKTODB (DT);
  12. Console.WriteLine ("finished");
  13. Console.ReadLine ();
  14. }
  15. Public static void Bulktodb (DataTable dt)
  16. {
  17. SqlConnection sqlconn = new SqlConnection (
  18. configurationmanager.connectionstrings["ConnStr1"].  ConnectionString);
  19. SqlBulkCopy SqlBulkCopy = new SqlBulkCopy (sqlconn);
  20. sqlbulkcopy.bulkcopytimeout = 0;
  21. sqlbulkcopy.batchsize = dt. Rows.Count;
  22. Sqlbulkcopy.destinationtablename = "XMLTable";
  23. Try
  24. {
  25. Sqlconn.open ();
  26. if (dt ! = null && dt. Rows.Count! = 0)
  27. {
  28. Sqlbulkcopy.writetoserver (DT);
  29. }
  30. }
  31. catch (Exception ex)
  32. {
  33. throw ex;
  34. }
  35. finally
  36. {
  37. Sqlconn.close ();
  38. }
  39. }
  40. Public static DataTable GetTableSchema ()
  41. {
  42. DataTable dt = new DataTable ();
  43. Dt. Columns.addrange (new datacolumn[]{
  44. New DataColumn ("Id",typeof (int)),
  45. New DataColumn ("Xmlcol",typeof (string))});
  46. return DT;
  47. }
  48. Public static int getrandrange (int start, int end)
  49. {
  50. Random random = new Random (Guid.NewGuid ().  GetHashCode ());
  51. return random.  Next (start, end);
  52. }
  53. Public static string Getpropertyxml ()
  54. {
  55. StringBuilder buffer = new StringBuilder ();
  56. Buffer.  Appendline ("<TJVICTOR>");
  57. For (int count = 0; count < Getrandrange (1, ten); count++)
  58. {
  59. int basenum = getrandrange (1, 100);
  60. Buffer. Appendline (string.  Format ("<item{0} v=/" property{0}/">Value{0}</Item{0}>", Basenum));
  61. }
  62. Buffer.  Appendline ("</TJVICTOR>");
  63. return buffer.  ToString ();
  64. }

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
  1. --xml Primary Index
  2. Create primary XML index Ipxml_xmltable_xmlcol on XMLTable (Xmlcol);
  3. --xml Path Secondary Index
  4. Create XML index Ixml_xmltable_xmlcol_path on XMLTable (Xmlcol)
  5. Using XML index Ipxml_xmltable_xmlcol for path
  6. --xml Property Secondary Index
  7. Create XML index Ixml_xmltable_xmlcol_property on XMLTable (Xmlcol)
  8. Using XML index Ipxml_xmltable_xmlcol for property
  9. --xml Content Secondary Index
  10. Create XML index Ixml_xmltable_xmlcol_value on XMLTable (Xmlcol)
  11. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.