How to list top tables (disk usage) - SQL
SQL - Way:
-- Use sys.tables, sys.indexes, sys.partitions, sys.allocation_units and sys.schemas to display the line number, and also the size of tables
select sch.name as SchemaName,
tab.name as TableName,
par.rows as RowCounts,
sum(alc.total_pages) * 8 as TotalSpace,
sum(alc.used_pages) * 8 as UsedSpace,
(sum(alc.total_pages) - sum(alc.used_pages)) * 8 as UnusedSpace
FROM sys.tables tab
INNER JOIN sys.indexes ind
ON tab.object_id = ind.object_id
INNER JOIN sys.partitions par
ON ind.object_id = par.object_id
and ind.index_id = par.index_id
INNER JOIN sys.allocation_units alc
ON par.partition_id = alc.container_id
LEFT OUTER JOIN sys.schemas sch
ON tab.schema_id = sch.schema_id
GROUP BY tab.name, sch.name, par.rows
ORDER BY UsedSpace desc
Result:
Reference:
https://expert-only.net/sql-server/display-list-all-tables-space-sql-server-database/
http://sql.lernenhoch2.de/lernen/sql-anfanger/select-daten-selektieren/order-by-sortieren/