清除SQL Server日志的兩種方法

2010-08-28 10:54:46來(lái)源:西部e網(wǎng)作者:

方法一

一般情況下,SQL數(shù)據(jù)庫(kù)的收縮并不能很大程度上減小數(shù)據(jù)庫(kù)大小,其主要作用是收縮日志大小,應(yīng)當(dāng)定期進(jìn)行此操作以免數(shù)據(jù)庫(kù)日志過(guò)大

1、設(shè)置數(shù)據(jù)庫(kù)模式為簡(jiǎn)單模式:打開(kāi)SQL企業(yè)管理器,在控制臺(tái)根目錄中依次點(diǎn)開(kāi)Microsoft SQL Server-->SQL Server組-->雙擊打開(kāi)你的服務(wù)器-->雙擊打開(kāi)數(shù)據(jù)庫(kù)目錄-->選擇你的數(shù)據(jù)庫(kù)名稱(chēng)(如論壇數(shù)據(jù)庫(kù)Forum)-->然后點(diǎn)擊右鍵選擇屬性-->選擇選項(xiàng)-->在故障還原的模式中選擇“簡(jiǎn)單”,然后按確定保存

2、在當(dāng)前數(shù)據(jù)庫(kù)上點(diǎn)右鍵,看所有任務(wù)中的收縮數(shù)據(jù)庫(kù),一般里面的默認(rèn)設(shè)置不用調(diào)整,直接點(diǎn)確定

3、收縮數(shù)據(jù)庫(kù)完成后,建議將您的數(shù)據(jù)庫(kù)屬性重新設(shè)置為標(biāo)準(zhǔn)模式,操作方法同第一點(diǎn),因?yàn)槿罩驹谝恍┊惓G闆r下往往是恢復(fù)數(shù)據(jù)庫(kù)的重要依據(jù)

方法二

  SET NOCOUNT ON DECLARE @LogicalFileName sysname,

  @MaxMinutes INT,

  @NewSize INT

  USE tablename -- 要操作的數(shù)據(jù)庫(kù)名

  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

關(guān)鍵詞:SQLServer

贊助商鏈接: