Home » Script to get table sizes in SQL Server

Script to get table sizes in SQL Server

by Vlad Drumea
4 comments 4 minutes read

This is just a brief post containing a script to get table sizes in SQL Server, along with some other information like table type, number of records, partition count, compression type, number of nonclustered indexes and their total size.

Intro

I was messing around with with the StackOverflow database, and I needed a quick way of getting the following information for a whole databases:

  • Table names
  • The name of the clustered index (if it exists)
  • The type of table (clustered or heap)
  • Number of records
  • Table size in GB (this would be calculated based on the number of pages allocated or reserved to the table)
  • The space that’s actually used in GB (based on the pages currently in use by the table)
  • The type of compression being used
  • The number of partitions making up the table
  • The number of nonclustered indexes on the table
  • The total size in GB of the nonclustered indexes

I ran into a few gotchas along the way, for example, to get the necessary size information, I had to do an INNER JOIN between the sys.partitions system view and the sys.allocation_units one, which ended up messing with the record counts due to each partition having 3 related allocation units, one for each allocation unit type.

Here’s an example using the Posts table:


I solved this with some “creative” aggregation.

The second gotcha is the fact that the CTE I have initially used to get the nonclustered index data was pretty slow, but I sorted that out by using a temp table instead.

The script


You can also find this script in my SQL Server Scripts repo.

And this is how the result set looks like:


Conclusion

TL;DR: I made a script to get table sizes in sql server and I figured I’d share it here.

If you’re thinking that I could have used sys.dm_db_partition_stats instead of joining sys.partitions and sys.allocation_units – Yes, I could have, but I was already halfway done with this query before I realized.
I might add an updated version at a later time.

If you’ve found this post helpful, you might also like this post about getting database file size details.

Update: I’ve updated the script to prepend the schema name to the table name to make things easier for databases that have multiple schemas.

You may also like

4 comments

Sab August 25, 2023 - 08:53

Nice work , like it!

Reply
Vlad Drumea August 25, 2023 - 12:30

Thank you!

Reply
Romy Ugalino September 5, 2025 - 02:04

Very good work Vlad. This is by far one of the better table size scripts I’ve seen. It would be wonderful if able to send the results to a table to track growth.

Reply
Vlad Drumea September 8, 2025 - 21:22

Hi Romy,
Thank you for the feedback! You could achieve that with minimal changes to the script.
If I have some time this week, I’ll add that as an option.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.