SQLServer增量收缩数据文件的大小
2022-10-25 14:11 abce 阅读(127) 评论(0) 编辑 收藏 举报增量收缩数据文件脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | -- SQLServer--Shrink_DB_File.sql /* This script is used to shrink a database file in increments until it reaches a target free space limit. Run this script in the database with the file to be shrunk. 1. Set @DBFileName to the name of database file to shrink. 2. Set @TargetFreeMB to the desired file free space in MB after shrink. 3. Set @ShrinkIncrementMB to the increment to shrink file by in MB 4. Run the script */ declare @DBFileName sysname declare @TargetFreeMB int declare @ShrinkIncrementMB int -- Set Name of Database file to shrink set @DBFileName = 'MyDatabaseFileName' -- Set Desired file free space in MB after shrink set @TargetFreeMB = 1000 -- Set Increment to shrink file by in MB set @ShrinkIncrementMB = 50 -- Show Size, Space Used, Unused Space, and Name of all database files select [FileSizeMB]= convert ( numeric (10,2),round(a. size /128.,2)), [UsedSpaceMB]= convert ( numeric (10,2),round(fileproperty( a. name , 'SpaceUsed' )/128.,2)) , [UnusedSpaceMB]= convert ( numeric (10,2),round((a. size -fileproperty( a. name , 'SpaceUsed' ))/128.,2)) , [DBFileName]= a. name from sysfiles a declare @sql varchar (8000) declare @SizeMB int declare @UsedMB int -- Get current file size in MB select @SizeMB = size /128. from sysfiles where name = @DBFileName -- Get current space used in MB select @UsedMB = fileproperty( @DBFileName, 'SpaceUsed' )/128. select [StartFileSize] = @SizeMB, [StartUsedSpace] = @UsedMB, [DBFileName] = @DBFileName -- Loop until file at desired size while @SizeMB > @UsedMB+@TargetFreeMB+@ShrinkIncrementMB begin set @sql = 'dbcc shrinkfile ( ' +@DBFileName+ ', ' + convert ( varchar (20),@SizeMB-@ShrinkIncrementMB)+ ' ) ' print 'Start ' + @sql print 'at ' + convert ( varchar (30),getdate(),121) exec ( @sql ) print 'Done ' + @sql print 'at ' + convert ( varchar (30),getdate(),121) -- Get current file size in MB select @SizeMB = size /128. from sysfiles where name = @DBFileName -- Get current space used in MB select @UsedMB = fileproperty( @DBFileName, 'SpaceUsed' )/128. select [FileSize] = @SizeMB, [UsedSpace] = @UsedMB, [DBFileName] = @DBFileName end select [EndFileSize] = @SizeMB, [EndUsedSpace] = @UsedMB, [DBFileName] = @DBFileName -- Show Size, Space Used, Unused Space, and Name of all database files select [FileSizeMB]= convert ( numeric (10,2),round(a. size /128.,2)), [UsedSpaceMB]= convert ( numeric (10,2),round(fileproperty( a. name , 'SpaceUsed' )/128.,2)) , [UnusedSpaceMB]= convert ( numeric (10,2),round((a. size -fileproperty( a. name , 'SpaceUsed' ))/128.,2)) , [DBFileName]= a. name from sysfiles a |
查看收缩的进度:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | SELECT a.database_id, d. name , a.session_id, a.command, b.text, percent_complete, done_in_minutes = a.estimated_completion_time / 1000 / 60, min_in_progress = DATEDIFF( MI, a.start_time, DATEADD( ms, a.estimated_completion_time, GETDATE( ) ) ), a.start_time, CONVERT ( VARCHAR , ( total_elapsed_time / ( 1000 ) ) / 60 ) + 'M ' + CONVERT ( VARCHAR , ( total_elapsed_time / ( 1000 ) ) % 60 ) + 'S' AS [Elapsed], CONVERT ( VARCHAR , ( estimated_completion_time / ( 1000 ) ) / 60 ) + 'M ' + CONVERT ( VARCHAR , ( estimated_completion_time / ( 1000 ) ) % 60 ) + 'S' AS [ETA], a.cpu_time, a.status, estimated_completion_time = DATEADD( ms, a.estimated_completion_time, GETDATE( ) ), connection_id, blocking_session_id FROM sys.dm_exec_requests a CROSS APPLY sys.dm_exec_sql_text ( a.sql_handle ) b LEFT JOIN sys.databases D ON a.database_id = d.database_id WHERE command LIKE '%dbcc%' ; |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
2016-10-25 ORA-25153: Temporary Tablespace is Empty
2015-10-25 创建一个已经存在数据的MySQL复制