{"id":15003210,"url":"https://github.com/madeiradata/databasefilesizemaintenance","last_synced_at":"2025-10-30T09:31:33.009Z","repository":{"id":42573283,"uuid":"249897604","full_name":"MadeiraData/DatabaseFileSizeMaintenance","owner":"MadeiraData","description":"Add-on to Ola Hallengren's maintenance solution, focusing on managing database file size growth and shrink.","archived":false,"fork":false,"pushed_at":"2022-03-31T09:37:36.000Z","size":226,"stargazers_count":6,"open_issues_count":4,"forks_count":4,"subscribers_count":3,"default_branch":"develop","last_synced_at":"2024-10-12T07:22:49.755Z","etag":null,"topics":["autogrow","maintenance","managing-database","microsoft-sql-server","ms-sql-server","mssql","shrink","size-growth","sql-server","sqlserver","t-sql"],"latest_commit_sha":null,"homepage":"https://madeiradata.github.io/DatabaseFileSizeMaintenance/","language":"TSQL","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MadeiraData.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-03-25T05:49:02.000Z","updated_at":"2024-07-06T19:57:28.000Z","dependencies_parsed_at":"2022-09-08T01:03:27.064Z","dependency_job_id":null,"html_url":"https://github.com/MadeiraData/DatabaseFileSizeMaintenance","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MadeiraData%2FDatabaseFileSizeMaintenance","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MadeiraData%2FDatabaseFileSizeMaintenance/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MadeiraData%2FDatabaseFileSizeMaintenance/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MadeiraData%2FDatabaseFileSizeMaintenance/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MadeiraData","download_url":"https://codeload.github.com/MadeiraData/DatabaseFileSizeMaintenance/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219856479,"owners_count":16556082,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["autogrow","maintenance","managing-database","microsoft-sql-server","ms-sql-server","mssql","shrink","size-growth","sql-server","sqlserver","t-sql"],"created_at":"2024-09-24T18:57:06.404Z","updated_at":"2025-10-30T09:31:32.480Z","avatar_url":"https://github.com/MadeiraData.png","language":"TSQL","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Database File Size Maintenance\n\n**APPLIES TO:** ![Yes](media/yes-icon.png)SQL Server ![Yes](media/yes-icon.png)Azure SQL Database (Managed Instance only) ![No](media/no-icon.png)Azure Synapse Analytics (SQL DW) ![No](media/no-icon.png)Parallel Data Warehouse \n\n-------\n\nThis project is for development of a stored procedure, which is to be used as an add-on to [Ola Hallengren's maintenance solution](https://ola.hallengren.com), and retains the same standards and conventions in its implementation of parameters.\n\nThe `DatabaseFileSizeMaintenance` procedure focuses on managing database file size growth and shrink, based on specified parameters.\n\n## Download\n\nDownload [DatabaseFileSizeMaintenance.sql](DatabaseFileSizeMaintenance.sql).\n\n## Adding to MaintenanceSolution.sql\n\nIn order to create a corresponding maintenance job as part of Ola Hallengren's `MaintenanceSolution.sql` script:\n\n- Find the first line that starts with `INSERT INTO @Jobs`, and add the following before that line:\n\n```\n  INSERT INTO @Jobs ([Name], CommandTSQL, DatabaseName, OutputFileNamePart01)\n  SELECT 'DatabaseFileSizeMaintenance - USER_DATABASES',\n         'EXECUTE [dbo].[DatabaseFileSizeMaintenance]' + CHAR(13) + CHAR(10) + '@Databases = ''USER_DATABASES'',' + CHAR(13) + CHAR(10) + '@LogToTable = ''' + @LogToTable + '''',\n         @DatabaseName,\n         'DatabaseFileSizeMaintenance'\n```\n\n## Syntax\n\nFor syntax conventions info, please see [Transact-SQL Syntax Conventions](https://docs.microsoft.com/en-us/sql/t-sql/language-elements/transact-sql-syntax-conventions-transact-sql).\n\n```\nEXEC dbo.DatabaseFileSizeMaintenance\n   @Databases\t\t\t\t= N'[ databases | ALL_DATABASES | SYSTEM_DATABASES | USER_DATABASES | AVAILABILITY_GROUP_DATABASES [ ,...n ] ]'\n[ ,@UsedSpacePercentHighThreshold\t= used_space_percent_high_threshold ]\n[ ,@UsedSpacePercentLowThreshold\t= used_space_percent_low_threshold ]\n[ ,@MinFileSizeToShrinkMB\t\t= minimum_file_size_to_shrink_mb ]\n[ ,@MinDatabaseAgeInDays\t\t= minimum_database_age_in_days ]\n[ ,@TargetShrinkSizePercent\t\t= target_shrink_size_percent ]\n[ ,@MinTargetShrinkSizeMB\t\t= minimum_target_shrink_size_mb ]\n[ ,@ShrinkAllowReorganize\t\t= { 'N' | 'Y' } ]\n[ ,@DatabaseOrder\t\t\t= { NULL | 'DATABASE_SIZE_ASC' | 'DATABASE_SIZE_DESC' | 'LOG_SIZE_SINCE_LAST_LOG_BACKUP_ASC' | 'LOG_SIZE_SINCE_LAST_LOG_BACKUP_DESC' | 'DATABASE_NAME_ASC' | 'DATABASE_NAME_DESC' } ]\n[ ,@DatabasesInParallel\t\t\t= { 'N' | 'Y' } ]\n[ ,@LogToTable\t\t\t\t= { 'N' | 'Y' } ]\n[ ,@Execute\t\t\t\t= { 'N' | 'Y' } ]\n[ ,@FileTypes\t\t\t\t= { 'ALL' | 'ROWS' | 'LOG' } ]\n```\n\n## Arguments\n\n### `@Databases`\n\nSelect databases.\n\nThe keywords `SYSTEM_DATABASES`, `USER_DATABASES`, `ALL_DATABASES`, and `AVAILABILITY_GROUP_DATABASES` are supported.\n\nThe hyphen character (`-`) is used to exclude databases, and the percent character (`%`) is used for wildcard selection.\n\nAll of these operations can be combined by using the comma (`,`).\n\nValue|Description\n---|---\nSYSTEM_DATABASES|All system databases (master, msdb, and model)\nUSER_DATABASES|All user databases\nALL_DATABASES|All databases\nAVAILABILITY_GROUP_DATABASES|All databases in availability groups\nUSER_DATABASES, -AVAILABILITY_GROUP_DATABASES|All user databases that are not in availability groups\nDb1|The database Db1\nDb1, Db2|The databases Db1 and Db2\nUSER_DATABASES, -Db1|All user databases, except Db1\n%Db%|All databases that have “Db” in the name\n%Db%, -Db1|All databases that have “Db” in the name, except Db1\nALL_DATABASES, -%Db%|All databases that do not have “Db” in the name\n\n### `@UsedSpacePercentHighThreshold`\n\nIf used space in a file is higher than this, then will grow the file.\n\nValue is in percent (0..100)\n\nDefault is `90` percent.\n\n### `@UsedSpacePercentLowThreshold`\n\nIf used space in a file is smaller than this, then will shrink the file.\n\nValue is in percent (0..100)\n\nDefault is `10` percent.\n\n### `@MinFileSizeToShrinkMB`\n\nIf file size is smaller than this, then will NOT shrink.\n\nValue is in MB.\n\nDefault is `50000` MB (i.e. 50 GB).\n\n### `@MinDatabaseAgeInDays`\n\nDatabases must be at least this old in order to be checked.\n\nValue is in days.\n\nDefault is `30` days.\n\n### `@TargetShrinkSizePercent`\n\nWhen shrinking, try to shrink down to this percentage of the current file size.\n\nValue is in percent (0..100).\n\nDefault is `33` percent.\n\n### `@MinTargetShrinkSizeMB`\n\nWhen shrinking files, prevent shrinking to a size smaller than this.\n\nValue is in MB.\n\nDefault is `64` MB.\n\n### `@ShrinkAllowReorganize`\n\nSet whether to allow reorganizing pages during shrink.\n\nValue|Description\n---|---\nY|Allow reorganizing pages during shrink. This is the **default**.\nN|Do not allow reorganizing pages during shrink.\n\n**NOTE:** Reorganizing pages during shrink is potentially a very resource-intensive process.\nPlease try to schedule execution of this process during low workload hours.\n\n### `@DatabaseOrder`\n\nSpecify the database order.\n\nValue|Description\n---|---\nNULL|The order that the databases have been specified in. Then ascending by the database name. This is the **default**.\nDATABASE_NAME_ASC|Ascending by the database name\nDATABASE_NAME_DESC|Descending by the database name\nDATABASE_SIZE_ASC|Ascending by the database size\nDATABASE_SIZE_DESC|Descending by the database size\nLOG_SIZE_SINCE_LAST_LOG_BACKUP_ASC|Ascending by `log_since_last_log_backup_mb` in `sys.dm_db_log_stats`\nLOG_SIZE_SINCE_LAST_LOG_BACKUP_DESC|Descending by `log_since_last_log_backup_mb` in `sys.dm_db_log_stats`\n\n### `@DatabasesInParallel`\n\nProcess databases in parallel.\n\nValue|Description\n---|---\nY|Process databases in parallel.\nN|Process databases one at a time. This is the **default**.\n\nYou can process databases in parallel by creating multiple jobs with the same parameters, and add the parameter `@DatabasesInParallel = 'Y'`.\n\n### `@LogToTable`\n\nLog commands to the table dbo.CommandLog.\n\nValue|Description\n---|---\nY|Log commands to the table.\nN|Do not log commands to the table. This is the **default**.\n\n### `@Execute`\n\nExecute commands. By default, the commands are executed normally. If this parameter is set to N, then the commands are printed only.\n\nValue|Description\n---|---\nY|Execute commands. This is the **default**.\nN|Only print commands.\n\n### `@FileTypes`\n\nSpecifies the type of database files that should be affected by this execution. It corresponds to the `type_desc` column of the [sys.master_files](https://docs.microsoft.com/en-us/sql/relational-databases/system-catalog-views/sys-master-files-transact-sql) and [sys.database_files](https://docs.microsoft.com/en-us/sql/relational-databases/system-catalog-views/sys-database-files-transact-sql) system tables. Available values:\n\nValue|Description\n---|---\nALL|Affects both data and transaction log files. This is the **default**.\nROWS|Affects data files only.\nLOG|Affects transaction log files only.\n\nOther file types (such as Full-Text, In-Memory, and FILESTREAM) cannot be shrank or grown using this stored procedure.\n\n## Remarks\n\nThe procedure requires [Ola Hallengren's CommandExecute procedure](https://ola.hallengren.com/scripts/CommandExecute.sql) in order to run.\n\nIf `@LogToTable = 'Y'` is specified, then you must also have the [CommandLog](https://ola.hallengren.com/scripts/CommandLog.sql) table in the same database.\n\nIf `@DatabasesInParallel = 'Y'` is specified, then you must also have the [Queue](https://ola.hallengren.com/scripts/Queue.sql) and [QueueDatabase](https://ola.hallengren.com/scripts/QueueDatabase.sql) tables.\n\n## Examples\n\nTBA\n\n## License\n\nThis project is licensed under the same open-source project as [Ola Hallengren's maintenance solution](https://ola.hallengren.com/license.html).\n\nThe DatabaseFileSizeMaintenance procedure is released under the [MIT license](LICENSE), a popular and widely used open source license.\n\nCopyright (c) 2020 Madeira Data Solutions\n\n## See Also\n\n- [Database File Size Maintenance - Eitan Blumin](https://eitanblumin.com/portfolio/database-file-size-maintenance/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmadeiradata%2Fdatabasefilesizemaintenance","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmadeiradata%2Fdatabasefilesizemaintenance","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmadeiradata%2Fdatabasefilesizemaintenance/lists"}