{"id":470,"date":"2012-09-22T15:02:00","date_gmt":"2012-09-22T15:02:00","guid":{"rendered":"\/blogs\/bobb\/post\/Selective-XML-Index-Implementation-Details.aspx"},"modified":"2014-01-20T12:22:15","modified_gmt":"2014-01-20T20:22:15","slug":"selective-xml-index-implementation-details","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/bobb\/selective-xml-index-implementation-details\/","title":{"rendered":"Selective XML Index &#8211; Implementation Details"},"content":{"rendered":"<p>\nA while after I posted part 1, there was a twitter message from Aaron (@AaronBertrand), a well-known &quot;smart person&quot; in the SQL Server space, about using feature packs as &quot;ship vehicles&quot;, especially when they (possibly) have the effect of requiring&nbsp;keeping all instances in-sync at the service pack level to keep things working&nbsp;and possibly even&nbsp;making SP feature relevent during a restore. I answered that &quot;it must be a compelling feature&quot;. And then we reminisced about features like &quot;vardecimal&quot; and &quot;15k partitions&quot;. So I&#39;ll discuss why it is a compelling feature (or at least why I happen to think it is) later on in the process.\n<\/p>\n<p>\nBut for now, about how it&#39;s implemented&#8230;\n<\/p>\n<p>\nBefore this even. You can add or remove path specs on a Selective XML Index. I found this one by &quot;reading into an error message&quot; and trying syntax until it worked.\n<\/p>\n<p>\n&#8212; add a pathspec<br \/>\nalter index fooidx on foo<br \/>\nfor<br \/>\n(<br \/>\nadd another_pathspec = &#39;foo\/bar2&#39;<br \/>\n);\n<\/p>\n<p>\n&#8211;remove the pathspec we just added<br \/>\nalter index fooidx on foo<br \/>\nfor<br \/>\n(<br \/>\nremove another_pathspec <br \/>\n);\n<\/p>\n<p>\nAs Micheal said, the selective XML index is implemented using sparse columns. The first column (or columns) is the primary key of the base table, which they always named pk1&#8230;n. Then a row_id column, which is incremented if there are multiple matching nodes in the same row.\n<\/p>\n<p>\nEach pathspec consists of at least two columns: a path column and&nbsp;1-2 value column(s). Path column is named path_[n]_id (which is always varbinary(900) and sparse).\n<\/p>\n<p>\nValue column is named either &quot;[pathspec_name]_[n]_value&quot; or &quot;[pathspec_name]_[n]_sql_value&quot; (for SQL type pathspec). If the pathspec contains a wildcard, there is an additional column named path_[n]_hid (varbinary(900)) where HID stands for hierarchyid (i.e. the path in the document). A pathspec with an XPath\/XQuery wildcard would look like this: &#39;\/foo\/*\/baz2&#39;.\n<\/p>\n<p>\nAs an aside, remember that the SQL Server XML data type (node paths) and hierarchyid data type use the same encoding scheme, known as Ordpath, see my old blog entrry &quot;<a href=\"http:\/\/3.209.169.194\/blogs\/bobb\/ordpath-ordpath-everywhere\/\" class=\"broken_link\">Ordpath, ordpath, everywhere<\/a>&quot;.\n<\/p>\n<p>\nAll of the columns (except pk1 and row_id) are sparse. As far as data types (because the nice thing is that SPARSE columns can preserve strong typing) as typed as:<br \/>\n&nbsp;Type 1: xs:untypedAtomic = varbinary(max)<br \/>\n&nbsp;Type 2: The closest SQL data type to XSD type (e.g. xs:double = SQL FLOAT)<br \/>\n&nbsp;Type 3: The SQL type specified in the &quot;pathspec AS SQL&#8230;&quot; specification. Including length and collation.\n<\/p>\n<p>\nThe length of all of the sparse columns are based on the length of the column (either express or implied) in the pathspec.\n<\/p>\n<p>\nSo, what&#39;s this all mean?\n<\/p>\n<p>\nIt means that, depending on how selective each pathspec is, how many pathspecs are designation in the Selective XML Index definition and the sparseness of each of the column values, the Selective XML Index can be MUCH, MUCH smaller than the (original) PRIMARY XML INDEX (which is implemented as a 12-column side-table, one row per XML node). Because you&#39;re only indexing those values your care about. And individual node data CAN be sparse in XML (element text can be, but XML attribute nodes are even more likely to be sparse).\n<\/p>\n<p>\nAnd it also means that the limitations of SQL Server 2008 SPARSE columns apply. See&nbsp;this <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/cc280604.aspx\">starting point in Books Online<\/a>&nbsp;to understand these limits in detail. And, because there isn&#39;t a XML columnset column, you are limited to theoretical maximum of 511 pathspecs, if none of them are wildcards and there&#39;s a 1 column primary key&nbsp;(1024 maximum columns in table without a columnset, &#8211; 2 \/ 2. I don&#39;t know what the real limit is yet.\n<\/p>\n<p>\n@bobbeauch<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A while after I posted part 1, there was a twitter message from Aaron (@AaronBertrand), a well-known &quot;smart person&quot; in the SQL Server space, about using feature packs as &quot;ship vehicles&quot;, especially when they (possibly) have the effect of requiring&nbsp;keeping all instances in-sync at the service pack level to keep things working&nbsp;and possibly even&nbsp;making SP [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31,37],"tags":[],"class_list":["post-470","post","type-post","status-publish","format-standard","hentry","category-sql-server-2012","category-sql-server-xml"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.9.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Selective XML Index - Implementation Details - Bob Beauchemin<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/selective-xml-index-implementation-details\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Selective XML Index - Implementation Details - Bob Beauchemin\" \/>\n<meta property=\"og:description\" content=\"A while after I posted part 1, there was a twitter message from Aaron (@AaronBertrand), a well-known &quot;smart person&quot; in the SQL Server space, about using feature packs as &quot;ship vehicles&quot;, especially when they (possibly) have the effect of requiring&nbsp;keeping all instances in-sync at the service pack level to keep things working&nbsp;and possibly even&nbsp;making SP [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/selective-xml-index-implementation-details\/\" \/>\n<meta property=\"og:site_name\" content=\"Bob Beauchemin\" \/>\n<meta property=\"article:published_time\" content=\"2012-09-22T15:02:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2014-01-20T20:22:15+00:00\" \/>\n<meta name=\"author\" content=\"Bob Beauchemin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bob Beauchemin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/selective-xml-index-implementation-details\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/selective-xml-index-implementation-details\/\",\"name\":\"Selective XML Index - Implementation Details - Bob Beauchemin\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\"},\"datePublished\":\"2012-09-22T15:02:00+00:00\",\"dateModified\":\"2014-01-20T20:22:15+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/selective-xml-index-implementation-details\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/bobb\/selective-xml-index-implementation-details\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/selective-xml-index-implementation-details\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL Server 2012\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/sql-server-2012\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Selective XML Index &#8211; Implementation Details\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/\",\"name\":\"Bob Beauchemin\",\"description\":\"SQL Server Blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e\",\"name\":\"Bob Beauchemin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g\",\"caption\":\"Bob Beauchemin\"},\"sameAs\":[\"http:\/www.sqlskills.com\/blogs\/bobb\/\"],\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/author\/bobb\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Selective XML Index - Implementation Details - Bob Beauchemin","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlskills.com\/blogs\/bobb\/selective-xml-index-implementation-details\/","og_locale":"en_US","og_type":"article","og_title":"Selective XML Index - Implementation Details - Bob Beauchemin","og_description":"A while after I posted part 1, there was a twitter message from Aaron (@AaronBertrand), a well-known &quot;smart person&quot; in the SQL Server space, about using feature packs as &quot;ship vehicles&quot;, especially when they (possibly) have the effect of requiring&nbsp;keeping all instances in-sync at the service pack level to keep things working&nbsp;and possibly even&nbsp;making SP [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/selective-xml-index-implementation-details\/","og_site_name":"Bob Beauchemin","article_published_time":"2012-09-22T15:02:00+00:00","article_modified_time":"2014-01-20T20:22:15+00:00","author":"Bob Beauchemin","twitter_misc":{"Written by":"Bob Beauchemin","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/selective-xml-index-implementation-details\/","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/selective-xml-index-implementation-details\/","name":"Selective XML Index - Implementation Details - Bob Beauchemin","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website"},"datePublished":"2012-09-22T15:02:00+00:00","dateModified":"2014-01-20T20:22:15+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/selective-xml-index-implementation-details\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/bobb\/selective-xml-index-implementation-details\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/selective-xml-index-implementation-details\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/"},{"@type":"ListItem","position":2,"name":"SQL Server 2012","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/sql-server-2012\/"},{"@type":"ListItem","position":3,"name":"Selective XML Index &#8211; Implementation Details"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/","name":"Bob Beauchemin","description":"SQL Server Blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlskills.com\/blogs\/bobb\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e","name":"Bob Beauchemin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g","caption":"Bob Beauchemin"},"sameAs":["http:\/www.sqlskills.com\/blogs\/bobb\/"],"url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/author\/bobb\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/470","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/comments?post=470"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/470\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/media?parent=470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/categories?post=470"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/tags?post=470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}