Table Size Query

The following query lists the tables and the space they use. This query is much faster (sub-second) than a standard SELECT COUNT(*) query since it uses the dynamic management views in SQL Server rather than scanning your data.

SELECT sum ( used_page_count ) * 8 as SizeKB,
  sum(row_count) as [RowCount], object_name ( object_id ) AS TableName
FROM sys.dm_db_partition_stats
WHERE index_id=0 or index_id=1
GROUP BY object_id
ORDER BY sum ( used_page_count ) DESC;

About Clay Lenhart

I am a DBA/Technical Architect for Latitude Group and love technology.
This entry was posted in SQL Server Administration and tagged , . Bookmark the permalink.

2 Responses to Table Size Query

  1. Amar says:

    Thank you for providing an updated and highly flexible way of querying for table sizes. Most blogs still talk about sp_spaceused…..that’s so old school ;)

  2. Thanks for sharing this Clay, it’s so much more easy then making a stored procedure for this! :)

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>