Technical Article

2005 unused indexes v3

,

create this view in a database, select * from index_usage.

reports #hits compared to #updates (as ratio)

statement to disable index also included.

Make sure you understand the index/database concepts before making descisions based on the output of this script !

NOTE: If you get strange errors when running this script, some stange characters might be added when pasting this script into the web

ALTER view [dbo].[vw_index_usage] as
select    object_name(a.object_id) "tablename"
        , c.name "indexname"
        , c.type_desc "indextype"
        , case c.is_unique 
                when 1 then 
                    case is_primary_key 
                        when 1 then 
                            'Primary Key' 
                        else
                            'Unique' 
                    end 
                else 
                    case c.is_unique_constraint 
                        when 1 then 
                            'Unique Constraint' 
                        else
                            'Performance'
                    end 
            end "IndexUsage"
        , (select used/128 from sysindexes b where b.name=c.name and c.index_id = b.indid and b.[id]=c.[object_id]) "MB"
        , (select count(*) from sys.index_columns d where a.object_id = d.object_id and a.index_id = d.index_id and d.is_included_column = 0) "cols"
        , (select count(*) from sys.index_columns d where a.object_id = d.object_id and a.index_id = d.index_id and d.is_included_column = 1) "included"
        , (a.user_seeks + a.user_scans + a.user_lookups) "hits"
        , (a.user_updates) "updates"
        , a.last_user_update "stats_date"
        , cast(a.user_seeks + a.user_scans + a.user_lookups AS REAL) / cast(case a.user_updates when 0 then 1 else a.user_updates end as REAL) * 100 "ratio"
        , 'alter index [' + c.name + '] on [' + object_name(a.object_id) + '] disable;' "SQLCmd"
from    sys.dm_db_index_usage_stats a
join sysobjects as o
on        (a.object_id = o.id)
join sys.indexes as c
on        (a.object_id = c.object_id and a.index_id = c.index_id)
where    o.type = 'U'                    -- exclude system tables
and        c.type <> 0                        -- exclude HEAPs
and        c.is_disabled = 0                -- only active indexes
and        a.database_id = DB_ID()            -- for current database only

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating