How to Find Which Queries have Missing Index Requests in SQL Server
If you’ve spent any time performance tuning in SQL Server you’ll have come across missing index requests. These happen when the query optimizer is creating an execution plan and flags that an additional index on one of the underlying tables would help performance of this query moving forward.
To find these, you can use a tool like sp_BlitzIndex or you can use a query like this one to query the underlying DMVs directly;
SELECT
mid.statement AS table_name,
migs.avg_total_user_cost * migs.avg_user_impact * (migs.user_seeks + migs.user_scans) AS improvement_measure,
mid.equality_columns,
mid.inequality_columns,
mid.included_columns,
migs.user_seeks,
migs.user_scans,
migs.last_user_seek
FROM sys.dm_db_missing_index_details mid
JOIN sys.dm_db_missing_index_groups mig ON mid.index_handle = mig.index_handle
JOIN sys.dm_db_missing_index_group_stats migs ON mig.index_group_handle = migs.group_handle
ORDER BY improvement_measure DESC;
You’re going to have an output something like this (this has been anonymized, I hope you don’t have a database called Database2!);

Where you can see the table that is being identified as needing an index, the estimated ‘benefit’ of adding this index as well as the indexed columns and included columns being asked for.
The ‘benefit’ here is a calculation based upon the average ‘cost’ of the query (the subtree cost of the entire execution plan). The average estimated improvement on query performance that we would see by applying this index as well as the number of seeks and scans we’re doing against this table. It’s just a general rule of thumb to be able to sort and show you the most impactful potential indexes in your environment.
Who’s Asking?
The data above is very useful, however we often want to know ‘who’s asking’ for this new index so we can assess whether that query is something that we want to add an index to improve.
The output of sp_BlitzIndex does have a column to show an example execution plan. However, I have rarely seen that work as it relies upon the plan cache. Let’s write a query to pull the data if it exists.
Plan Cache
We’re going to have to write a couple of different queries here and join them together to get the results we need. This is not simple as there is not a way of directly joining all of these tables without parsing out some XML. This is also extremely inefficient when ran against an entire instance so it has been intentionally written to require a database, schema and table name to be able to see which queries are asking for indexes on a specific table.
DECLARE @DatabaseName sysname = N'DatabaseName';
DECLARE @SchemaName sysname = N'dbo';
DECLARE @TableName sysname = N'TableName';
;WITH XMLNAMESPACES (DEFAULT 'http://schemas.microsoft.com/sqlserver/2004/07/showplan'),
MissingIndexRequests AS (
SELECT
REPLACE(REPLACE(ISNULL(mid.equality_columns,''), '[', ''), ']', '') AS equality_columns,
REPLACE(REPLACE(ISNULL(mid.inequality_columns,''), '[', ''), ']', '') AS inequality_columns,
REPLACE(REPLACE(ISNULL(mid.included_columns,''), '[', ''), ']', '') AS included_columns,
migs.avg_total_user_cost * migs.avg_user_impact * (migs.user_seeks + migs.user_scans) AS improvement_measure,
migs.user_seeks,
migs.user_scans,
migs.last_user_seek
FROM sys.dm_db_missing_index_details mid
JOIN sys.dm_db_missing_index_groups mig ON mid.index_handle = mig.index_handle
JOIN sys.dm_db_missing_index_group_stats migs ON mig.index_group_handle = migs.group_handle
WHERE mid.database_id = DB_ID(@DatabaseName)
AND mid.object_id = OBJECT_ID(QUOTENAME(@DatabaseName) + N'.' + QUOTENAME(@SchemaName) + N'.' + QUOTENAME(@TableName))
),
CandidatePlans AS (
-- cheap pre-filter on dbid + raw text, before any XML is touched
SELECT DISTINCT cp.plan_handle
FROM sys.dm_exec_cached_plans cp
CROSS APPLY sys.dm_exec_sql_text(cp.plan_handle) st
WHERE st.dbid = DB_ID(@DatabaseName)
AND st.text LIKE '%' + @TableName + '%'
),
CachedPlanMissingIndexes AS (
SELECT
st.text AS query_text,
cp.usecounts,
REPLACE(REPLACE(ISNULL(STUFF((SELECT ',' + c.value('@Name','nvarchar(128)')
FROM mi.nodes('ColumnGroup[@Usage="EQUALITY"]/Column') AS eq(c)
FOR XML PATH('')), 1, 1, ''), ''), '[', ''), ']', '') AS equality_columns,
REPLACE(REPLACE(ISNULL(STUFF((SELECT ',' + c.value('@Name','nvarchar(128)')
FROM mi.nodes('ColumnGroup[@Usage="INEQUALITY"]/Column') AS ineq(c)
FOR XML PATH('')), 1, 1, ''), ''), '[', ''), ']', '') AS inequality_columns,
REPLACE(REPLACE(ISNULL(STUFF((SELECT ',' + c.value('@Name','nvarchar(128)')
FROM mi.nodes('ColumnGroup[@Usage="INCLUDE"]/Column') AS inc(c)
FOR XML PATH('')), 1, 1, ''), ''), '[', ''), ']', '') AS included_columns
FROM CandidatePlans cpl
JOIN sys.dm_exec_cached_plans cp ON cp.plan_handle = cpl.plan_handle
CROSS APPLY sys.dm_exec_query_plan(cp.plan_handle) qp
CROSS APPLY sys.dm_exec_sql_text(cp.plan_handle) st
CROSS APPLY qp.query_plan.nodes('//MissingIndexGroup/MissingIndex') AS t(mi)
WHERE REPLACE(REPLACE(mi.value('@Table', 'nvarchar(128)'), '[', ''), ']', '') = @TableName
)
SELECT
mir.equality_columns,
mir.inequality_columns,
mir.included_columns,
mir.improvement_measure,
mir.user_seeks,
mir.user_scans,
mir.last_user_seek,
cpmi.query_text,
cpmi.usecounts
FROM MissingIndexRequests mir
LEFT JOIN CachedPlanMissingIndexes cpmi
ON mir.equality_columns = cpmi.equality_columns
AND mir.inequality_columns = cpmi.inequality_columns
AND mir.included_columns = cpmi.included_columns
ORDER BY mir.improvement_measure DESC;
This is intentionally a LEFT JOIN in the final select so that we can be happy that we have data returned from the index request whether or not we have found a related plan in the cache.

(the above has had the actual table and column names removed)
You’ll see here that for my most beneficial query we can’t find a related query in the plan cache. This could be for a number of reasons however this is the situation we find ourselves in.
Query Store
We do have the option to utilise the Query Store here. This is a 2016 feature and needs to be enabled on our database in order for this method to be available, however we meet both of those criteria in this specific scenario so let’s have at it.
Quickie on the Query Store if you haven’t used it before. It’s an inbuilt feature that tracks and aggregates query performance over time, we can use the collected data the same as any other system DMV. The big advantage that Query Store has over the Plan Cache is that QS persists through things like restarts whereas the cache can be cleared by a number of processes (service restarts, dbcc freeproccache, natural recycling etc) so is much less reliable to store data long term.
It’s also worth noting that QS is database specific, the queries below are going to query only the database it’s run against. If you have cross database queries then you may have to run this against the DBs involved to find your full list.
Let’s start with a sample query, this is going to find the first 100 query plans we have in query store that include missing index requests. We’ll get the sql_text, the query_id, the plan_id as well as some more useful metrics here. The final 6 columns are a run down of the specific index that is being requested, which database and table as well as the definition of the index being requested.
;WITH XMLNAMESPACES (DEFAULT 'http://schemas.microsoft.com/sqlserver/2004/07/showplan')
SELECT TOP (100)
qsqt.query_sql_text,
OBJECT_NAME(qsq.object_id) AS containing_object,
qsq.query_id,
qsp.plan_id,
qsrs.count_executions,
qsrs.avg_duration,
qsrs.last_execution_time,
mi.value('@Database', 'nvarchar(128)') AS database_name,
mi.value('@Schema', 'nvarchar(128)') AS schema_name,
mi.value('@Table', 'nvarchar(128)') AS table_name,
STUFF((SELECT ',' + c.value('@Name','nvarchar(128)')
FROM mi.nodes('ColumnGroup[@Usage="EQUALITY"]/Column') AS eq(c)
FOR XML PATH('')), 1, 1, '') AS equality_columns,
STUFF((SELECT ',' + c.value('@Name','nvarchar(128)')
FROM mi.nodes('ColumnGroup[@Usage="INEQUALITY"]/Column') AS ineq(c)
FOR XML PATH('')), 1, 1, '') AS inequality_columns,
STUFF((SELECT ',' + c.value('@Name','nvarchar(128)')
FROM mi.nodes('ColumnGroup[@Usage="INCLUDE"]/Column') AS inc(c)
FOR XML PATH('')), 1, 1, '') AS included_columns
FROM sys.query_store_plan qsp
JOIN sys.query_store_query qsq ON qsp.query_id = qsq.query_id
JOIN sys.query_store_query_text qsqt ON qsq.query_text_id = qsqt.query_text_id
JOIN sys.query_store_runtime_stats qsrs ON qsrs.plan_id = qsp.plan_id
CROSS APPLY (SELECT TRY_CAST(qsp.query_plan AS XML) AS plan_xml) x
CROSS APPLY x.plan_xml.nodes('//MissingIndexGroup') AS t(mig)
CROSS APPLY mig.nodes('MissingIndex') AS t2(mi)
WHERE qsp.query_plan LIKE '%<MissingIndexes>%'
AND x.plan_xml IS NOT NULL
AND qsq.is_internal_query = 0
Output will look something like this:

The duplicates are caused by the same execution asking for the same index more than once, it likely hits the table multiple times, feel free to dig into the XML for the plan_id to check what’s being asked for.
Conclusion
Hopefully, at this point you’re able to find which tables are suggesting new indexes and you’re able to answer the question as to which queries are requesting those indexes (either via the Plan Cache or Query Store). I don’t have a master script I can give you to answer this question as it would take a prohibitive amount of time to execute, especially as you’re probably running these queries against your production environment as that’s where you’re trying to improve performance.
Did these work for you? Feel free to reach out with any questions or comments about your missing index requests. I’d love to hear them!




