SQL操作應(yīng)用——SQL SERVER日志清除的兩種方法
方法一
一般情況下,SQL數(shù)據(jù)庫的收縮并不能很大程度上減小數(shù)據(jù)庫大小,其主要作用是收縮日志大小,應(yīng)當(dāng)定期進(jìn)行此操作以免數(shù)據(jù)庫日志過大
1、設(shè)置數(shù)據(jù)庫模式為簡單模式:打開SQL企業(yè)管理器,在控制臺(tái)根目錄中依次點(diǎn)開Microsoft SQL Server-->SQL Server組-->雙擊打開你的服務(wù)器-->雙擊打開數(shù)據(jù)庫目錄-->選擇你的數(shù)據(jù)庫名稱(如論壇數(shù)據(jù)庫Forum)-->然后點(diǎn)擊右鍵選擇屬性-->選擇選項(xiàng)-->在故障還原的模式中選擇“簡單”,然后按確定保存
2、在當(dāng)前數(shù)據(jù)庫上點(diǎn)右鍵,看所有任務(wù)中的收縮數(shù)據(jù)庫,一般里面的默認(rèn)設(shè)置不用調(diào)整,直接點(diǎn)確定
3、收縮數(shù)據(jù)庫完成后,建議將您的數(shù)據(jù)庫屬性重新設(shè)置為標(biāo)準(zhǔn)模式,操作方法同第一點(diǎn),因?yàn)槿罩驹谝恍┊惓G闆r下往往是恢復(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
點(diǎn)擊加載更多評(píng)論>>