Skip to content

Instantly share code, notes, and snippets.

@mikeblas
Created July 22, 2024 22:20
Show Gist options
  • Save mikeblas/aca98daf669127cfd2682f14e8cd52b3 to your computer and use it in GitHub Desktop.
Save mikeblas/aca98daf669127cfd2682f14e8cd52b3 to your computer and use it in GitHub Desktop.
Sizes of all indexes
-- get the row count and space used stats for each index
-- in this database
SELECT
s.Name as SchemaName, t.Name as TableName, i.name as IndexName,
p.Rows as RowCounts, SUM(AU.Total_pages) * 8.0/1024 AS TotalSpaceMB,
SUM(AU.used_pages) * 8.0/1024 AS UsedSpaceMB,
(SUM(AU.Total_Pages) - SUM(AU.used_pages)) * 8.0/1024 AS UnusedSpaceMB
FROM sys.tables AS T
JOIN sys.indexes AS I ON T.object_id = I.object_id
JOIN sys.partitions AS P on I.object_id = P.object_id AND I.index_id = P.index_ID
JOIN sys.allocation_units AS AU on P.partition_id = AU.container_id
LEFT OUTER JOIN sys.schemas AS S on T.Schema_ID = S.Schema_ID
WHERE T.is_ms_shipped = 0
GROUP BY T.Name, S.Name, I.Name, P.Rows
ORDER BY s.Name, t.Name;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment