Skip to main content

Show suggestions for missing table indexes in SQL Server.

select db_name(mid.database_id) as [database],
    object_schema_name(mid.object_id) as [schema],
    object_name(mid.object_id) as [table],
    migs.avg_total_user_cost * migs.avg_user_impact / 100.0 * (migs.user_seeks + migs.user_scans) as improvement_measure,
    mid.equality_columns,
    mid.inequality_columns,
    mid.included_columns,
    migs.unique_compiles,
    migs.user_seeks,
    migs.user_scans,
    migs.avg_total_user_cost,
    migs.avg_user_impact,
    migs.system_seeks,
    migs.system_scans,
    migs.avg_total_system_cost,
    migs.avg_system_impact
from sys.dm_db_missing_index_groups as mig
inner join sys.dm_db_missing_index_group_stats as migs
    on migs.group_handle = mig.index_group_handle
inner join sys.dm_db_missing_index_details as mid
    on mig.index_handle = mid.index_handle
where migs.avg_total_user_cost * migs.avg_user_impact / 100.0 * (migs.user_seeks + migs.user_scans) > 10
order by migs.avg_total_user_cost * migs.avg_user_impact * (migs.user_seeks + migs.user_scans) desc;

--
-- Simple....

select object_name(fk.constraint_object_id) as constraint_name,
    object_name(fk.parent_object_id) as table_name,
    cl.name
from sys.foreign_key_columns as fk
inner join sys.columns as cl
    on fk.parent_object_id = cl.object_id
        and fk.parent_column_id = cl.column_id
left outer join sys.index_columns as c
    on fk.parent_object_id = c.object_id
        and fk.parent_column_id = c.column_id
where c.object_id is null
order by table_name, fk.constraint_column_id;