Saturday, October 15, 2011

Database table indexes with type and size

The following query retrieves index names and shows type and size of indices.

SELECT
sysSchma.name AS SchemaName
, sysObj.name AS TableName
, sysIndx.name AS IndexName
, sysIndx.type_desc AS IndexType
, sysPartStat.used_page_count * 8 AS IndexSizeKB
, CAST(sysPartStat.used_page_count * 8 / 1024.00 AS Decimal(10,3))
AS IndexSizeMB
FROM sys.dm_db_partition_stats AS sysPartStat
INNER JOIN sys.indexes AS sysIndx
ON sysPartStat.[object_id] = sysIndx.[object_id]
AND sysPartStat.index_id = sysIndx.index_id
AND sysIndx.type_desc <> 'HEAP'
INNER JOIN sys.objects AS sysObj
ON sysObj.[object_id] = sysPartStat.[object_id]
INNER JOIN sys.schemas AS sysSchma
ON sysObj.[schema_id] = sysSchma.[schema_id]
AND sysSchma.name <> 'SYS'
-- WHERE partition_stats.[object_id] = object_id('dbo.TableName')
ORDER BY SchemaName, TableName, IndexName, IndexType

Share/Bookmark