Skip to content

Instantly share code, notes, and snippets.

@petesql
Last active September 13, 2022 22:57
Show Gist options
  • Save petesql/d513e9c4ca22d7949c1360ac89187c1c to your computer and use it in GitHub Desktop.
Save petesql/d513e9c4ca22d7949c1360ac89187c1c to your computer and use it in GitHub Desktop.
-- Get sizes of all databases and store into a temp table.
SELECT
DB_NAME(database_id) AS [database_name],
CAST(SUM(CASE WHEN type_desc = 'ROWS' THEN size END) * 8. / 1024 AS DECIMAL(8,2)) AS [data_size_mb],
--CAST(SUM(CASE WHEN type_desc = 'ROWS' THEN size END) * 8. / 1024 / 1024 AS DECIMAL(8,2)) AS [data_size_gb],
CAST(SUM(CASE WHEN type_desc = 'LOG' THEN size END) * 8. / 1024 AS DECIMAL(8,2)) AS [log_size_mb],
--CAST(SUM(CASE WHEN type_desc = 'LOG' THEN size END) * 8. / 1024 / 1024 AS DECIMAL(8,2)) AS [log_size_gb],
CAST(SUM(size) * 8. / 1024 AS DECIMAL(8,2)) AS [total_size_mb]
--,CAST(SUM(size) * 8. / 1024 / 1024 AS DECIMAL(8,2)) AS [total_size_gb]
FROM sys.master_files WITH(NOWAIT)
GROUP BY database_id
ORDER BY data_size_mb DESC
@petesql
Copy link
Author

petesql commented Sep 13, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment