Skip to content

Instantly share code, notes, and snippets.

@danielmackay
Created May 2, 2015 01:57
Show Gist options
  • Save danielmackay/b921fc2d6c125892c7d0 to your computer and use it in GitHub Desktop.
Save danielmackay/b921fc2d6c125892c7d0 to your computer and use it in GitHub Desktop.
Displays larges tables with their number of rows and sizes in MB/GB. #sql
-- Displays larges tables with their number of rows and sizes in MB/GB
CREATE TABLE #temp (
table_name sysname ,
row_count INT,
reserved_size VARCHAR(50),
data_size VARCHAR(50),
index_size VARCHAR(50),
unused_size VARCHAR(50))
SET NOCOUNT ON
INSERT #temp
EXEC sp_msforeachtable 'sp_spaceused ''?'''
SELECT a.table_name,
a.row_count,
COUNT(*) AS col_count,
a.data_size,
CAST(REPLACE(a.data_size, ' KB', '')/1024 AS INTEGER) AS 'MB',
CAST(REPLACE(a.data_size, ' KB', '')/(1024 * 1024) AS INTEGER) AS 'GB'
FROM #temp a
INNER JOIN information_schema.columns b
ON a.table_name collate database_default
= b.table_name collate database_default
GROUP BY a.table_name, a.row_count, a.data_size
ORDER BY CAST(REPLACE(a.data_size, ' KB', '') AS integer) desc
DROP TABLE #temp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment