In previous posts (here and here), I described the methods I use to monitor and maintain indexes. This is a next chapter – let’s try to find candidates for new indexes or hints for extending existing ones. Of course, there are few methods to achieve this goal:
- Find a candidate query and analyze it in Management Studio (applies to SQL Server 2008 or newer). This reduces to another problem – how to find a suitable query? You can use Profiler, but you have to spend some time working on filtering plus you can miss the query, if you’re not patient enough. You can also use reports in Management Studio – right click on server, select Reports –> Standard Reports and choose Peformance – Top Queries by Average IO and Top Queries by Total IO. Queries in Top Queries in Average IO are the best candidates for optimizing, especially if they are frequently used.
As for analysis – you can do it yourself manually or take advantage of new SSMS feature in 2008 – if you display execution plan, it will mention an index which could be helpful and projected benefit if such index is created.
- If you have a query or a bunch of them, you can provide them as input to Database Engine Tuning Advisor and get the recommendation.
- You can also check sys.dm_db_missing_index_details DMV to get information of all possible missing indexes in a database. This is actually the method used in SSMS (mentioned in step 1) – if it stumbles upon a scan or a seek which would benefit from additional index, it updates stats in the DMV and returns information to the user. If you want to find all indexes you can use following script:
select mid.statement as [Table name], mid.equality_columns, mid.inequality_columns, mid.included_columns, isnull(migs.unique_compiles, 0) as [Compile count], isnull(migs.user_seeks, 0) as [User seeks], isnull(migs.user_scans, 0) as [User scans], migs.avg_total_user_cost, migs.avg_user_impact from sys.dm_db_missing_index_details mid inner join sys.dm_db_missing_index_groups mig on mid.index_handle = mig.index_handle left join sys.dm_db_missing_index_group_stats migs on mig.index_group_handle = migs.group_handle
Compare it also with Books On-Line entries for sys.dm_db_missing_index_columns dynamic management function (there is a similar example to this one in this article) and sys.dm_db_missing_index_details dynamic management view for more information. And – as usual – please let me know if you have any comments or suggestions.