Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Raise your hand if you have seen code that sends email from within a trigger in SQL ServerTransaction Log: VLF's, Auto Growth and Performance »
    comments

    Prior to SQL Server 2005, determining index needs was a much more intense process.  The use of tracing and reviewing queries running on SQL Server would have to be performed.  In SQL Server 2005 and on, DMVs have been added which make the process more efficient and stable.  Determining index needs can be done with a query instead of a resource consuming process.  With these new abilities, another ugly practice arose out of SQL Server; applying all the indexes the DMVs said to apply.

    Missing Indexes

    Missing indexes can be found in detail by looking into the sys.dm_db_missing_index_details, sys.dm_db_missing_index_groups DMVs and sys.dm_db_missing_index_columns DMF.  These three objects are the base to retrieve the details to make observations on what SQL Server has determined is a missing index.  The example code below shows them in use.

    1. SELECT
    2.     *
    3. FROM sys.dm_db_missing_index_details AS details
    4. CROSS APPLY sys.dm_db_missing_index_columns (details.index_handle)
    5. INNER JOIN sys.dm_db_missing_index_groups AS groups ON groups.index_handle = details.index_handle

    SQL Server will populate these results without much (or any) consideration to the indexing that is already there.  This means that SQL Server will not take into account index problems that are referred to as overlapping or duplicate indexes.

    To show this lets perform an example in the AdventureWorks database.

    Using the Person.Address table in AdventureWorks, disable indexes IX_Address_AddressLine1_AddressLine2_City_StateProvinceID_PostalCode and IX_Address_StateProvinceID.

    Note: To enable an index after disabling it, rebuild the index. This will enable it again

    Run the following query while having “show actual execution plan” set on.

    1. SELECT
    2.     AddressID,
    3.     AddressLine1,
    4.     AddressLine2
    5. FROM Person.Address
    6. WHERE City = 'Seattle'

    In the actual execution plan we can see SSMS 2008 R2 shows a suggestion for a missing index

    With the query we wrote earlier using the DMV and DMFs, we can also see the results showing the need for an index on City

    Now run the following query on the Person.Address table.

    1. SELECT
    2.     AddressID,
    3.     AddressLine1,
    4.     AddressLine2
    5. FROM Person.Address
    6. WHERE City = 'Seattle'
    7.         AND AddressLine1 LIKE '11%'

    Reviewing the actual execution plan shows another index suggestion but now on City and AddressLine1.

    The missing index suggestion in green shown in the actual execution plan is very helpful.  However, the second image showing the missing index DMV/DMF results is misleading.  What the query is suggesting is to create an index on City and then another index on City and AddressLine1.  This is what is referred to as an overlapping index.

    Down to it with the Tip

    The missing index query using the DMVs and DMF we have shown is great but as discussed, may produce results that will require analysis on the reviewers’ part.  In the example covered, the best course would be to create one index covering both query’s requirements.  (While also placing in the covering aspects of the select statement.)

    1. CREATE INDEX IDX_CITY_ADDY1_ASC ON Person.Address (City,AddressLine1)
    2. INCLUDE (AddressLine2)
    3. WITH DROP_EXISTING  
    4. GO

    The results of both queries will now perform Index Seek operations off the IDX_CITY_ADDY1_ASC index. 

    Creating overlapping or duplicate indexes has a large negative impact on performance during INSERT, UPDATE and DELETE transactions.  During maintenance tasks such as fragmentation management, long runtimes due to unwanted indexes can also cause problems that should be avoided.  Optimizing any query is important.  Optimizing the use and creation of indexes themselves is just as important.

    Resources

    Jason Strate - Index Black Ops Series/Whitepaper and more

    Index DMV Usage Considerations

    About the Author

    Ted Krueger is a SQL Server MVP and has been working in development and database administration for 13+ years. Specialties range from High Availability and Disaster / Recovery setup and testing methods down to custom assembly development for SQL Server Reporting Services. Ted blogs and is also one of the founders of LessThanDot.com technology community. Some of the articles focused on are Backup / Recovery, Security, SSIS and working on SQL Server and using all of the SQL Server features available to create stable and scalable database services. @onpnt Personal Blog over at http://onpnt.wordpress.com/
    Social SitingsTwitterLinkedInLTD RSS Feed
    1312 views
    InstapaperVote on HN

    3 comments

    Comment from: Brad Schulz [Visitor] · http://bradsruminations.blogspot.com
    Brad Schulz Hi Ted...

    Great post as always.

    The "missing index" DMV's are great, but they don't tell you WHICH queries needed those indexes.

    I wrote a post not long ago that finds "missing index" queries in the cache, so you can actually see the specific queries and plans that need those indexes:

    Index Tuning Detective

    --Brad
    05/12/11 @ 14:50
    Comment from: Ted Krueger (onpnt) [Member]
    Ted Krueger (onpnt) Thanks, Brad
    05/12/11 @ 20:38
    Comment from: dineshvishe [Member]
    dineshvishe Hi ,
    SELECT
    *
    FROM sys.dm_db_missing_index_details
    show index_handle,equality_columns and included columns,stataement shown.
    can please tell above terms.
    11/13/12 @ 02:16

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)