{"id":547,"date":"2009-03-08T17:29:00","date_gmt":"2009-03-08T17:29:00","guid":{"rendered":"\/blogs\/kimberly\/post\/Why-arent-those-nonclustered-indexes-being-used.aspx"},"modified":"2013-01-11T23:10:19","modified_gmt":"2013-01-12T07:10:19","slug":"why-arent-those-nonclustered-indexes-being-used","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/why-arent-those-nonclustered-indexes-being-used\/","title":{"rendered":"Why aren&#8217;t those nonclustered indexes being used?"},"content":{"rendered":"<p>\nAlong the same lines of improving database design and getting better performance on SQL Server (which [IMO] <strong>DOES<\/strong> take an experienced SQL Server database developer &#8211; but, we&#39;ll talk more about &quot;<a href=\"https:\/\/www.sqlskills.com\/blogs\/kimberly\/whose-job-is-it-anyway\/\" target=\"_blank\">whose job this really is<\/a>&quot; in many more posts and probably even a <a href=\"http:\/\/www.runasradio.com\/\" target=\"_blank\">RunAs<\/a> &#8211; which Richard and I just setup to record on Thursday (Mar 12)), I started thinking about how I could convince people of why they NEED a database developer. So, I thought I&#39;d ask this VERY important question&#8230;\n<\/p>\n<p>\nWhat percentage of data IS selective enough to use a nonclustered index which doesn&#39;t cover the query&#8230; in other words (just in case you&#39;re not entirely sure of what I mean :)), think of indexes in the back of a book&#8230; if you need to go to the back of the book to reference a bunch of data (this is called a [bookmark] lookup in SQL Server), there&#39;s a point where the randomness of the lookups (especially if you think in terms of many rows on a page) becomes too expensive. For example, imagine that the index is customer name and the data (the book) is customer orders &#8211; and, each page (of this rather weird book ;)), has 20 orders on it. Doing a query to lookup customer number&nbsp;12&#39;s orders might be really easy (if they only have only a few orders) BUT, what if the query is &quot;show me all of the orders for people that have an &#39;e&#39; in their name&quot;. First, the number of people have have an &#39;e&#39; in their name is probably better than 50% (that&#39;s TOTALLY a guess) and, if there&#39;s 20 orders per page then a lookup from the index into the book would require SQL Server to touch every page roughly 10 times. If the table has 50,000 pages (therefore 1 million rows &#8211; at 20 rows per page), then to find the 500,000 rows (remember, I&#39;m estimating half), SQL Server would have to do 500,000 bookmark lookups. For a table with only 50,000 pages that&#39;s terribly expensive.\n<\/p>\n<p>\nSo, here&#39;s the question &#8211; what&#39;s the tipping point? When is a nonclustered index on customer name NOT going to be used to lookup rows of sales orders? I&#39;m going to use a survey to see what you think and then within a week, I&#39;ll give the specific SQL Server math AND a query you can run within your own DBs to see EVERY one of your table&#39;s &quot;tipping points&quot;. It&#39;s really interesting and I think will really help you to understand why SQL Server might not be using those nonclustered indexes&#8230;&#8230;&#8230;&#8230;.\n<\/p>\n<p>\n<script>\nem24_survey_x = \"0951f66c-5487-4d2f-9a61-0adc5f6365e9\";\nem24_survey_width = \"255\";\nem24_survey_height = \"268\";\nem24_styles = \".em24_s {border:solid 1px #626A84; width:250px;} .em24_s td {font-size:12px;} .em24_q {background:#798BC6; color:#ffffff;} .em24_ai0, .em24_at0 {background:#E7F3FF; border-top:1px solid #B3C7D9;} .em24_ai1, .em24_at1 {background:#E3F7DE; border-top:1px solid #B3C7D9;} .em24_v {background:#798BC6}\";<\/script><br \/>\n<script src=\"http:\/\/my.surveypopups.com\/show\/si.js\" type=\"text\/javascript\">\n<\/script>\n<\/p>\n<p>\nCheers,<br \/>\nkt<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Along the same lines of improving database design and getting better performance on SQL Server (which [IMO] DOES take an experienced SQL Server database developer &#8211; but, we&#39;ll talk more about &quot;whose job this really is&quot; in many more posts and probably even a RunAs &#8211; which Richard and I just setup to record on [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36,48,65,66,73,77],"tags":[],"class_list":["post-547","post","type-post","status-publish","format-standard","hentry","category-indexes","category-nonclustered-indexes","category-sql-server-2005","category-sql-server-2008","category-survey","category-the-tipping-point"],"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/posts\/547","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/comments?post=547"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/posts\/547\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/media?parent=547"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/categories?post=547"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/tags?post=547"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}