SQL操作應(yīng)用——SQL SERVER日志清除的兩種方法
方法一
一般情況下,SQL數(shù)據(jù)庫的收縮并不能很大程度上減小數(shù)據(jù)庫大小,其主要作用是收縮日志大小,應(yīng)當定期進行此操作以免數(shù)據(jù)庫日志過大
1、設(shè)置數(shù)據(jù)庫模式為簡單模式:打開SQL企業(yè)管理器,在控制臺根目錄中依次點開Microsoft SQL Server-->SQL Server組-->雙擊打開你的服務(wù)器-->雙擊打開數(shù)據(jù)庫目錄-->選擇你的數(shù)據(jù)庫名稱(如論壇數(shù)據(jù)庫Forum)-->然后點擊右鍵選擇屬性-->選擇選項-->在故障還原的模式中選擇“簡單”,然后按確定保存
2、在當前數(shù)據(jù)庫上點右鍵,看所有任務(wù)中的收縮數(shù)據(jù)庫,一般里面的默認設(shè)置不用調(diào)整,直接點確定
3、收縮數(shù)據(jù)庫完成后,建議將您的數(shù)據(jù)庫屬性重新設(shè)置為標準模式,操作方法同第一點,因為日志在一些異常情況下往往是恢復(fù)數(shù)據(jù)庫的重要依據(jù)
方法二
SET NOCOUNT ON
DECLARE @LogicalFileName sysname,
@MaxMinutes INT,
@NewSize INT
USE tablename -- 要操作的數(shù)據(jù)庫名
SELECT @LogicalFileName = 'tablename_log', -- 日志文件名
@MaxMinutes = 10, -- Limit on time allowed to wrap log.
@NewSize = 1 -- 你想設(shè)定的日志文件的大小(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
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()) -- time has not expired
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
點擊加載更多評論>>