it:ad:sql_server:howto:get_table_sizes

IT:AD:SQL Server:HowTo:Get Table Sizes

Summary

It's common that NFRs specify that the system must be able to handle future growth.

The cost of hardware is constantly decreasing and makes the question redundant, but sometimes there are sticklers for factual reports…

The following query will list all table sizes and rows… It can easily be extended to derive size per row, and from there the projected size…

SELECT 
t.NAME AS TableName,
i.name as indexName,
p.[Rows],
sum(a.total_pages) as TotalPages, 
sum(a.used_pages) as UsedPages, 
sum(a.data_pages) as DataPages,
(sum(a.total_pages) * 8) / 1024 as TotalSpaceMB, 
(sum(a.used_pages) * 8) / 1024 as UsedSpaceMB, 
(sum(a.data_pages) * 8) / 1024 as DataSpaceMB
FROM 
sys.tables t
INNER JOIN 
sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN 
sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN 
sys.allocation_units a ON p.partition_id = a.container_id
WHERE 
t.NAME NOT LIKE 'dt%' AND
i.OBJECT_ID > 255 AND 
i.index_id <= 1
GROUP BY 
t.NAME, i.object_id, i.index_id, i.name, p.[Rows]
ORDER BY 
object_name(i.object_id)

Alternatively, if you don't want to script things:

  • In SSMS
  • right click on database,
  • select Reports, Standard Reports, Disk Usage by Top Tables.
    • The report will give you number of rows and kilobytes used per table.
    • But doesn't give you the number of records per row.
  • /home/skysigal/public_html/data/pages/it/ad/sql_server/howto/get_table_sizes.txt
  • Last modified: 2023/11/04 01:57
  • by 127.0.0.1