首页 > 代码库 > DB太大?一键帮你收缩所有DB文件大小(Shrink Files for All Databases in SQL Server)
DB太大?一键帮你收缩所有DB文件大小(Shrink Files for All Databases in SQL Server)
本文介绍一个简单的SQL脚本,实现收缩整个Microsoft SQL Server实例所有非系统DB文件大小的功能。
作为一个与SQL天天打交道的程序猿,经常会遇到DB文件太大,把空间占满的情况:
而对于开发测试人员来说,如果DB数据不是特别重要的话,不会特意扩大磁盘空间,而是直接利用SQL的Shrink File功能缩小DB文件大小,详见:https://docs.microsoft.com/en-us/sql/relational-databases/databases/shrink-a-file。
这里介绍一个脚本,支持一键收缩整个SQL Server实例上所有非系统DB文件大小。
脚本支持功能和相关逻辑如下:
- 五个系统DB(master、model、msdb、tempdb、Resource)不会执行Shrink File操作;
- 如果DB的Recovery Model是FULL或者BULK_LOGGED,会自动改成SIMPLE,Shrink File操作后再改回原来的;
- 只会对DB状态为ONLINE的DB进行Shrink File操作;
- DB的所有文件,包括数据文件和日志文件都会被执行收缩操作;
- 执行SQL脚本的用户需要有sysadmin或者相关数据库的DBO权限。
脚本如下:
1 -- Created by Bob from http://www.cnblogs.com/lavender000/ 2 use master 3 DECLARE dbCursor CURSOR for select name from [master].[sys].[databases] where state = 0 and is_in_standby = 0; 4 DECLARE @dbname NVARCHAR(255) 5 DECLARE @recoveryModel NVARCHAR(255) 6 DECLARE @tempTSQL NVARCHAR(255) 7 DECLARE @dbFilesCursor CURSOR 8 DECLARE @dbFile NVARCHAR(255) 9 DECLARE @flag BIT10 11 OPEN dbCursor12 FETCH NEXT FROM dbCursor INTO @dbname13 14 WHILE @@FETCH_STATUS = 015 BEGIN16 if((@dbname <> ‘master‘) and (@dbname <> ‘model‘) and (@dbname <> ‘msdb‘) and (@dbname <> ‘tempdb‘) and (@dbname <> ‘Resource‘))17 begin18 print(‘‘)19 print(‘Database [‘ + @dbname + ‘] will be shrinked log...‘)20 SET @flag = 121 SET @recoveryModel = (SELECT recovery_model_desc FROM sys.databases WHERE name = @dbname)22 if((@recoveryModel = ‘FULL‘) or (@recoveryModel = ‘BULK_LOGGED‘))23 begin24 SET @tempTSQL = (select CONCAT(‘ALTER DATABASE [‘, @dbname, ‘] SET RECOVERY SIMPLE with no_wait‘))25 EXEC sp_executesql @tempTSQL26 if (@@ERROR = 0)27 begin28 print(‘ Database [‘ + @dbname + ‘] recovery model has been changed to ‘‘SIMPLE‘‘.‘)29 SET @flag = 130 end31 else32 begin33 print(‘Database [‘ + @dbname + ‘] recovery model failed to be changed to ‘‘SIMPLE‘‘.‘)34 SET @flag = 035 end36 end37 38 if(@flag = 1)39 begin40 SET @tempTSQL = (select CONCAT(‘use [‘, @dbname, ‘]‘))41 EXEC sp_executesql @tempTSQL 42 SET @dbFilesCursor = CURSOR for select sys.master_files.name from sys.master_files, [master].[sys].[databases] where databases.name = @dbname and databases.database_id = sys.master_files.database_id43 open @dbFilesCursor44 FETCH NEXT FROM @dbFilesCursor INTO @dbFile45 WHILE @@FETCH_STATUS = 046 BEGIN47 SET @tempTSQL = (select CONCAT(‘use [‘, @dbname, ‘] DBCC SHRINKFILE (N‘‘‘, @dbFile, ‘‘‘) with NO_INFOMSGS‘))48 EXEC sp_executesql @tempTSQL49 if(@@ERROR = 0) print(‘ Database file [‘ + @dbFile + ‘] has been shrinked log successfully.‘)50 FETCH NEXT FROM @dbFilesCursor INTO @dbFile51 END52 CLOSE @dbFilesCursor53 DEALLOCATE @dbFilesCursor54 55 if(@recoveryModel <> ‘SIMPLE‘)56 begin57 -- Finally changed back58 SET @tempTSQL = (select CONCAT(‘ALTER DATABASE [‘, @dbname, ‘] SET RECOVERY ‘, @recoveryModel, ‘ with no_wait‘))59 EXEC sp_executesql @tempTSQL60 if (@@ERROR = 0)61 begin62 print(‘ Database [‘ + @dbname + ‘] recovery model has been changed back to ‘‘‘ + @recoveryModel + ‘‘‘‘)63 end64 else 65 begin66 print(‘ Database [‘ + @dbname + ‘] recovery model failed to be changed back to ‘‘‘ + @recoveryModel + ‘‘‘‘)67 end68 end69 end70 end71 FETCH NEXT FROM dbCursor INTO @dbname72 END73 74 CLOSE dbCursor75 DEALLOCATE dbCursor
执行完效果如下:
Note:
- 如果不放心使用,可提前备份相关数据库;
- 使用前请仔细阅读脚本支持功能和相关逻辑,如与自己需求不符,请不要使用该脚本,或者请根据自己需求自行修改脚本;
- 脚本为简易脚本,仅用于测试学习,可能有BUG,不可生产环境使用,如有错误,请留言。
[原创文章,转载请注明出处,仅供学习研究之用,如有错误请留言,谢谢支持]
[原站点:http://www.cnblogs.com/lavender000/p/6882741.html,来自永远薰薰]
DB太大?一键帮你收缩所有DB文件大小(Shrink Files for All Databases in SQL Server)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。