{"id":649,"date":"2009-04-25T22:02:00","date_gmt":"2009-04-25T22:02:00","guid":{"rendered":"\/blogs\/bobb\/post\/How-to-ensure-your-spatial-index-is-being-used.aspx"},"modified":"2013-01-03T23:59:46","modified_gmt":"2013-01-04T07:59:46","slug":"how-to-ensure-your-spatial-index-is-being-used","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/bobb\/how-to-ensure-your-spatial-index-is-being-used\/","title":{"rendered":"How to ensure your spatial index is being used"},"content":{"rendered":"<p>\nI&#39;ve answered quite a few questions lately about how to make sure that your spatial index is being used. Use of a spatial index with a large or complex spatial dataset can mean the difference between a query that takes minutes to execute and subsecond execution. You also want to use an index hint as a last resort, the spatial index should not need a hint. Here&#39;s a few things to try, in order of importance.\n<\/p>\n<p>\n1. Apply SQL Server 2008 SP1!!! I can&#39;t stress this one too much. There was a change to query costing that affected spatial index use.<br \/>\n2. Make the query as uncomplicated as possible. Don&#39;t try to combine STIntersects with a call to STBuffer, MakeValid or other nested spatial method calls or subqueries. Use multiple statements if needed.<br \/>\n3. If you&#39;re running the code in SSMS, use sp_executesql around the spatial query (or use your own stored procedure with the spatial value as a parameter) to ensure the query coster &quot;knows&quot; the parameter value at the time its creating the query plan, that is, at beginning of the batch or on entry to a stored procedure or sp_executesql.Here&#39;s an example:\n<\/p>\n<p>\n&#8212; does not use the spatial index without a hint<br \/>\ndeclare @latlonPoint geometry = geometry::Parse(&#39;POINT (45.518066 -122.767464)&#39;)<br \/>\nselect a.id, a.shape.STAsText() <br \/>\nfrom zipcodes a <br \/>\nwhere a.shape.STIntersects(@latlonPoint)=1<br \/>\ngo\n<\/p>\n<p>\n&#8212; this does use the spatial index without using a hint<br \/>\ndeclare @latlonPoint geometry = geometry::Parse(&#39;POINT (45.518066 -122.767464)&#39;)<br \/>\nexec sp_executesql <br \/>\nN&#39;select a.id, a.shape.STAsText() <br \/>\nfrom zipcodes a <br \/>\nwhere a.shape.STIntersects(@latlonPoint)=1&#39;, <a href=\"mailto:N'@latlonPoint\">N&#39;@latlonPoint<\/a> geometry&#39;, @latlonPoint<br \/>\ngo\n<\/p>\n<p>\n&#8212; so does this<br \/>\ncreate procedure find_zipcode (@g geometry)<br \/>\nas<br \/>\nselect a.id, a.shape.STAsText() <br \/>\nfrom zipcodes a <br \/>\nwhere a.shape.STIntersects(@latlonPoint)=1<br \/>\ngo\n<\/p>\n<p>\ndeclare @latlonPoint geometry = geometry::Parse(&#39;POINT (45.518066 -122.767464)&#39;)<br \/>\nexecute find_zipcode(@latlonPoint)\n<\/p>\n<p>\n4. If you&#39;re using client code, make sure you use a parameterized query and that it passes the value in the SqlParametersCollection. ADO.NET will change this into a call that uses sp_executesql.\n<\/p>\n<p>\n5. Don&#39;t depend of passing in a string literal to give the query coster the right info, because the code &quot;creates&quot; the point inline, after the query plan has been created.:\n<\/p>\n<p>\n&#8212; use a parameterized query with sp_executesql or stored procedure instead<br \/>\nselect a.id, a.shape.STAsText() <br \/>\nfrom zipcodes a <br \/>\nwhere a.shape.STIntersects(&#39;POINT (45.518066 -122.767464)&#39;)=1\n<\/p>\n<p>\n6. Check the query plan (actual or estimated plan will work) to ensure the index is being used. The Spatial Index step in the query plan is easy to locate.\n<\/p>\n<p>\n7. Make sure you have an appropriate spatial index with your data AND for your query sample. You can check this with the spatial index diagnostic stored procedures. To see how to use these procedures, start with the blog series <a href=\"http:\/\/3.209.169.194\/blogs\/bobb\/spatial-index-diagnostic-procs-intro\/\">here<\/a>.\n<\/p>\n<p>\n8. Use a hint as a last resort and see if it makes a difference in the query speed. If using a spatial index hint causes an error &quot;Could not create plan&quot;, it may mean that your query is too complex. See step 2.\n<\/p>\n<p>\nI hope this helps your query response be as fast as possible.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#39;ve answered quite a few questions lately about how to make sure that your spatial index is being used. Use of a spatial index with a large or complex spatial dataset can mean the difference between a query that takes minutes to execute and subsecond execution. You also want to use an index hint as [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,29,36],"tags":[],"class_list":["post-649","post","type-post","status-publish","format-standard","hentry","category-performance","category-sql-server-2008","category-sql-server-spatial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.9.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to ensure your spatial index is being used - 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\/how-to-ensure-your-spatial-index-is-being-used\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to ensure your spatial index is being used - Bob Beauchemin\" \/>\n<meta property=\"og:description\" content=\"I&#039;ve answered quite a few questions lately about how to make sure that your spatial index is being used. Use of a spatial index with a large or complex spatial dataset can mean the difference between a query that takes minutes to execute and subsecond execution. You also want to use an index hint as [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/how-to-ensure-your-spatial-index-is-being-used\/\" \/>\n<meta property=\"og:site_name\" content=\"Bob Beauchemin\" \/>\n<meta property=\"article:published_time\" content=\"2009-04-25T22:02:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2013-01-04T07:59:46+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\/how-to-ensure-your-spatial-index-is-being-used\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/how-to-ensure-your-spatial-index-is-being-used\/\",\"name\":\"How to ensure your spatial index is being used - Bob Beauchemin\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\"},\"datePublished\":\"2009-04-25T22:02:00+00:00\",\"dateModified\":\"2013-01-04T07:59:46+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/how-to-ensure-your-spatial-index-is-being-used\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/bobb\/how-to-ensure-your-spatial-index-is-being-used\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/how-to-ensure-your-spatial-index-is-being-used\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Performance\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/performance\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"How to ensure your spatial index is being used\"}]},{\"@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":"How to ensure your spatial index is being used - 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\/how-to-ensure-your-spatial-index-is-being-used\/","og_locale":"en_US","og_type":"article","og_title":"How to ensure your spatial index is being used - Bob Beauchemin","og_description":"I&#39;ve answered quite a few questions lately about how to make sure that your spatial index is being used. Use of a spatial index with a large or complex spatial dataset can mean the difference between a query that takes minutes to execute and subsecond execution. You also want to use an index hint as [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/how-to-ensure-your-spatial-index-is-being-used\/","og_site_name":"Bob Beauchemin","article_published_time":"2009-04-25T22:02:00+00:00","article_modified_time":"2013-01-04T07:59:46+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\/how-to-ensure-your-spatial-index-is-being-used\/","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/how-to-ensure-your-spatial-index-is-being-used\/","name":"How to ensure your spatial index is being used - Bob Beauchemin","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website"},"datePublished":"2009-04-25T22:02:00+00:00","dateModified":"2013-01-04T07:59:46+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/how-to-ensure-your-spatial-index-is-being-used\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/bobb\/how-to-ensure-your-spatial-index-is-being-used\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/how-to-ensure-your-spatial-index-is-being-used\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/"},{"@type":"ListItem","position":2,"name":"Performance","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/performance\/"},{"@type":"ListItem","position":3,"name":"How to ensure your spatial index is being used"}]},{"@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\/649","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=649"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/649\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/media?parent=649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/categories?post=649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/tags?post=649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}