How to Find Size of the Index for Tables? – Interview Question of the Week #198

Question: How do you find the size of a table’s indexes?

Answer: Sum each index’s used_page_count in sys.dm_db_partition_stats. Each SQL Server page is 8 KB, so multiplying by 8 gives used space in KB.

Stacks of blank index cards are weighed on a brass balance

A client asked this during a Comprehensive Database Performance Health Check. I use this query because it puts the schema, table, index and size together:

SELECT SCHEMA_NAME(t.schema_id) AS [Schema Name],
       t.name AS [Table name], ix.name AS [Index name],
       SUM(ps.used_page_count) * 8 AS [Index size (KB)],
       SUM(ps.used_page_count) * 8 / 1024.0 AS [Index size (MB)]
FROM sys.dm_db_partition_stats AS ps
JOIN sys.indexes AS ix
  ON ps.object_id = ix.object_id AND ps.index_id = ix.index_id
JOIN sys.tables AS t ON t.object_id = ix.object_id
GROUP BY t.object_id, t.schema_id, t.name, ix.index_id, ix.name
ORDER BY SCHEMA_NAME(t.schema_id), t.name, ix.index_id;
Current WideWorldImporters first eight index rows with full names, used KB and MB
Current WideWorldImporters index sizes, first eight displayed rows. Full index names and both KB/MB fields are visible. These values are measurements from this lab database, not the original AdventureWorks instance.

The sum covers all partitions of an index. This is used allocated space, including in-row, LOB and row-overflow pages; it is not the number of bytes occupied only by key values. Use reserved_page_count instead if the question is reserved space.

A heap has index_id = 0 and a NULL index name, so it can appear too. The clustered index includes the table’s stored rows. Do not count it a second time as both table data and an independent copy. Specialized memory-optimized storage needs other diagnostics.

For SQL Server 2022 and later this DMV requires VIEW DATABASE PERFORMANCE STATE and VIEW SECURITY DEFINITION; earlier versions require VIEW DATABASE STATE and VIEW DEFINITION. If you use another helpful size query, share it and I will give credit.

Related interview questions:

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.


Discover more from SQL Authority with Pinal Dave

Subscribe to get the latest posts sent to your email.

SQL DMV, SQL Index, SQL Scripts, SQL Server
Previous Post
What is Read Ahead Read in SQL Server? – Interview Question of the Week #197
Next Post
Does Sort Order in Index Column Matters for Performance? – Interview Question of the Week #199

Related Posts

1 Comment. Leave new

  • Wilfred van Dijk
    November 5, 2018 1:22 pm

    although deprecated, sysindexes makes this query much easier:

    select object_schema_name(id) as SchemaName, object_name(id) as TableName, name as IndexName, dpages/128 as IndexSizeMB, rowcnt
    from sysindexes
    where indid in (1,2)
    and FirstIAM is not null
    and objectproperty(id, ‘IsUserTable’) = 1
    order by 1,2

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.