Skip to content

Instantly share code, notes, and snippets.

@MarshalOfficial
Created October 21, 2015 12:39
Show Gist options
  • Save MarshalOfficial/d658897d98da2e0bf624 to your computer and use it in GitHub Desktop.
Save MarshalOfficial/d658897d98da2e0bf624 to your computer and use it in GitHub Desktop.
this script list all unused index in database as output
select object_name (i.object_id) as NomTable,isnull( i.name,'HEAP') as IndexName
from sys.objects o inner join sys.indexes i
ON i.[object_id] = o.[object_id] left join
sys.dm_db_index_usage_stats s
on i.index_id = s.index_id and s.object_id = i.object_id
where object_name (o.object_id) is not null
and object_name (s.object_id)
is null
AND o.[type] = 'U'
and isnull( i.name,'HEAP') <>'HEAP'
union all
select object_name (i.object_id) as NomTable,isnull( i.name,'HEAP') as IndexName
from sys.objects o inner join sys.indexes i
ON i.[object_id] = o.[object_id] left join
sys.dm_db_index_usage_stats s
on i.index_id = s.index_id and s.object_id = i.object_id
where user_seeks= 0
and user_scans=0
and user_lookups= 0
AND o.[type] = 'U'
and isnull( i.name,'HEAP') <>'HEAP'
order by NomTable asc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment