The symptom
While SQL Server compiles a query it notes any index that would have made the chosen plan cheaper. The note travels with the plan: Management Studio prints it in green above the diagram, the plan XML carries it as a MissingIndexes element, and the query plan viewer lists it as a finding with the CREATE INDEX statement written out.
The suggestion is a fact about one query, not a recommendation for the table. It is worth reading every time and worth building only some of the time.
Diagnose it
Read the suggestion in the plan
The examples use a shop schema with dbo.Customers (20,000 rows) and dbo.Orders (400,000 rows, 1% of them pending). Capture the actual plan of a query that filters on a column with no index:
SET STATISTICS XML ON;
SELECT c.Country, COUNT(*) AS Orders, SUM(o.Total) AS Total
FROM dbo.Orders AS o
JOIN dbo.Customers AS c ON c.CustomerId = o.CustomerId
WHERE o.Status = 'pending'
GROUP BY c.Country;
The plan XML contains the request:
<MissingIndexes>
<MissingIndexGroup Impact="73.5789">
<MissingIndex Database="[learnfx]" Schema="[dbo]" Table="[Orders]">
<ColumnGroup Usage="EQUALITY">
<Column Name="[Status]" ColumnId="3" />
</ColumnGroup>
<ColumnGroup Usage="INCLUDE">
<Column Name="[CustomerId]" ColumnId="2" />
<Column Name="[Total]" ColumnId="5" />
</ColumnGroup>
</MissingIndex>
</MissingIndexGroup>
</MissingIndexes>
Impact is the optimizer's estimate of how much of the query's cost the index would remove, as a percentage. The columns come in three groups. EQUALITY columns are compared with = and go first in the index key. INEQUALITY columns are compared with a range and go after them. INCLUDE columns are only read, so they belong in the INCLUDE list, not in the key.
A plan compiled as trivial carries no suggestion. A single-table query such as SELECT OrderId FROM dbo.Orders WHERE Status = 'pending' gets a trivial plan here and prints nothing, although the same index would help it.
Find the same request in the DMVs
Every suggestion is also recorded in the missing-index DMVs, with a count of how many times queries wanted that index. That count is what separates an index one report asked for once from an index the workload asks for all day.
SET STATISTICS XML OFF;
SELECT d.statement AS table_name,
d.equality_columns, d.inequality_columns, d.included_columns,
s.user_seeks, s.avg_user_impact
FROM sys.dm_db_missing_index_details AS d
JOIN sys.dm_db_missing_index_groups AS g ON g.index_handle = d.index_handle
JOIN sys.dm_db_missing_index_group_stats AS s ON s.group_handle = g.index_group_handle
WHERE d.database_id = DB_ID()
ORDER BY s.user_seeks * s.avg_total_user_cost * s.avg_user_impact DESC;
For the query above it returns one row: table [learnfx].[dbo].[Orders], equality columns [Status], included columns [CustomerId], [Total], one seek, impact 73.58.
das missing index suggestions snippet is a fuller version of this query. The DMVs are not a permanent record: creating any index on the table removes the table's rows, and the counting starts again.
Compare it with the indexes the table already has
The optimizer does not look at existing indexes when it writes the suggestion. If the table has an index on Status alone, the plan still asks for a new index on Status with included columns, when widening the existing one is the better change:
CREATE INDEX IX_Orders_Status ON dbo.Orders (Status);
GO
SET STATISTICS XML ON;
GO
SELECT c.Country, COUNT(*) AS Orders, SUM(o.Total) AS Total
FROM dbo.Orders AS o
JOIN dbo.Customers AS c ON c.CustomerId = o.CustomerId
WHERE o.Status = 'pending'
GROUP BY c.Country;
GO
SET STATISTICS XML OFF;
List what is there before you create anything. The index listing snippet prints every index on a table with its key and included columns.
SELECT i.name AS index_name, c.name AS column_name,
ic.key_ordinal, ic.is_included_column
FROM sys.indexes AS i
JOIN sys.index_columns AS ic ON ic.object_id = i.object_id AND ic.index_id = i.index_id
JOIN sys.columns AS c ON c.object_id = ic.object_id AND c.column_id = ic.column_id
WHERE i.object_id = OBJECT_ID('dbo.Orders')
ORDER BY i.index_id, ic.is_included_column, ic.key_ordinal;
Fix it
Widen an existing index when one is close
When an existing index has the same leading key columns, rebuild it with the extra columns instead of adding a second index. DROP_EXISTING replaces the index in one statement:
CREATE INDEX IX_Orders_Status ON dbo.Orders (Status)
INCLUDE (CustomerId, Total)
WITH (DROP_EXISTING = ON);
GO
SET STATISTICS XML ON;
GO
SELECT c.Country, COUNT(*) AS Orders, SUM(o.Total) AS Total
FROM dbo.Orders AS o
JOIN dbo.Customers AS c ON c.CustomerId = o.CustomerId
WHERE o.Status = 'pending'
GROUP BY c.Country;
GO
SET STATISTICS XML OFF;
The plan now seeks IX_Orders_Status for the 4,000 pending orders and the MissingIndexes element is gone.
Create the index when nothing is close
Build the key from the equality columns, then the inequality columns, and put the rest in INCLUDE. The suggestion lists the equality columns in table order, not in the best order for your workload: put first the column that other queries also filter on, so one index serves more than one query. Name the index yourself; the generated names in Management Studio say nothing about what the index is for.
Merge overlapping suggestions
A busy database produces many suggestions that differ only in their included columns. Building each one gives the table five indexes on the same key. Group the DMV rows by equality_columns und inequality_columns, build one index per group with the union of the included columns, and check the result against the duplicate index snippet.
When to ignore the suggestion
Ignore it when the query runs rarely, when user_seeks in the DMV is small, when the table takes heavy writes and already has many indexes, or when the suggestion includes most of the table's columns (that index is a second copy of the table). The impact number counts only the saving for this query. It does not count the cost of maintaining the index on every insert, update and delete.
SQLite: ask the shell for a suggestion
SQLite plans carry no index advice, but the sqlite3 command-line shell can produce it. .expert analyses the next statement and prints the index it would add and the plan that results, without creating anything:
.expert
SELECT order_id, total FROM orders WHERE customer_id = 4242;
CREATE INDEX orders_idx_38e08b36 ON orders(customer_id);
SEARCH orders USING INDEX orders_idx_38e08b36 (customer_id=?)
For PostgreSQL, MySQL and Oracle plans, which do not print suggestions, start from the scan itself: the full table scan page shows how to work out the index from the plan.
The checklist
Work through it in order. Each step gives you the evidence for the next one.
- Read the suggestion: equality columns, inequality columns, included columns and impact.
- Look up the same request in sys.dm_db_missing_index_group_stats and note user_seeks.
- List the table's existing indexes and look for one with the same leading key columns.
- Widen that index with DROP_EXISTING, or create a new one with equality columns first.
- Merge suggestions that share key columns into one index.
- Re-capture the plan and confirm the seek and the drop in logical reads.
- Check sys.dm_db_index_usage_stats after a week: an index with writes and no seeks should go.
Questions that come up
Why does my plan show no missing index although it scans a table?
The optimizer only records a suggestion during full optimization. Plans compiled as trivial, which includes many simple single-table queries, never carry one. Suggestions are also not produced for every plan shape, so the absence of a suggestion does not mean no index would help.
Does the column order in the suggestion matter?
The order of the groups matters: equality columns first, then inequality columns, with included columns outside the key. The order inside the equality group is the order of the columns in the table and carries no meaning. Choose it yourself, usually with the column most other queries filter on first.
Should I create every missing index SQL Server suggests?
No. Each suggestion is made for one query without regard to the indexes that exist or to write cost. Build the ones that many executions ask for, merge the ones that overlap, and prefer widening an existing index to adding a new one.
What does the Impact percentage mean?
It is the estimated percentage by which the cost of that one query would fall if the index existed. It is an estimate from the optimizer's cost model, not a measurement, and it says nothing about how often the query runs. Multiply it by user_seeks from the DMV to rank suggestions.
Do other databases print missing index suggestions?
Not in the plan. PostgreSQL, MySQL, Oracle and SQLite plans show the scan and leave the index to you. The sqlite3 shell has the .expert command, which proposes indexes for a statement.