标签:
SQL2008 的收缩日志
由于SQL2008对文件和日志管理进行了优化,所以以下语句在SQL2005中可以运行但在SQL2008中已经被取消:
(SQL2000)
-- Prog: Xinsoft
-- Time: 2005-03-26 10:34
SET NOCOUNT ON
DECLARE @LogicalFileName sysname, @MaxMinutes INT, @NewSize INT
USE dbJieXin -- 要操作的数据库名
SELECT @LogicalFileName = ‘dbJieXin_log‘, -- 日志文件名
@MaxMinutes = 10, -- Limit on time allowed to wrap log.
@NewSize = 10 -- 你想设定的日志文件的大小(M)
-- Setup / initialize
DECLARE @OriginalSize int
SELECT @OriginalSize = size FROM sysfiles WHERE name = @LogicalFileName
SELECT ‘Original Size of ‘ + db_name() + ‘ LOG is ‘ + CONVERT(VARCHAR(30),@OriginalSize) + ‘ 8K pages or ‘ + CONVERT(VARCHAR(30),(@OriginalSize*8/1024)) + ‘MB‘ FROM sysfiles WHERE name = @LogicalFileName
--Drop TABLE DummyTrans
CREATE TABLE DummyTrans (DummyColumn char (8000) not null)
DECLARE @Counter INT, @StartTime DATETIME, @TruncLog VARCHAR(255)
SELECT @StartTime = GETDATE(), @TruncLog = ‘BACKUP LOG ‘ + db_name() + ‘ WITH TRUNCATE_ONLY‘
DBCC SHRINKFILE (@LogicalFileName, @NewSize)
EXEC (@TruncLog)
-- Wrap the log if necessary.
WHILE @MaxMinutes > DATEDIFF (mi, @StartTime, GETDATE()) AND @OriginalSize = (SELECT size FROM sysfiles WHERE name = @LogicalFileName) AND (@OriginalSize * 8 /1024) > @NewSize
BEGIN -- Outer loop.
SELECT @Counter = 0
WHILE ((@Counter < @OriginalSize / 16) AND (@Counter < 50000))
BEGIN -- update
INSERT DummyTrans valueS (‘Fill Log‘)
DELETE DummyTrans
SELECT @Counter = @Counter + 1
END
EXEC (@TruncLog)
END
SELECT ‘Final Size of ‘ + db_name() + ‘ LOG is ‘ + CONVERT(VARCHAR(30),size) + ‘ 8K pages or ‘ + CONVERT(VARCHAR(30),(size*8/1024)) + ‘MB‘ FROM sysfiles WHERE name = @LogicalFileName
DROP TABLE DummyTrans
SET NOCOUNT OFF
-------------------------------
USE dbJieXin
backup log dbJieXin with no_log
dump tran dbJieXin with no_log
dbcc shrinkdatabase (dbJieXin)
--------------------------------------------------------------
(SQL2005)
Backup Log DNNamewith no_log
go
dump transaction DNNamewith no_log
go
USE DNName
DBCC SHRINKFILE (2)
Go
--------------------------------------------------------------
(SQL2008):
在SQL2008中清除日志就必须在简单模式下进行,等清除动作完毕再调回到完全模式。
USE [master]
GO
ALTER DATABASE DNName SET RECOVERY SIMPLE WITH NO_WAIT
GO
ALTER DATABASE DNName SET RECOVERY SIMPLE --简单模式
GO
USE DNName
GO
DBCC SHRINKFILE (N‘DNName_Log‘ ,11, TRUNCATEONLY)
GO
USE [master]
GO
ALTER DATABASE DNName SET RECOVERY FULL WITH NO_WAIT
GO
ALTER DATABASE DNName SET RECOVERY FULL --还原为完全模式
GO
优点:此清除日志所运行消耗的时间短,90GB的日志在分钟左右即可清除完毕,做完之后做个完全备份在分钟内
即可完成。
缺点: 不过此动作最好不要经常使用,因为它的运行会带来系统碎片。普通状态下LOG和DIFF的备份即可截断日志。
此语句使用的恰当环境:当系统的日志文件异常增大或者备份LOG时间太长可能影响生产的情况下使用。
标签:
原文地址:http://my.oschina.net/jon/blog/502333