Skip to content

Largest tables

Rows and reserved space per table, from partition stats, to see which SQL Server tables take up the most disk.

SQL Server · SQL
SELECT TOP 25       s.name + '.' + t.name AS table_name,       SUM(CASE WHEN p.index_id IN (0, 1) THEN p.row_count ELSE 0 END) AS rows_count,       SUM(p.reserved_page_count) * 8 / 1024 AS reserved_mb,       SUM(p.used_page_count) * 8 / 1024 AS used_mbFROM sys.dm_db_partition_stats pJOIN sys.tables t ON t.object_id = p.object_idJOIN sys.schemas s ON s.schema_id = t.schema_idGROUP BY s.name, t.nameORDER BY reserved_mb DESC

This query is part of the "SQL Server health" pack: connect the database in DataRunner and install the whole pack at once, with queries and dashboard.

What the query shows

This query reads sys.dm_db_partition_stats, joins it to sys.tables and sys.schemas, and returns the 25 tables that reserve the most space in the current database. Space includes the table data, all its indexes and large object pages.

Run it when the disk is filling up, when backups grow, when you plan a migration, or on a regular basis to see which tables drive the growth.

How to read each column

  • table_name: schema and table name.
  • rows_count: the approximate row count. It sums the heap or clustered index partitions only (index_id 0 and 1), so rows are not counted once per index.
  • reserved_mb: pages reserved by the table and all its indexes, times 8 KB, in MB. This is the size you pay for on disk.
  • used_mb: pages actually in use, in MB. The gap between reserved and used is allocated space that is still empty.

Both size columns use integer division, so values are rounded down to whole megabytes.

Healthy and worrying results

There is no right size, only expected sizes. Worry when a table is far bigger than the business would explain, for example a log or audit table with years of history nobody reads, or a staging table that was never emptied.

A large difference between reserved_mb and used_mb on a big table is worth a look. As a rule of thumb, more than 20 percent unused on a table of many gigabytes suggests fragmentation or deleted rows. Compare two readings a week apart to find the tables that grow fastest.

What to do about it

For a closer look at one table, split data and index size.

EXEC sp_spaceused 'dbo.AuditLog';

Common ways to reduce a large table are archiving old rows, deleting them in small batches to keep the log under control, removing indexes nobody reads (see the unused indexes template) and compressing.

Page compression on a table
ALTER TABLE dbo.AuditLog REBUILD WITH (DATA_COMPRESSION = PAGE);

Compression costs CPU, needs free space while it rebuilds and is not available in every edition of every version, so test on a copy. Avoid DBCC SHRINKFILE as a routine fix. It fragments indexes and the file grows again.

Limits to know

  • It looks at the database of the connection only. Run it per database.
  • row_count is approximate, so do not use it where you need an exact count.
  • The query covers tables from sys.tables. Views are not included, and neither are tables in other databases.
  • It needs VIEW DATABASE STATE on the database. In Azure SQL Database it runs on the database you connect to.
  • Unlike most DMVs in this pack, the numbers come from current allocation, so a restart does not reset them.

Run it on a schedule in DataRunner

Run this query once a week in DataRunner and send it by e-mail, Slack, Telegram, Discord, WhatsApp (beta), a signed webhook or Google Sheets, in CSV, XLSX, PDF or HTML. A weekly series shows which table grows and gives you time to act before the disk is full.

Frequently asked questions

How do I find the largest tables in a SQL Server database?
Sum reserved_page_count from sys.dm_db_partition_stats by table, multiply by 8 to get KB, and sort descending. This template does it and also shows the approximate row count and the used space.
What is the difference between reserved and used space?
Reserved is the space allocated to the table and its indexes. Used is the part holding data. The difference is allocated but empty space, which stays with the table until it is reused or the table is rebuilt.
Is the row count exact?
No. sys.dm_db_partition_stats reports an approximate row count. For an exact number run SELECT COUNT(*) on the table, which can be slow on a big one.
Does this size include indexes?
Yes. reserved_mb and used_mb add the pages of the table data and of every index on it. Use sp_spaceused on one table to see data and index size apart.