Last active
May 17, 2024 08:05
-
-
Save ghotz/c7ff88cf37d328a080c5 to your computer and use it in GitHub Desktop.
Evaluate and standardize database size and growth
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
SELECT DB_NAME(F.database_id) AS database_name, F.[name] as logical_filename, type_desc | |
, CAST(CAST(size as bigint) * 8192 / 1024. /1024. AS decimal(15,2)) AS size_mb | |
, is_percent_growth | |
, CASE is_percent_growth WHEN 0 THEN CAST(CAST(growth as bigint) * 8192 / 1024. /1024. AS decimal(15,2)) END AS growth_mb | |
, CASE is_percent_growth WHEN 1 THEN growth END AS growth_percent | |
, 'ALTER DATABASE [' + DB_NAME(F.database_id) + '] MODIFY FILE (NAME = ''' + F.[name] + '''' | |
+ CASE | |
WHEN type_desc = 'LOG' AND (CAST(size as bigint) * 8192) < (256*1024*1024) THEN ', SIZE = 64MB' | |
ELSE '' | |
END | |
+ CASE | |
WHEN type_desc = 'ROWS' AND (CAST(growth as bigint) * 8192) < (128*1024*1024) THEN ', FILEGROWTH = 64MB' | |
WHEN type_desc = 'LOG' AND (CAST(growth as bigint) * 8192) < (256*1024*1024) THEN ', FILEGROWTH = 64MB' | |
ELSE NULL | |
END | |
+ ');' AS alter_command | |
FROM sys.master_files AS F | |
JOIN sys.databases AS D | |
ON F.database_id = D.database_id | |
WHERE DB_NAME(F.database_id) NOT IN ('master','model','msdb','tempdb') | |
AND D.state_desc = 'ONLINE' | |
ORDER BY D.[name], F.[type_desc] DESC, F.[name] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment