SQL Scripts: Find Queries That Have Missing Index Requests

In a previous article, I went over how SQL Server will recommend an index that it believes will help a query run faster, and I showed how to find missing index recommendations. Now, as I said in that article, you do not want to just blindly add whatever index SQL Server is suggesting. It is very good at recognizing when an index is needed, but not so good recommending what that index should be. The best course of action is to determine which query needs the index, and then begin testing. And while SQL Server does make it easy to see these recommended indexes, finding out which queries actually generated the recommendations isn’t quite as straight forward.

In order to find the query that generated the missing index recommendation, we’re going to have to dig through some XML in the plan cache. That’s right, I said XML. And even though SQL Server does have built in XML capabilities, they can sometimes be cumbersome and difficult to understand.

Thankfully, there are some really smart people in the SQL Server world who have already figured this out for us! The code below is a modification of a script originally written by Jason Strate (and Jonathan Kehayias has an excellent script that I have used for this as well!) This script starts by querying the sys.dm_exec_cached_plans and sys.dm_exec_query_plan DMVs to get any available execution plans that have a MissingIndexes element. Once the execution plan is recovered, the script parses through the XML of the plan to retrieve the database, schema, and table names, as well as the columns recommended in the index, and the cost and impact associated with the recommended index. It also retrieves the execution plan so that you can open it up and see the exact t-sql query that requested the index.

NOTE: on the next to the last line of the script, I have a WHERE clause so you can specify the database name and table name to help you hone your query results in to find the specific index you are looking for. If you would rather pull back everything for the database, you just specify the database name, or you could even completely remove the WHERE clause to pull back all execution plans in cache with a MissingIndexes element (not recommended!).

ALSO NOTE: This query can take a while to run, especially on larger, busier servers. I recommend testing it out on a non-production server, because downloading free stuff off of the internet and running it straight in production is stupid.


(Visited 12,413 times, 1 visits today)

3 thoughts on “SQL Scripts: Find Queries That Have Missing Index Requests

  1. Pingback: SQL Scripts: How To Find Missing Indexes - SQL Nuggets

  2. Magnus

    Hi
    I’m using it with WHERE database = XXXX

    but how do I exclude offline databases from the first query?

    Msg 942, Level 14, State 4, Line 4
    Database ‘YYYY’ cannot be opened because it is offline.

  3. Robert Sievers

    I think you could just change the bottom of the script as follows:

    FROM MissingIndexes m
    INNER JOIN sys.databases d
    ON m.DatabaseName = d.name
    WHERE d.state = 0
    ORDER BY Cost * UseCounts DESC;

Comments are closed.