Welcome to Aussie SQL Server Bloggers Sign in | Join | Help

usp_helpindexusagestats: a helpfull index analysis script

Here's an index analysis script I put together recently which combines output from sp_helpindex (also with included columns, which the regular sp_helpindex lacks) and the dm_db_index_usage_stats() DMV.

I use this script primarily to identify which indexes are sub-sets of each other & can therefore be consolidated into a single index with the added information about usage patterns so I also have an understanding of how often the indexes are being accessed by queries.

create procedure usp_helpindexusagestats
  @tablename varchar(1000)
as

set nocount on

if object_id('tempdb..#helpindex') > 0 drop table #helpindex

create table #helpindex (
   index_name varchar (1000) not null primary key
 , index_description varchar (1000) null
 , index_keys varchar (1000) null
)

insert #helpindex
exec sp_helpindex @tablename

alter table #helpindex add inccols varchar(1000) null

declare cr cursor for
select si.name, sc.name
from sysobjects so
join sysindexes si on so.id = si.id
join sys.index_columns ic on si.id = ic.object_id and si.indid = ic.index_id
join sys.columns sc on ic.object_id = sc.object_id and ic.column_id = sc.column_id
where so.xtype = 'U'
  and so.name = @tablename
  and ic.is_included_column = 1
order by si.name, ic.index_column_id

declare @siname varchar(1000), @scname varchar(1000)

open cr

fetch next from cr into @siname, @scname

while @@fetch_status = 0
 begin

  update #helpindex set inccols = isnull(inccols , '') + @scname + ', ' where index_name = @siname

  fetch next from cr into @siname, @scname
 end

update #helpindex set inccols = left(inccols, datalength(inccols) - 2)
where right(inccols, 2) = ', '

close cr
deallocate cr

select hi.index_name
     , hi.index_description
     , hi.index_keys
     , hi.inccols as included_columns
     , ius.index_id
     , user_seeks
     , user_scans
     , user_lookups
     , user_updates
     , last_user_seek
     , last_user_scan
     , last_user_lookup
from  #helpindex hi
join  sysindexes si on si.name = hi.index_name collate database_default
join sysobjects so on si.id = so.id
left join sys.dm_db_index_usage_stats ius on ius.object_id = si.id and ius.index_id = si.indid and ius.database_id = db_id()
where so.name = @tablename

drop table #helpindex
go

This usp_ should first be created within the user database & can then be called simply as follows:

exec usp_helpindexusagestats 'MyTableName'

Output includes some rich information about the indexes on the table including column names (keys & included columns) and also usage stats information about how often seeks, scans & lookups are being performed on each index.

Published Saturday, 23 February 2008 8:32 AM by Greg_Linwood

Comments

# re: usp_helpindexusagestats: a helpfull index analysis script

Hi Greg

Just tried running this script like so:

sp_msforeachtable "exec usp_helpindexusagestats '?'"

but get empty result sets back for each table. I had a quick look to see if I could figure it out, but nothing springs to mind?

Cheers, Mitch
Monday, 14 April 2008 5:40 PM by MitchW

# re: usp_helpindexusagestats: a helpfull index analysis script

Thanks for pointing this out Mitch. I've posted an update to the SP which should solve this.. Cheers, Greg
Friday, 18 April 2008 1:16 AM by Greg_Linwood

# re: usp_helpindexusagestats: a helpfull index analysis script

Just wanted to say thx for this Greg!

I implemented it in my company and clawed back 3GB of duplicate indexes.

The next step is to identify overlapping indexes, then move onto indexes that aren't used at all (I am collecting stats each week).

jag

Saturday, 14 February 2009 1:57 AM by jst1699

# How earn $ 1000 a week?

You can earn from 3000$-5000$ a Month with this program!
You will make money from from multiple streams: people search engine, ClickBank, HD Publishing, Google AdSense, HostGator, My Life and GDI (Global Domain International).
Visit site: [url=http://bit.ly/aLNyID]acme-people-search.com[/url]
Saturday, 6 March 2010 3:40 AM by AleVaserman
Anonymous comments are disabled