{"id":186,"date":"2016-03-31T09:30:42","date_gmt":"2016-03-31T08:30:42","guid":{"rendered":"https:\/\/sqldoubleg.live-website.com\/?p=186"},"modified":"2016-03-31T11:32:12","modified_gmt":"2016-03-31T10:32:12","slug":"index-creation-vs-ssms-query","status":"publish","type":"post","link":"https:\/\/www.sqldoubleg.com\/es\/2016\/03\/31\/index-creation-vs-ssms-query\/","title":{"rendered":"Index Creation VS SSMS Query"},"content":{"rendered":"<p>Is it true that creating the index is quicker than running the query you needed it for? Let me show you the answer&nbsp;<br \/>\nTonight, Ladies and Gentlemen, we have one of the most expected fights in SQL Server!! Yeeeeeah!! Booooohhhh!!! (People cheering and booing)<\/p>\n<p>Hm, now seriously, I just read a comment in a blog post called <a href=\"https:\/\/www.brentozar.com\/archive\/2016\/03\/indexes-helping-indexes\/\" target=\"_blank\">Indexes Helping Indexes<\/a> and one user wondered why creating an index is always faster than running the same SELECT statement.<\/p>\n<p>My immediate thought was that cannot be true, but I need to prove it!<\/p>\n<p>For these cases where I need a slightly longer running query, I use the big version of AdventureWorks, you can find it in <a href=\"http:\/\/sqlblog.com\/blogs\/adam_machanic\/archive\/2011\/10\/17\/thinking-big-adventure.aspx\" target=\"_blank\">Adam Machanic&#8217;s blog<\/a>.<\/p>\n<p>I will create one index on [dbo].[bigTransactionHistory] and then create an index using the same columns and sorting<\/p>\n<p>First things first, here is the table, about 31M rows.<\/p>\n<p><a href=\"https:\/\/sqldoubleg.live-website.com\/wp-content\/uploads\/2016\/03\/COUNT_transactionBig.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/sqldoubleg.live-website.com\/wp-content\/uploads\/2016\/03\/COUNT_transactionBig.png\" alt=\"COUNT_transactionBig\" width=\"682\" height=\"172\" class=\"aligncenter size-full wp-image-187\" srcset=\"https:\/\/www.sqldoubleg.com\/wp-content\/uploads\/2016\/03\/COUNT_transactionBig.png 682w, https:\/\/www.sqldoubleg.com\/wp-content\/uploads\/2016\/03\/COUNT_transactionBig-300x76.png 300w, https:\/\/www.sqldoubleg.com\/wp-content\/uploads\/2016\/03\/COUNT_transactionBig-150x38.png 150w\" sizes=\"(max-width: 682px) 100vw, 682px\" \/><\/a><\/p>\n<p>To be fair to both opponents, I will do a little trick to get all data into cache, so both will benefit from not having to go disk to read it. (You&#8217;ll see it later)<\/p>\n<p>Now I can create the index<\/p>\n<p><a href=\"https:\/\/sqldoubleg.live-website.com\/wp-content\/uploads\/2016\/03\/create_index.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/sqldoubleg.live-website.com\/wp-content\/uploads\/2016\/03\/create_index.png\" alt=\"create_index\" width=\"818\" height=\"341\" class=\"aligncenter size-full wp-image-189\" srcset=\"https:\/\/www.sqldoubleg.com\/wp-content\/uploads\/2016\/03\/create_index.png 818w, https:\/\/www.sqldoubleg.com\/wp-content\/uploads\/2016\/03\/create_index-300x125.png 300w, https:\/\/www.sqldoubleg.com\/wp-content\/uploads\/2016\/03\/create_index-150x63.png 150w\" sizes=\"(max-width: 818px) 100vw, 818px\" \/><\/a><\/p>\n<p>You can see that all reads are logical, no physical, no read aheads, hence no disk is involved. About 13 seconds<\/p>\n<p>Now let&#8217;s run the query again and see what happens<\/p>\n<p><a href=\"https:\/\/sqldoubleg.live-website.com\/wp-content\/uploads\/2016\/03\/select_same_columns_as_index.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/sqldoubleg.live-website.com\/wp-content\/uploads\/2016\/03\/select_same_columns_as_index.png\" alt=\"select_same_columns_as_index\" width=\"929\" height=\"340\" class=\"aligncenter size-full wp-image-190\" srcset=\"https:\/\/www.sqldoubleg.com\/wp-content\/uploads\/2016\/03\/select_same_columns_as_index.png 929w, https:\/\/www.sqldoubleg.com\/wp-content\/uploads\/2016\/03\/select_same_columns_as_index-300x110.png 300w, https:\/\/www.sqldoubleg.com\/wp-content\/uploads\/2016\/03\/select_same_columns_as_index-150x55.png 150w\" sizes=\"(max-width: 929px) 100vw, 929px\" \/><\/a><\/p>\n<p>BOOM! My system is not so bad&#8230; i7 16GB RAM, SSD &#8230; true that I keep opening new tabs everywhere, but&#8230; It really did take longer, 13 secs for CREATE INDEX vs Crashing after 23 secs.<\/p>\n<p>He might be right&#8230; a query takes longer than index creation. Let&#8217;s try a little trick before giving up<\/p>\n<p><a href=\"https:\/\/sqldoubleg.live-website.com\/wp-content\/uploads\/2016\/03\/discard_results.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/sqldoubleg.live-website.com\/wp-content\/uploads\/2016\/03\/discard_results.png\" alt=\"discard_results\" width=\"758\" height=\"439\" class=\"aligncenter size-full wp-image-191\" srcset=\"https:\/\/www.sqldoubleg.com\/wp-content\/uploads\/2016\/03\/discard_results.png 758w, https:\/\/www.sqldoubleg.com\/wp-content\/uploads\/2016\/03\/discard_results-300x174.png 300w, https:\/\/www.sqldoubleg.com\/wp-content\/uploads\/2016\/03\/discard_results-150x87.png 150w\" sizes=\"(max-width: 758px) 100vw, 758px\" \/><\/a><\/p>\n<p>And run the query again in a new window (IMPORTANT!!)<\/p>\n<p><a href=\"https:\/\/sqldoubleg.live-website.com\/wp-content\/uploads\/2016\/03\/select_same_columns_as_index_no_results.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/sqldoubleg.live-website.com\/wp-content\/uploads\/2016\/03\/select_same_columns_as_index_no_results.png\" alt=\"select_same_columns_as_index_no_results\" width=\"775\" height=\"392\" class=\"aligncenter size-full wp-image-192\" srcset=\"https:\/\/www.sqldoubleg.com\/wp-content\/uploads\/2016\/03\/select_same_columns_as_index_no_results.png 775w, https:\/\/www.sqldoubleg.com\/wp-content\/uploads\/2016\/03\/select_same_columns_as_index_no_results-300x152.png 300w, https:\/\/www.sqldoubleg.com\/wp-content\/uploads\/2016\/03\/select_same_columns_as_index_no_results-150x76.png 150w\" sizes=\"(max-width: 775px) 100vw, 775px\" \/><\/a><\/p>\n<p>That&#8217;s more like it! Obviously there are no results as we requested, not even STATISTICS, but we can see that numbers (10 vs 13 secs) are pretty similar (~ish) if not lower than CREATE INDEX as it actually does less.<\/p>\n<p>The reason is very simple, SSMS is pretty SLOOOOOOW and processes rows 1 by 1 so it consumes a lot time before it finishes displaying them in the default grid. Since CREATE INDEX happens internally, we have the illusion that is faster, but it&#8217;s only faster displaying the results.<\/p>\n<p>This trick is very useful together with stopping \u00abDisplay Actual Execution Plan\u00bb (As Erik Darling points) to tune queries as probably SSMS will not be the final user application, so removing the time to display the results will give you a better idea of how long the query really takes.<\/p>\n<p>Hope you enjoyed reading and feel free to comment here or in the <a href=\"https:\/\/www.brentozar.com\/archive\/2016\/03\/indexes-helping-indexes\/\" target=\"_blank\">original post<\/a><br \/>\n&nbsp;<br \/>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Is it true that creating the index is quicker than running the query you needed it for? Let me show you the answer&nbsp; Tonight, Ladies and Gentlemen, we have one of the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[32,5,31],"_links":{"self":[{"href":"https:\/\/www.sqldoubleg.com\/es\/wp-json\/wp\/v2\/posts\/186"}],"collection":[{"href":"https:\/\/www.sqldoubleg.com\/es\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqldoubleg.com\/es\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqldoubleg.com\/es\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqldoubleg.com\/es\/wp-json\/wp\/v2\/comments?post=186"}],"version-history":[{"count":1,"href":"https:\/\/www.sqldoubleg.com\/es\/wp-json\/wp\/v2\/posts\/186\/revisions"}],"predecessor-version":[{"id":193,"href":"https:\/\/www.sqldoubleg.com\/es\/wp-json\/wp\/v2\/posts\/186\/revisions\/193"}],"wp:attachment":[{"href":"https:\/\/www.sqldoubleg.com\/es\/wp-json\/wp\/v2\/media?parent=186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqldoubleg.com\/es\/wp-json\/wp\/v2\/categories?post=186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqldoubleg.com\/es\/wp-json\/wp\/v2\/tags?post=186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}