Skip to content

Instantly share code, notes, and snippets.

@mika76
Created December 10, 2019 22:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mika76/bea74cc81b89c6af993fea16df97afbf to your computer and use it in GitHub Desktop.
Save mika76/bea74cc81b89c6af993fea16df97afbf to your computer and use it in GitHub Desktop.
List 10 largest tables in SQL Server - From https://dataedo.com/kb/query/sql-server/list-10-largest-tables
select top 10 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 tab
join sys.indexes ind
on tab.object_id = ind.object_id
join sys.partitions part
on ind.object_id = part.object_id and ind.index_id = part.index_id
join sys.allocation_units spc
on part.partition_id = spc.container_id
group by schema_name(tab.schema_id) + '.' + tab.name
order by sum(spc.used_pages) desc;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment