{"id":708,"date":"2004-07-26T05:14:19","date_gmt":"2004-07-26T05:14:19","guid":{"rendered":"\/blogs\/kimberly\/post\/MSDN-Webcast-QA-Index-Defrag-Best-Practices-Index-Usage-Questions.aspx"},"modified":"2013-01-02T07:13:24","modified_gmt":"2013-01-02T15:13:24","slug":"msdn-webcast-qa-index-defrag-best-practices-index-usage-questions","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/msdn-webcast-qa-index-defrag-best-practices-index-usage-questions\/","title":{"rendered":"MSDN Webcast Q&#038;A: Index Defrag Best Practices &#8211; Index Usage Questions"},"content":{"rendered":"<p><FONT face=Arial color=#000000 size=3><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt\">Before I launch into the Q&amp;A from this area there are a few other resources with Index Usage Q&amp;A that you should also check out:<\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt\"><SPAN style=\"mso-tab-count: 1\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/SPAN>Review the Q&amp;A from the June 11 Webcast <a href=\"https:\/\/www.sqlskills.com\/blogs\/kimberly\/permalink.aspx?guid=ecdcd11b-9eb7-4d88-b20c-b9225990080a\">here<\/a>.<\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt\"><SPAN style=\"mso-tab-count: 1\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/SPAN>Review the Q&amp;A page on SQLskills <a href=\"https:\/\/www.sqlskills.com\/consolidatedqa.asp\">here<\/a>.<\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt\"><?xml:namespace prefix = o ns = \"urn:schemas-microsoft-com:office:office\" \/><o:p>&nbsp;<\/o:p><\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt\"><B style=\"mso-bidi-font-weight: normal\">Q: How does SQL server decide what index to use when there are multiple indexes in at table?<o:p><\/o:p><\/B><\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt\">When SQL Server receives a request for data it goes through 5 primary steps: parsing, standardization, optimization, compilation and execution. The process of optimization (<I style=\"mso-bidi-font-style: normal\">while a bit simplified here<\/I>) includes multiple phases as well: query evaluation, index evaluation, join evaluation and so on. During index evaluation SQL Server determines whether or not USEFUL indexes exist to aid in processing your query. The usefulness of an index is based on the selectivity of the query and the columns requested in the query&#8230; There are many things for which SQL Server looks but the idea is that the better the information it has &#8211; the better the job of optimization. To ensure that the optimizer does a good job you should make sure to have auto update statistics and auto create statistics and you should consider using tools like Index Tuning Wizard to help you to create useful indexes. <\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt\"><o:p>&nbsp;<\/o:p><\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt\">While this is only touching the tip of the iceberg on these concepts last month&#8217;s webcast: <a href=\"https:\/\/msevents.microsoft.com\/cui\/Error.aspx?culture=en-US&amp;ErrorMsgID=ErrorMessage.EventDoesNotExist&amp;EventID=\">Indexing Best Practices<\/a> &#8211; will give you a lot more information. And &#8211; if you&#8217;re really interested&#8230; I offer a 5 day course in Performance Tuning and almost 2 full days cover almost nothing but indexes (nope, not kidding!). For details on the next SQL Immersion Event, click <A href=\"https:\/\/www.sqlskills.com\/sqlimmersion.asp\">here<\/A>. <\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt\"><o:p>&nbsp;<\/o:p><\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt\"><B style=\"mso-bidi-font-weight: normal\">Q: Why does the optimizer occasionally decide NOT to use an index(s)? In a few cases, we have had to force an index (e.g select * from a (index=index name) join b on a.id = b.id&#8230;<o:p><\/o:p><\/B><\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt\">Also, related to how the optimizer makes decisions on index usage&#8230; and a bit hard to answer with certainty BUT there are a few reasons why the optimizer might make an incorrect decision. However, before I get there &#8211; are you sure that using an index was really the &#8220;right&#8221; decision? There are often cases where a query is not selective enough to use an index and a table scan is actually a better choice&#8230; But if you find a case where your queries performance improves when you force an index and SQL Server is unable to make that same optimization choice then I&#8217;d check these things:<\/P><br \/>\n<UL style=\"MARGIN-TOP: 0in\" type=disc><br \/>\n<LI class=MsoNormal style=\"MARGIN: 0in 0in 0pt; mso-list: l0 level1 lfo1; tab-stops: list .5in\">Make sure that statistics are current and up to date. First, make sure the auto create and auto update statistics database-level options are turned ON. Also, you can use these commands to get some information about statistics: DBCC SHOW_STATISTICS or the STATS_DATE function. If the statistics are up to date then you might also want to check to see if they were based on a sampling v. a full scan. In the output of DBCC SHOW_STATISTICS you can see the number of rows as well as the number of rows sampled. If the sampling is low compared to the number of rows in the table then you might want to automate an UPDATE STATISTICS with FULLSCAN. However, this can cause blocking so you&#8217;ll want to schedule it off hours.<br \/>\n<LI class=MsoNormal style=\"MARGIN: 0in 0in 0pt; mso-list: l0 level1 lfo1; tab-stops: list .5in\">Maybe you don&#8217;t have the best index for the query&#8230; consider using Index Tuning Wizard on the query in Query Analyzer.<br \/>\n<LI class=MsoNormal style=\"MARGIN: 0in 0in 0pt; mso-list: l0 level1 lfo1; tab-stops: list .5in\">Maybe consider re-writing the query.. If it&#8217;s a join consider a subquery and visa versa.<br \/>\n<LI class=MsoNormal style=\"MARGIN: 0in 0in 0pt; mso-list: l0 level1 lfo1; tab-stops: list .5in\">If the command is in a stored procedure consider recompilation techniques. See <a href=\"https:\/\/www.sqlskills.com\/blogs\/kimberly\/permalink.aspx?guid=147db68e-3b54-4a4b-b092-7e98ddb13250\">this blog entry<\/a> for more details on stored procedure recompilation issues.<\/LI><\/UL><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt\"><SPAN style=\"FONT-SIZE: 12pt; FONT-FAMILY: Arial; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA\">Well, this certainly isn&#8217;t an exhaustive list but it should help!<\/SPAN><\/FONT><\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Before I launch into the Q&amp;A from this area there are a few other resources with Index Usage Q&amp;A that you should also check out: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Review the Q&amp;A from the June 11 Webcast here. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Review the Q&amp;A page on SQLskills here. &nbsp; Q: How does SQL server decide what index to use when [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27,36,58,78],"tags":[],"class_list":["post-708","post","type-post","status-publish","format-standard","hentry","category-events","category-indexes","category-resources","category-tips"],"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/posts\/708","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=708"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/posts\/708\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/media?parent=708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/categories?post=708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/tags?post=708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}