Today there isn’t a fine-tuned way to clear the accumulated statistics from the “missing index” set of DMVs. Certainly restarting SQL Server will do this, but you don’t have something like DBCC SQLPERF (‘sys.dm_os_wait_stats’, CLEAR) to reset statistics for dm_db_missing_index_columns, dm_db_missing_index_groups, dm_db_missing_index_group_stats and dm_db_missing_index_details.
While we don’t have this direct option, there is an interesting behavior that I’ll demonstrate on this blog post using SQL Server 2008 R2 (10.50.1617).
Right after restarting SQL Server, I run the following query to generate a few entries in the missing index DMVs (each query spawns a missing index recommendation):
WHERE UnitPriceDiscount = 0.40
WHERE LineTotal = 236.421500
WHERE TaxAmt = '10.316'
Next I execute the following query to see the three entries in the missing index DMVs:
FROM sys.dm_db_missing_index_group_stats AS s
INNER JOIN sys.dm_db_missing_index_groups AS g
ON (s.group_handle = g.index_group_handle)
INNER JOIN sys.dm_db_missing_index_details AS d
ON (g.index_handle = d.index_handle)
So I have three index suggestions so far. What happens if I create one of suggested indexes for the SalesOrderDetail table?
CREATE NONCLUSTERED INDEX IDX_SalesOrderDetail_UnitPriceDiscount
ON [Sales].[SalesOrderDetail] ([UnitPriceDiscount])
If I re-execute the query against the missing index DMVs – I’ll now see that BOTH suggestions from the SalesOrderDetail table got cleared out (even though I only added one of the suggestions), leaving behind the suggestion for the other table (SalesOrderHeader):
So by virtue of adding an index to SalesOrderDetail, it clears out BOTH suggestions for that table.
And if I re-execute the query for the LineTotal column on SalesOrderDetail (that was not yet indexed), the missing index entry pops back in (since we didn’t create an index for this):
And just to test from another direction, I'll add the suggested index on the SalesOrderHeader table:
CREATE NONCLUSTERED INDEX IDX_SalesOrderHeader_TaxAmt
ON [Sales].[SalesOrderHeader] ([TaxAmt])
Re-running the query against the missing index DMVs, sure enough, the SalesOrderHeader suggestion is removed, leaving the SalesOrderDetail suggestion (for the one index we haven’t yet created):
So in a nutshell, the missing index views aren’t as static as they may appear to be and if you’re adding indexes, you may expect to see suggestions disappear for that table (not waiting for a SQL Server restart) – and this happens whether or not you created indexes for each suggestion for a specific table. I can understand the trade-offs of this behavior – especially since a new index on a table can have an impact on future execution plans. Basically one index creation “resets” the suggestions. But that also means that you should be sampling these DMVs over time and not rely fully on a single sampling.