Skip to content

Missing indexes the optimiser asked for

The engine's own suggestions, ranked by the improvement it expects. Treat the top few as candidates, not orders.

SQL Server · SQL
SELECT TOP 20       d.statement AS table_name,       d.equality_columns,       d.inequality_columns,       d.included_columns,       CAST(s.avg_total_user_cost * s.avg_user_impact * (s.user_seeks + s.user_scans) AS bigint) AS improvement_measure,       s.user_seeks + s.user_scans AS usesFROM sys.dm_db_missing_index_details dJOIN sys.dm_db_missing_index_groups g ON g.index_handle = d.index_handleJOIN sys.dm_db_missing_index_group_stats s ON s.group_handle = g.index_group_handleWHERE d.database_id = DB_ID()ORDER BY improvement_measure DESC

This query is part of the "SQL Server health" pack: connect the database in DataRunner and install the whole pack at once, with queries and dashboard.

What the query shows

While it compiles queries, the optimizer records indexes that would have made a plan cheaper. This template reads those records from sys.dm_db_missing_index_details, sys.dm_db_missing_index_groups and sys.dm_db_missing_index_group_stats, for the current database only, and returns the top 20 by estimated benefit.

Run it when queries are slow and you need a list of candidates, or after a release, to see whether new queries are scanning tables.

How to read each column

  • table_name: the table, as database, schema and table in brackets.
  • equality_columns: columns the queries compare with =. They go first in the index key.
  • inequality_columns: columns used with >, <, BETWEEN or <>. They go after the equality columns.
  • included_columns: columns the queries only return. They belong in INCLUDE, not in the key.
  • improvement_measure: avg_total_user_cost times avg_user_impact times (user_seeks + user_scans). It is a relative score for ranking, not milliseconds.
  • uses: seeks plus scans that the index could have served since the counters started.

Healthy and worrying results

A short list, or a list with low improvement_measure, is a good sign. Microsoft publishes no threshold. As a rule of thumb, a suggestion deserves attention when its score is far above the rest and uses is in the thousands. Suggestions with few uses are often one ad hoc query.

The same table appearing several times with overlapping columns means the optimizer asked for slightly different indexes, and one wider index can usually replace them.

What to do about it

Do not create every suggestion. Each index costs disk space and slows INSERT, UPDATE and DELETE. Before you create one, check the indexes that already exist on the table, because the DMV does not look at them and often suggests something an existing index almost covers.

Existing indexes on the table
EXEC sp_helpindex 'dbo.Orders';

Say the row shows equality_columns [CustomerId], inequality_columns [OrderDate] and included_columns [Status], [Total]. The key is equality first, then inequality. Included columns go in INCLUDE.

From suggestion to index
CREATE NONCLUSTERED INDEX IX_Orders_CustomerId_OrderDate
ON dbo.Orders (CustomerId, OrderDate)
INCLUDE (Status, Total);

When there are several equality columns, Microsoft advises ordering them from most to least selective. The DMV does not do that for you. After creating the index, run the slow query again and compare reads and time.

Limits to know

  • The data lives in memory and is cleared when the service restarts. Right after a restart the list is empty or misleading.
  • The DMV keeps at most 600 missing index groups.
  • It does not consider column order among equality columns, does not check existing indexes and can ask for near duplicates.
  • It never suggests clustered, filtered or columnstore indexes.
  • It needs VIEW SERVER STATE (VIEW SERVER PERFORMANCE STATE on SQL Server 2022 and later). Azure SQL Database filters the rows to your own database.

Run it on a schedule in DataRunner

Because the data is wiped by a restart, a weekly copy is useful. Schedule this query in DataRunner and receive the table as CSV, XLSX, PDF or HTML by e-mail, Slack, Telegram, Discord, WhatsApp (beta), a signed webhook, or in a Google Sheet. Each run stays in the run history of the schedule, so you can see which suggestions keep coming back.

Frequently asked questions

How do I find missing indexes in SQL Server?
Join sys.dm_db_missing_index_details, sys.dm_db_missing_index_groups and sys.dm_db_missing_index_group_stats, and sort by avg_total_user_cost * avg_user_impact * (user_seeks + user_scans). This template does exactly that for the current database and returns the top 20.
Should I create every missing index SQL Server suggests?
No. Suggestions ignore existing indexes and may overlap each other, and every index slows writes and uses space. Take the top few, compare them with the indexes you already have, create one at a time and measure.
Why is the missing index list empty?
The counters are cleared when the service restarts, and also when the table definition or its indexes change. A recently restarted server, or a workload that already has good indexes, gives an empty result.
In what order do the columns go in CREATE INDEX?
Equality columns first, then inequality columns, as the key. Included columns go in the INCLUDE clause. If there are several equality columns, put the most selective first.