在 AdventureWorks 範例資料庫中執行以下查詢,即可列出每張表的資料列數與空間佔用(KB):
USE ADVENTUREWORKS; GO
SELECT
TableName = obj.name,
TotalRows = prt.rows,
[SpaceUsed(KB)] = SUM(alloc.used_pages) * 8
FROM sys.objects obj
JOIN sys.indexes idx ON obj.object_id = idx.object_id
JOIN sys.partitions prt ON obj.object_id = prt.object_id
JOIN sys.allocation_units alloc ON alloc.container_id = prt.partition_id
WHERE obj.type = 'U' -- 僅使用者表
AND idx.index_id IN (0, 1) -- 堆或叢集索引
GROUP BY obj.name, prt.rows
ORDER BY TableName;

sys.partitions.rows 記錄的是資料列層級的筆數(堆或叢集索引各一列),故以 idx.index_id IN (0,1) 過濾避免重複計算。sys.allocation_units.used_pages 為已使用頁數,每頁 8 KB,乘 8 即得 KB 數。若需包含索引大小,可移除 idx.index_id IN (0,1) 條件並調整分組鍵;若只想看前 N 大表,加上 ORDER BY [SpaceUsed(KB)] DESC 配合 TOP 即可。