Created
May 2, 2015 01:57
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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