{"id":1645,"date":"2013-01-07T13:07:36","date_gmt":"2013-01-07T21:07:36","guid":{"rendered":"http:\/\/3.209.169.194\/blogs\/bobb\/?p=1645"},"modified":"2013-01-07T13:07:36","modified_gmt":"2013-01-07T21:07:36","slug":"choosing-paths-for-selective-xml-index-part-2-using-the-sxi-xevents","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/bobb\/choosing-paths-for-selective-xml-index-part-2-using-the-sxi-xevents\/","title":{"rendered":"Choosing Paths for Selective XML Index \u2013 Part 2 &#8211; Using the SXI XEvents"},"content":{"rendered":"<p>In part 1 of this series, we tried a simple example from the Books Online without any SXI index at all, and an SXI with all the paths covered. No XEvents were emitted in either case. Now let&#8217;s change the XSI around (by dropping and recreating, although we could use ALTER, so envision a drop in between each example) to see what triggers the XEvents and what the query plan effects are.<\/p>\n<p>&#8212; first let&#8217;s back off one path<br \/>\nCREATE SELECTIVE XML INDEX simple_sxi ON Tbl(xmlcol)<br \/>\nFOR (<br \/>\npath123 =\u00a0 &#8216;\/a\/b&#8217; as XQUERY &#8216;node()&#8217;<br \/>\n&#8212; path124 =\u00a0 &#8216;\/a\/b\/c&#8217;<br \/>\n);<\/p>\n<p>&#8212; now, you get XML Reader, XML Reader with XPath Filer, CI Seek on SIDX, CI Scan on base table<br \/>\n&#8212; and &#8220;selective_xml_index_path_not_indexed&#8221; XEvent for \/a\/b\/c<br \/>\nSELECT T.id FROM tbl T WHERE T.xmlcol.exist(&#8216;\/a\/b[c = &#8220;43&#8221;]&#8217;) = 1<\/p>\n<p>&#8212; how about adding the SINGLETON keyword and XQuery Type?<br \/>\nCREATE SELECTIVE XML INDEX simple_sxi ON Tbl(xmlcol)<br \/>\nFOR (<br \/>\npath123 =\u00a0 &#8216;\/a\/b&#8217; as XQUERY &#8216;node()&#8217;,<br \/>\npath124 =\u00a0 &#8216;\/a\/b\/c&#8217; as XQUERY &#8216;xs:string&#8217; SINGLETON<br \/>\n);<\/p>\n<p>&#8212; now, you get a single CI Seek on SIDX and CI Scan on base table<br \/>\nSELECT T.id FROM tbl T WHERE T.xmlcol.exist(&#8216;\/a\/b[c = &#8220;43&#8221;]&#8217;) = 1<\/p>\n<p>&#8212; how about an XML.value query instead of XML.exists?<br \/>\n&#8212; get XML Reader with XPath Filter, XML Reader, and CI Scan on base table<br \/>\n&#8212; and &#8220;selective_xml_index_no_compatible_sql_type&#8221; XEvent for \/a\/b\/c<br \/>\nSELECT T.id FROM tbl T WHERE T.xmlcol.value(&#8216;(\/a\/b\/c)[1]&#8217;, &#8216;nvarchar(10)&#8217;) = &#8217;43&#8217;;<\/p>\n<p>&#8212; For this to use SXI, we need:<br \/>\nALTER INDEX simple_sxi ON Tbl FOR (add path124s = &#8216;\/a\/b\/c&#8217; as SQL nvarchar(10) SINGLETON);<\/p>\n<p>&#8212; SXI CI Seek and CI Scan on base table, no XEvents<br \/>\nSELECT T.id FROM tbl T WHERE T.xmlcol.value(&#8216;(\/a\/b\/c)[1]&#8217;, &#8216;nvarchar(10)&#8217;) = &#8217;43&#8217;;<\/p>\n<p>BTW, if you use &#8216;varchar(10)&#8217; rather than &#8216;nvarchar(10)&#8217; in the query, the SXI won&#8217;t be used. One last one, let&#8217;s see if the SXI will be used if we specify an incorrect XQuery type:<\/p>\n<p>CREATE SELECTIVE XML INDEX simple_sxi ON Tbl(xmlcol)<br \/>\nFOR (<br \/>\npath123 =\u00a0 &#8216;\/a\/b&#8217; as XQUERY &#8216;node()&#8217;,<br \/>\npath124 =\u00a0 &#8216;\/a\/b\/c&#8217; as XQUERY &#8216;xs:double&#8217; SINGLETON<br \/>\n);<\/p>\n<p>&#8212; We get &#8220;selective_xml_index_no_compatible_xsd_types&#8221; XEvent for \/a\/b\/c<br \/>\n&#8212; And XML Reader with XPath Filter, XML Reader, CI Seek on SIDX and CI Scan on base table<br \/>\nSELECT T.id FROM tbl T WHERE T.xmlcol.exist(&#8216;\/a\/b[c = &#8220;43&#8221;]&#8217;) = 1;<\/p>\n<p>So, to summarize, in this post and the last post we&#8217;ve shown:<br \/>\nThe SXI cannot be used to search for mixed content.<br \/>\nThe &#8216;AS XQUERY node()&#8217; specification can be used to check for existance of non-terminating node.<br \/>\nThe XEvents for SXI can guide you to understanding which nodes to specify.<br \/>\nYou must use &#8220;As SQL&#8230;&#8221; hint in order for SXI to be used in XML.value method.<br \/>\nYou use &#8220;As XQUERY&#8230;&#8221; hint to use SXI for XQuery predicates or paths in the XML.exist method.<\/p>\n<p>To go back to the BOL examples and test your understanding, write a selective XML index create statement for the following:<\/p>\n<p>SELECT T.record,<br \/>\nT.xmldata.value(&#8216;(\/a\/b\/c\/d\/e[.\/f = &#8220;SQL&#8221;]\/g)[1]&#8217;, &#8216;nvarchar(100)&#8217;)<br \/>\nFROM myXMLTable T<\/p>\n<p>Cheers, Bob<\/p>\n<p>@bobbeauch<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In part 1 of this series, we tried a simple example from the Books Online without any SXI index at all, and an SXI with all the paths covered. No XEvents were emitted in either case. Now let&#8217;s change the XSI around (by dropping and recreating, although we could use ALTER, so envision a drop [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,31,37],"tags":[],"class_list":["post-1645","post","type-post","status-publish","format-standard","hentry","category-event-notifications","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>Choosing Paths for Selective XML Index \u2013 Part 2 - Using the SXI XEvents - 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\/choosing-paths-for-selective-xml-index-part-2-using-the-sxi-xevents\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Choosing Paths for Selective XML Index \u2013 Part 2 - Using the SXI XEvents - Bob Beauchemin\" \/>\n<meta property=\"og:description\" content=\"In part 1 of this series, we tried a simple example from the Books Online without any SXI index at all, and an SXI with all the paths covered. No XEvents were emitted in either case. Now let&#8217;s change the XSI around (by dropping and recreating, although we could use ALTER, so envision a drop [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/choosing-paths-for-selective-xml-index-part-2-using-the-sxi-xevents\/\" \/>\n<meta property=\"og:site_name\" content=\"Bob Beauchemin\" \/>\n<meta property=\"article:published_time\" content=\"2013-01-07T21:07:36+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\/choosing-paths-for-selective-xml-index-part-2-using-the-sxi-xevents\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/choosing-paths-for-selective-xml-index-part-2-using-the-sxi-xevents\/\",\"name\":\"Choosing Paths for Selective XML Index \u2013 Part 2 - Using the SXI XEvents - Bob Beauchemin\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\"},\"datePublished\":\"2013-01-07T21:07:36+00:00\",\"dateModified\":\"2013-01-07T21:07:36+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/choosing-paths-for-selective-xml-index-part-2-using-the-sxi-xevents\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/bobb\/choosing-paths-for-selective-xml-index-part-2-using-the-sxi-xevents\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/choosing-paths-for-selective-xml-index-part-2-using-the-sxi-xevents\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Event Notifications\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/event-notifications\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Choosing Paths for Selective XML Index \u2013 Part 2 &#8211; Using the SXI XEvents\"}]},{\"@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":"Choosing Paths for Selective XML Index \u2013 Part 2 - Using the SXI XEvents - 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\/choosing-paths-for-selective-xml-index-part-2-using-the-sxi-xevents\/","og_locale":"en_US","og_type":"article","og_title":"Choosing Paths for Selective XML Index \u2013 Part 2 - Using the SXI XEvents - Bob Beauchemin","og_description":"In part 1 of this series, we tried a simple example from the Books Online without any SXI index at all, and an SXI with all the paths covered. No XEvents were emitted in either case. Now let&#8217;s change the XSI around (by dropping and recreating, although we could use ALTER, so envision a drop [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/choosing-paths-for-selective-xml-index-part-2-using-the-sxi-xevents\/","og_site_name":"Bob Beauchemin","article_published_time":"2013-01-07T21:07:36+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\/choosing-paths-for-selective-xml-index-part-2-using-the-sxi-xevents\/","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/choosing-paths-for-selective-xml-index-part-2-using-the-sxi-xevents\/","name":"Choosing Paths for Selective XML Index \u2013 Part 2 - Using the SXI XEvents - Bob Beauchemin","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website"},"datePublished":"2013-01-07T21:07:36+00:00","dateModified":"2013-01-07T21:07:36+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/choosing-paths-for-selective-xml-index-part-2-using-the-sxi-xevents\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/bobb\/choosing-paths-for-selective-xml-index-part-2-using-the-sxi-xevents\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/choosing-paths-for-selective-xml-index-part-2-using-the-sxi-xevents\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/"},{"@type":"ListItem","position":2,"name":"Event Notifications","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/event-notifications\/"},{"@type":"ListItem","position":3,"name":"Choosing Paths for Selective XML Index \u2013 Part 2 &#8211; Using the SXI XEvents"}]},{"@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\/1645","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=1645"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/1645\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/media?parent=1645"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/categories?post=1645"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/tags?post=1645"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}