Objective
Get information on top X number of tables by total space occupied within current database.
Query
SELECT TOP 10 -- Replace 10 with desired number
SCHEMA_NAME([tab].[schema_id]) + '.' + [tab].[name] AS [table],
CAST(SUM([spc].[used_pages] * 8)/1024.00 AS NUMERIC(36, 2)) AS [used_mb],
CAST(SUM([spc].[total_pages] * 8)/1024.00 AS NUMERIC(36, 2)) AS [allocated_mb]
from
[sys].[tables] AS [tab]
INNER JOIN [sys].[indexes] AS [ind] ON [tab].[object_id] = [ind].[object_id]
INNER JOIN [sys].[partitions] AS [part] ON [ind].[object_id] = [part].[object_id] AND [ind].[index_id] = [part].[index_id]
INNER JOIN [sys].[allocation_units] AS [spc] ON [part].[partition_id] = [spc].[container_id]
GROUP BY
SCHEMA_NAME([tab].[schema_id]) + '.' + [tab].[name]
ORDER BY
SUM([spc].[used_pages]) DESC
Comments