{"id":629,"date":"2011-01-16T13:07:00","date_gmt":"2011-01-16T13:07:00","guid":{"rendered":"\/blogs\/paul\/post\/Adventures-in-query-tuning-non-seekable-WHERE-clause-expressions.aspx"},"modified":"2017-07-12T13:10:42","modified_gmt":"2017-07-12T20:10:42","slug":"adventures-in-query-tuning-non-seekable-where-clause-expressions","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/paul\/adventures-in-query-tuning-non-seekable-where-clause-expressions\/","title":{"rendered":"Adventures in query tuning: non-seekable WHERE clause expressions"},"content":{"rendered":"<p style=\"text-align: justify;\">I&#8217;ve been doing a lot of performance tuning work over the last couple of months and this weekend found something that&#8217;s very pervasive out in the wild. Kimberly was helping me optimize a gnarly query plan and spotted something in the code I hadn&#8217;t noticed that was causing an index scan instead of an index seek. Even though the query plan was using a covering nonclustered index, the way the code was written was causing the index to be scanned.<\/p>\n<p>I&#8217;ll set up a test to show you what I mean.<\/p>\n<p>First, create an\u00a0example sales tracking\u00a0table and populate it (took 3m45s on my laptop):<\/p>\n<pre class=\"brush: sql; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\n-- Create example table\r\nCREATE TABLE &#x5B;BigTableLotsOfColumns] (\r\n    &#x5B;SalesID] BIGINT IDENTITY,\r\n    &#x5B;SalesDate] DATETIME,\r\n    &#x5B;Descr] VARCHAR (100),\r\n    &#x5B;CustomerID] INT,\r\n    &#x5B;ProductID] INT,\r\n    &#x5B;ModifyDate] DATETIME DEFAULT NULL,\r\n    &#x5B;PayDate] DATETIME DEFAULT NULL,\r\n    &#x5B;Quantity] INT,\r\n    &#x5B;Price] DECIMAL (6,2),\r\n    &#x5B;Discount] INT DEFAULT NULL,\r\n    &#x5B;WarehouseID] INT,\r\n    &#x5B;PickerID] INT,\r\n    &#x5B;ShipperID] INT);\r\nGO\r\n\r\n-- Populate table\r\nSET NOCOUNT ON;\r\n\r\nDECLARE @Date DATETIME;\r\nDECLARE @Loop INT;\r\n\r\nSET @Loop = 1;\r\nSET @Date = '01\/01\/2010';\r\n\r\n-- Insert 1500 sales per day\r\nWHILE @Loop &amp;lt; 547600\r\nBEGIN\r\n    INSERT INTO &#x5B;BigTableLotsOfColumns] (\r\n        &#x5B;SalesDate], &#x5B;Descr], &#x5B;CustomerID], &#x5B;ProductID], &#x5B;Quantity],\r\n        &#x5B;Price], &#x5B;WarehouseID], &#x5B;PickerID], &#x5B;ShipperID])\r\n     VALUES (\r\n        @Date,\r\n        'A nice order from someone',\r\n        CONVERT (INT, (RAND () * 1000)),\r\n        CONVERT (INT, (RAND () * 1000)),\r\n        CONVERT (INT, (RAND () * 10)),\r\n        ROUND (RAND () * 1000, 2),\r\n        1,\r\n        CONVERT (INT, (RAND () * 4)),\r\n        CONVERT (INT, (RAND () * 4)));\r\n\r\n    IF @Loop % 1500 = 0 SET @Date = @Date + 1;\r\n\r\n    SET @Loop = @Loop + 1;\r\nEND\r\nGO\r\n\r\n-- And a clustered index\r\nCREATE CLUSTERED INDEX &#x5B;IX_BigTableLotsOfColumns_Clustered] ON &#x5B;BigTableLotsOfColumns] (&#x5B;SalesID]);\r\nGO\r\n<\/pre>\n<p>Now I&#8217;ve got a simple stored procedure to find the total sales amount for any particular date.<\/p>\n<pre class=\"brush: sql; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nCREATE PROCEDURE &#x5B;TotalSalesForDate] (@TargetDate DATETIME)\r\nAS\r\n    SELECT\r\n        SUM (&#x5B;Quantity] * &#x5B;Price])\r\n    FROM\r\n        &#x5B;BigTableLotsOfColumns]\r\n    WHERE DATEDIFF (DAY, &#x5B;SalesDate], @TargetDate) = 0\r\n    OPTION (MAXDOP 1);\r\nGO\r\n<\/pre>\n<p style=\"text-align: justify;\">The idea is that the stored procedure will pick up all sales for a particular date, no matter what time of that day the sale occurred. I&#8217;ve got <em>OPTION (MAXDOP 1)<\/em> to simulate a system that&#8217;s been tuned for OLTP queries (and to make the query plans fit better in my blog post :-)<\/p>\n<p style=\"text-align: justify;\">If I run the stored procedure for, say, December 26th 2010, the query plan it uses is as follows:<\/p>\n<pre class=\"brush: sql; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nEXEC TotalSalesForDate '12\/26\/2010';\r\nGO\r\n<\/pre>\n<p><img decoding=\"async\" src=\"\/blogs\/paul\/wp-content\/uploads\/2011\/1\/plan1.jpg\" alt=\"\" \/><\/p>\n<p>And it uses the following CPU and I\/O (you can get these by using <em>SET STATISTICS IO ON<\/em> and <em>SET STATISTICS TIME ON<\/em>, but be careful &#8211; the can generate lots of output for big procs):<\/p>\n<pre class=\"brush: plain; gutter: false; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nTable 'BigTableLotsOfColumns'. Scan count 1, logical reads 7229, physical reads 0, ...\r\n\r\nSQL Server Execution Times:\r\nCPU time = 125 ms,\u00a0 elapsed time = 186 ms.\r\n<\/pre>\n<p>I ran the proc twice; once to get it\u00a0to compile, and the second time to factor out the compile time.<\/p>\n<p style=\"text-align: justify;\">Pretty obvious that I can speed this up by creating a covering nonclustered index, and in fact you can see the query processor telling me that with the missing index suggestion. I create the covering nonclustered index:<\/p>\n<pre class=\"brush: sql; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nCREATE NONCLUSTERED INDEX &#x5B;IX_BigTableLotsOfColumns_Date] ON\r\n    &#x5B;BigTableLotsOfColumns] (&#x5B;SalesDate]) INCLUDE (&#x5B;Quantity], &#x5B;Price]);\r\n<\/pre>\n<p>Now when I run the proc, I get a better query plan:<\/p>\n<p><img decoding=\"async\" src=\"\/blogs\/paul\/wp-content\/uploads\/2011\/1\/plan2.jpg\" alt=\"\" \/><\/p>\n<p>And the following CPU and IO:<\/p>\n<pre class=\"brush: plain; gutter: false; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nTable 'BigTableLotsOfColumns'. Scan count 1, logical reads 2113, physical reads 0,\u00a0...\r\n\r\nSQL Server Execution Times:\r\nCPU time = 78 ms,\u00a0 elapsed time = 115 ms.\r\n<\/pre>\n<p style=\"text-align: justify;\">Much better, as I&#8217;d expect. But look at how the nonclustered index is being used: it&#8217;s being scanned when instead there should be a seek. It&#8217;s still doing more than 2,000 logical reads because it&#8217;s scanning the whole index.<\/p>\n<p style=\"text-align: justify;\">The reason for this is that the <em>DATEDIFF<\/em> in the proc is forcing every <em>SalesDate<\/em> column value to be evaluated, hence the scan. If I know that the proc only ever is called with a date without a time portion, I can do some rearranging of the logic.<\/p>\n<p style=\"text-align: justify;\">The updated proc is below. Notice that there&#8217;s no reason to scan any more as the <em>WHERE<\/em> clause doesn&#8217;t require every column value to passed through the <em>DATEDIFF<\/em>:<\/p>\n<pre class=\"brush: sql; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nCREATE PROCEDURE &#x5B;TotalSalesForDate] (@TargetDate DATETIME)\r\nAS\r\n    SELECT\r\n        SUM (&#x5B;Quantity] * &#x5B;Price])\r\n    FROM\r\n        &#x5B;BigTableLotsOfColumns]\r\n    WHERE &#x5B;SalesDate] &gt;= @TargetDate AND &#x5B;SalesDate] &lt; DATEADD (DAY, 1, @TargetDate)\r\n        OPTION (MAXDOP 1);\r\nGO\r\n<\/pre>\n<p>And when I run it I get the best plan:<\/p>\n<p><img decoding=\"async\" src=\"\/blogs\/paul\/wp-content\/uploads\/2011\/1\/plan3.jpg\" alt=\"\" \/><\/p>\n<p>And the following CPU and I\/O:<\/p>\n<pre class=\"brush: plain; gutter: false; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nTable 'BigTableLotsOfColumns'. Scan count 1, logical reads 10, physical reads 0, read-ahead reads 0,\u00a0...\r\n\r\nSQL Server Execution Times:\r\nCPU time = 0 ms,\u00a0 elapsed time = 64 ms.\r\n<\/pre>\n<p>Wow! 200x fewer logical I\/Os and not even enough CPU time to register as 1ms.<\/p>\n<p style=\"text-align: justify;\">Summary: although you may have nonclustered indexes that are being used, make sure that they&#8217;re being used for seeks as you expect. For small queries that are executed hundreds of times a second, the difference between seeking and scanning can be huge in overall performance.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been doing a lot of performance tuning work over the last couple of months and this weekend found something that&#8217;s very pervasive out in the wild. Kimberly was helping me optimize a gnarly query plan and spotted something in the code I hadn&#8217;t noticed that was causing an index scan instead of an index [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66,73],"tags":[],"class_list":["post-629","post","type-post","status-publish","format-standard","hentry","category-performance-tuning","category-query-tuning"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Adventures in query tuning: non-seekable WHERE clause expressions - Paul S. Randal<\/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\/paul\/adventures-in-query-tuning-non-seekable-where-clause-expressions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Adventures in query tuning: non-seekable WHERE clause expressions - Paul S. Randal\" \/>\n<meta property=\"og:description\" content=\"I&#8217;ve been doing a lot of performance tuning work over the last couple of months and this weekend found something that&#8217;s very pervasive out in the wild. Kimberly was helping me optimize a gnarly query plan and spotted something in the code I hadn&#8217;t noticed that was causing an index scan instead of an index [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/paul\/adventures-in-query-tuning-non-seekable-where-clause-expressions\/\" \/>\n<meta property=\"og:site_name\" content=\"Paul S. Randal\" \/>\n<meta property=\"article:published_time\" content=\"2011-01-16T13:07:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-07-12T20:10:42+00:00\" \/>\n<meta name=\"author\" content=\"Paul Randal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Paul Randal\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/adventures-in-query-tuning-non-seekable-where-clause-expressions\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/adventures-in-query-tuning-non-seekable-where-clause-expressions\/\",\"name\":\"Adventures in query tuning: non-seekable WHERE clause expressions - Paul S. Randal\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#website\"},\"datePublished\":\"2011-01-16T13:07:00+00:00\",\"dateModified\":\"2017-07-12T20:10:42+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/adventures-in-query-tuning-non-seekable-where-clause-expressions\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/paul\/adventures-in-query-tuning-non-seekable-where-clause-expressions\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/adventures-in-query-tuning-non-seekable-where-clause-expressions\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Adventures in query tuning: non-seekable WHERE clause expressions\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#website\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/\",\"name\":\"Paul S. Randal\",\"description\":\"In Recovery...\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce\",\"name\":\"Paul Randal\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g\",\"caption\":\"Paul Randal\"},\"sameAs\":[\"http:\/\/3.209.169.194\/blogs\/paul\"],\"url\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/author\/paul\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Adventures in query tuning: non-seekable WHERE clause expressions - Paul S. Randal","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\/paul\/adventures-in-query-tuning-non-seekable-where-clause-expressions\/","og_locale":"en_US","og_type":"article","og_title":"Adventures in query tuning: non-seekable WHERE clause expressions - Paul S. Randal","og_description":"I&#8217;ve been doing a lot of performance tuning work over the last couple of months and this weekend found something that&#8217;s very pervasive out in the wild. Kimberly was helping me optimize a gnarly query plan and spotted something in the code I hadn&#8217;t noticed that was causing an index scan instead of an index [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/paul\/adventures-in-query-tuning-non-seekable-where-clause-expressions\/","og_site_name":"Paul S. Randal","article_published_time":"2011-01-16T13:07:00+00:00","article_modified_time":"2017-07-12T20:10:42+00:00","author":"Paul Randal","twitter_misc":{"Written by":"Paul Randal","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/adventures-in-query-tuning-non-seekable-where-clause-expressions\/","url":"https:\/\/www.sqlskills.com\/blogs\/paul\/adventures-in-query-tuning-non-seekable-where-clause-expressions\/","name":"Adventures in query tuning: non-seekable WHERE clause expressions - Paul S. Randal","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#website"},"datePublished":"2011-01-16T13:07:00+00:00","dateModified":"2017-07-12T20:10:42+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/adventures-in-query-tuning-non-seekable-where-clause-expressions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/paul\/adventures-in-query-tuning-non-seekable-where-clause-expressions\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/adventures-in-query-tuning-non-seekable-where-clause-expressions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/paul\/"},{"@type":"ListItem","position":2,"name":"Adventures in query tuning: non-seekable WHERE clause expressions"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#website","url":"https:\/\/www.sqlskills.com\/blogs\/paul\/","name":"Paul S. Randal","description":"In Recovery...","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlskills.com\/blogs\/paul\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce","name":"Paul Randal","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g","caption":"Paul Randal"},"sameAs":["http:\/\/3.209.169.194\/blogs\/paul"],"url":"https:\/\/www.sqlskills.com\/blogs\/paul\/author\/paul\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/posts\/629","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/comments?post=629"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/posts\/629\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/media?parent=629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/categories?post=629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/tags?post=629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}