时间:2021-07-01 10:21:17 帮助过:18人阅读
当mdf或者ndf文件很大时,传统的dbcc shrinkfile命令很难生效,目前可行的解决方案:
大数据文件收缩
DECLARE @BeginSize BIGINT --原始大小MB
DECLARE @EndSize BIGINT --最终大小MB
DECLARE @ShrinkSize BIGINT --需要收缩的大小MB
DECLARE @UnitSize INT --每次收缩的大小MB
DECLARE @Loop INT --循环次数
DECLARE @FileName VARCHAR(256) --收缩的文件逻辑名称
SET @UnitSize = 512
SET @BeginSize = 239493
SET @EndSize = 164413
SET @ShrinkSize = @BeginSize - @EndSize
SET @FileName = N‘需要收缩的数据文件逻辑名称‘
SET @Loop = 1
WHILE @ShrinkSize > @Loop * @UnitSize
BEGIN
SET @EndSize = @BeginSize - @Loop * @UnitSize
PRINT ‘DBCC SHRINKFILE (‘ + CAST(@FileName AS VARCHAR) + ‘,‘ + CAST(@EndSize AS VARCHAR) + ‘) BEGING DATETIME:‘ + CAST(GETDATE() AS VARCHAR)
DBCC SHRINKFILE (@FileName, @EndSize)
PRINT ‘DBCC SHRINKFILE (‘ + CAST(@FileName AS VARCHAR) + ‘,‘ + CAST(@EndSize AS VARCHAR) + ‘) END DATETIME:‘ +CAST(GETDATE() AS VARCHAR)
SET @Loop = @Loop + 1
END
SQL Server压缩数据库文件
标签:解决方案 get 数据库文件 数据文件 while 使用 循环 sql filename