SQL Server Tips

SQL Server table size: see every table's size

Updated December 5, 20269 min read

Get the size of all tables. Ploom measures a row of four storage crates of different sizes against a tall ruler.

How do you see the size of every table in SQL Server?

A DB on my test server grew, and I needed to know which tables took the space. SQL Server has no single command for SQL Server table size across every table, so I looked up how to check table size in SQL Server for all of them at once.

There are three ways:

  1. My favorite: one query for every table (fastest): use when I want all tables ranked, largest first.
  2. sp_spaceused: use when I need just one table.
  3. The SSMS report: use when I'd rather click than type.

Two more sections: few rows but a lot of data and index size that looks too high. I made test data so you can try these: jump to it. I ran this on SQL 2022.

SQL Server table size for every table

This is my favorite. It adds up the pages in sys.dm_db_partition_stats for each table and converts them to MB. A page is 8 KB.

T-SQL
SELECT s.name  AS schema_name,
       t.name  AS table_name,
       SUM(CASE WHEN ps.index_id IN (0,1) THEN ps.row_count ELSE 0 END) AS row_count,
       CAST(SUM(ps.reserved_page_count) * 8 / 1024.0 AS decimal(18,2)) AS reserved_mb,
       CAST(SUM(CASE WHEN ps.index_id IN (0,1)
                     THEN ps.in_row_data_page_count + ps.lob_used_page_count + ps.row_overflow_used_page_count
                     ELSE 0 END) * 8 / 1024.0 AS decimal(18,2)) AS data_mb,
       CAST((SUM(ps.used_page_count)
             - SUM(CASE WHEN ps.index_id IN (0,1)
                        THEN ps.in_row_data_page_count + ps.lob_used_page_count + ps.row_overflow_used_page_count
                        ELSE 0 END)) * 8 / 1024.0 AS decimal(18,2)) AS index_mb,
       CAST((SUM(ps.reserved_page_count) - SUM(ps.used_page_count)) * 8 / 1024.0 AS decimal(18,2)) AS unused_mb
FROM   sys.tables t
JOIN   sys.schemas s ON s.schema_id = t.schema_id
JOIN   sys.dm_db_partition_stats ps ON ps.object_id = t.object_id
GROUP BY s.name, t.name
ORDER BY reserved_mb DESC;
schema_nametable_namerow_countreserved_mbdata_mbindex_mbunused_mb
dboDocuments2000250.33250.13.02.19
dboOrders20000019.9011.527.85.52
dboOrderLog10000011.3811.33.01.05
dboLookup3.07.01.01.05

The Orders row matches the sp_spaceused result in the next section: 19.90 MB is 20,376 KB. reserved_mb is the size on disk, so sort by it. Add TOP (20) after SELECT if the DB has thousands of tables. Add WHERE s.name = N'dbo' for one schema.

You need VIEW DATABASE STATE and VIEW DEFINITION to run it. On SQL 2022 and later, Microsoft lists VIEW DATABASE PERFORMANCE STATE and VIEW SECURITY DEFINITION instead.

Size of one table

Pass the table name:

T-SQL
EXEC sp_spaceused N'dbo.Orders';
namerowsreserveddataindex_sizeunused
Orders20000020376 KB11800 KB8040 KB536 KB

Run it again for another table:

T-SQL
EXEC sp_spaceused N'dbo.Documents';
namerowsreserveddataindex_sizeunused
Documents2000256336 KB256128 KB16 KB192 KB

Sizes come back in KB. For Orders, reserved (20,376 KB) is data plus index plus unused: 11,800 + 8,040 + 536. Microsoft describes each column on its sp_spaceused page.

sp_spaceused splits the Orders table: 20,376 KB reserved is data, index and unused space.
Figure 1: Reserved size is data, indexes and unused space added together.

Skip the table name to get the whole DB:

T-SQL
EXEC sp_spaceused;
database_namedatabase_sizeunallocated space
t_sql_server_table_size656.00 MB54.20 MB
reserveddataindex_sizeunused
280368 KB269080 KB9504 KB1784 KB

It checks one table at a time and skips the schema name. For all tables, use the query above.

Use the SSMS disk usage report

SSMS has a built-in disk usage report for each DB.

It is a quick picture. I use the script to filter, sort or keep numbers.

Few rows, huge table

Rows and MB tell different stories. Orders has 200,000 rows and takes 19.90 MB. Documents has 2,000 rows and takes 250.33 MB. A row count alone misses this.

Orders has 200,000 rows and takes 19.90 MB, while Documents has 2,000 rows and takes 250.33 MB.
Figure 2: Documents has far fewer rows than Orders and is over ten times larger.

Documents has a big nvarchar(max) column. This splits its pages into in-row and LOB (large object) pages:

T-SQL
SELECT OBJECT_NAME(object_id) AS table_name,
       CAST(SUM(in_row_data_page_count) * 8 / 1024.0 AS decimal(18,2)) AS in_row_mb,
       CAST(SUM(lob_used_page_count)    * 8 / 1024.0 AS decimal(18,2)) AS lob_mb
FROM   sys.dm_db_partition_stats
WHERE  object_id = OBJECT_ID('dbo.Documents') AND index_id IN (0,1)
GROUP BY object_id;
table_namein_row_mblob_mb
Documents.12250.01

Almost all 250 MB is LOB data. A query that sums only in_row_data_page_count shows 0.12 MB. My first query adds LOB and row-overflow pages, so it counts them.

Index size looks too high

In the first query, heap or clustered index data counts as data_mb. Every other index counts as index_mb. To see each index, group by index:

T-SQL
SELECT OBJECT_NAME(ps.object_id) AS table_name,
       ISNULL(i.name, '(heap)')  AS index_name,
       i.type_desc,
       SUM(ps.row_count)         AS row_count,
       CAST(SUM(ps.reserved_page_count) * 8 / 1024.0 AS decimal(18,2)) AS reserved_mb
FROM   sys.dm_db_partition_stats ps
JOIN   sys.indexes i ON i.object_id = ps.object_id AND i.index_id = ps.index_id
WHERE  ps.object_id IN (OBJECT_ID('dbo.Orders'), OBJECT_ID('dbo.OrderLog'))
GROUP BY ps.object_id, i.name, i.type_desc
ORDER BY table_name, reserved_mb DESC;
table_nameindex_nametype_descrow_countreserved_mb
OrderLog(heap)HEAP10000011.38
OrdersPK__Orders__C3905BAF6A6527C7CLUSTERED20000011.76
OrdersIX_Orders_CustomerNONCLUSTERED2000004.45
OrdersIX_Orders_DateNONCLUSTERED2000003.70

OrderLog is a heap with no indexes, so all 11.38 MB is data. Orders has a clustered primary key and two nonclustered indexes. Those two take 8.15 MB, more than half the table's data. A note I left myself in 2015 says sp_spaceused gives all indexes combined. That is why I run this query for index size.

Gotcha: the numbers look wrong or stale

Check these before you trust the table size in SQL Server:

  1. Sort by reserved_mb, not data_mb. It ranks what each table takes on disk.
  2. Compare one table with sp_spaceused. They should agree, as Orders did.
  3. Run EXEC sp_spaceused @updateusage = N'TRUE'. Microsoft says it fixes stale usage info, can run long on large objects and needs db_owner.
  4. After you drop or truncate a large table, numbers may lag. Microsoft says the engine frees the pages later.
  5. row_count here is approximate, per Microsoft's partition stats page. Use SELECT COUNT(*) for an exact number.

Try it on my test data

Run this in a test DB to repeat my results. It loads 200,000, 100,000, 2,000 and 3 rows.

T-SQL
CREATE TABLE dbo.Orders (OrderID int IDENTITY(1,1) PRIMARY KEY, CustomerID int NOT NULL, OrderDate datetime2 NOT NULL, Status char(1) NOT NULL, Note varchar(100) NULL);
CREATE INDEX IX_Orders_Customer ON dbo.Orders (CustomerID) INCLUDE (OrderDate);
CREATE INDEX IX_Orders_Date ON dbo.Orders (OrderDate);
CREATE TABLE dbo.OrderLog (LogID int NOT NULL, Msg varchar(200) NOT NULL);
CREATE TABLE dbo.Documents (DocID int IDENTITY(1,1) PRIMARY KEY, Title nvarchar(100) NOT NULL, Body nvarchar(max) NOT NULL);
CREATE TABLE dbo.Lookup (Code int PRIMARY KEY, Label varchar(20) NOT NULL);
GO
;WITH n AS (SELECT TOP (200000) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS i FROM sys.all_objects a CROSS JOIN sys.all_objects b)
INSERT dbo.Orders (CustomerID, OrderDate, Status, Note)
SELECT i % 5000, DATEADD(minute, i, '2025-01-01'), 'N', REPLICATE('x', i % 60) FROM n;
;WITH n AS (SELECT TOP (100000) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS i FROM sys.all_objects a CROSS JOIN sys.all_objects b)
INSERT dbo.OrderLog (LogID, Msg) SELECT i, REPLICATE('y', 100) FROM n;
;WITH n AS (SELECT TOP (2000) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS i FROM sys.all_objects)
INSERT dbo.Documents (Title, Body) SELECT CONCAT(N'Doc ', i), REPLICATE(CONVERT(nvarchar(max), N'lorem ipsum '), 5000) FROM n;
INSERT dbo.Lookup VALUES (1,'New'),(2,'Paid'),(3,'Shipped');

Check Red9's SQL Server Services

SQL Server Consulting

Perfect for one-time projects like SQL migrations or upgrades, and short-term fixes such as performance issues or SQL remediation.

Discover More ➜

SQL Server Managed Services

Continuous SQL support, proactive monitoring, and expert DBA help with one predictable monthly fee.

Discover More ➜

Emergency SQL Support

Take the stress out of emergencies with immediate access to a SQL Server Sr. DBA 24x7x365

Discover More ➜

Explore All Services ➜