SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

The Ultimate Missing Index Finder

By Jesse Roberge,

By Jesse Roberge - YeshuaAgapao@Yahoo.com
Update 1:
Fixed broken order by (data tyep conversion error) for @Sort=3, 4, and 5
Update 2:
Added a missing 'sys.' name-part in the join to sys.dm_db_missing_index_details. Was causing the proc to return no results on some peoples' systems.
Also updated the description with the permission requirements and the effect of running without sufficient permissions.
Update 3 2009-01-14:
Fixed Rows Output - Was being erroneously multiplied by (1+#NonClusteredIndexes)
SUM(Rows) Should be SUM(CASE WHEN index_id BETWEEN 0 AND 1 THEN row_count ELSE 0 END)
Added 'Last xxx' datetime columns to the result set
Added @Delimiter parameter for the column listing output (Defaults to ,) for accomodating export to csv files.

Reports stats on what the query optimizer records in the DMVs as missing indexes and what it says the cost savings will be if they were present. Can limit by table / schema name patterns.
Requires the 'VIEW SERVER STATE' permission. The sysadmin fixed server role has this permission. dbo (datase owner) gives 'VIEW DATABASE STATE', which is not sufficient. If you run this without sufficent permissions, you will get a empty result set.

Required Input Parameters

Optional Input Parameters
@SchemaName sysname='' Filters schemas. All schemas if blank. Accepts LIKE Wildcards.
@TableName sysname='' Filters tables. All tables if blank. Accepts LIKE Wildcards.
@Sort Tinyint=1 Determines what to sort the results by:
Value Sort Columns
1 Score DESC, user_seeks DESC, avg_total_user_cost DESC
2 Score ASC, user_seeks ASC, equality_columns ASC
3 SchemaName ASC, TableName ASC, SeekColumns ASC
4 SchemaName ASC, TableName ASC, Score DESC
5 SchemaName ASC, TableName ASC, Score ASC

EXECUTE Util_MissingIndexes 'dbo', 'visit%', 1

Licensed under the L-GPL - a weak copyleft license - you are permitted to use this as a component of a proprietary database and call this from proprietary software.
Copyleft lets you do anything you want except plagarize, conceal the source, proprietarize modifications, or prohibit copying & re-distribution of this script/proc.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
GNU Lesser General Public License for more details.

see <http://www.fsf.org/licensing/licenses/lgpl.html> for the license text.


Total article views: 19132 | Views in the last 30 days: 2
Related Articles

How to get schemaname, tablename, identity column, foreign key constraints

How to get schemaname, tablename, identity column, foreign key constraints


Column Level Permissions

GRANT Column Level Permissions


Update with column permission

UPDATE permissions required for unaffected columns


The Ultimate Column Searcher

Searches for columns with names containing @Colname that belong to tables with names containing @Tab...


Column Level Permissions

Did you know that you can grant permissions down to the column level in SQL Server?   Well, if you d...