誤區 #14.清除日誌後會將相關的LSN填零初始化
錯誤
當記錄檔在手動增長,自動成長和建立時都會進行填零初始化操作。但是請不要把這個過程和定期清除日誌的過程搞混。日誌截斷僅僅意味著將一個或多個VLF標記為不活動以便被重複使用。在日誌清除的過程中,並沒有任何日誌被清除或是填0。“清除日誌”和”截斷日誌”意思是一樣的,但都屬於用詞不當,因為在這個過程中日誌的大小不會有任何改變。
你可以在我的部落格中看到有關記錄檔填零初始化的博文:Search Engine Q&A #24: Why can't the transaction log use instant initialization?。以及我發布在TechNet雜誌的文章:Understanding Logging and Recovery in SQL Server。
你可以通過跟蹤標記3004來查看SQL Server對記錄檔進行填零初始化的過程。將這個追蹤標記開啟當記錄檔增長時,你就可以在SQL Server日誌中看到相關資訊,下面是測試代碼:
複製代碼 代碼如下:DBCC TRACEON (3004, 3605);
GO
-- Create database and put in SIMPLE recovery model so the log will clear on checkpoint
CREATE DATABASE LogClearTest ON PRIMARY (
NAME = 'LogClearTest_data',
FILENAME = N'D:\SQLskills\LogClearTest_data.mdf')
LOG ON (
NAME = 'LogClearTest_log',
FILENAME = N'D:\SQLskills\LogClearTest_log.ldf',
SIZE = 20MB);
GO
-- Error log mark 1
ALTER DATABASE LogClearTest SET RECOVERY SIMPLE;
GO
USE LogClearTest;
GO
-- Create table and fill with 10MB - so 10MB in the log
CREATE TABLE t1 (c1 INT IDENTITY, c2 CHAR (8000) DEFAULT 'a');
GO
INSERT INTO t1 DEFAULT VALUES;
GO 1280
-- Clear the log
CHECKPOINT;
GO
-- Error log mark 2
ALTER DATABASE LogClearTest SET RECOVERY SIMPLE;
GO
相應的,在日誌中你可以看到: 複製代碼 代碼如下:2010-04-13 13:20:27.55 spid53 DBCC TRACEON 3004, server process ID (SPID) 53. This is an informational message only; no user action is required.
2010-04-13 13:20:27.55 spid53 DBCC TRACEON 3605, server process ID (SPID) 53. This is an informational message only; no user action is required.
2010-04-13 13:20:27.63 spid53 Zeroing D:\SQLskills\LogClearTest_log.ldf from page 0 to 2560 (0x0 to 0x1400000)
2010-04-13 13:20:28.01 spid53 Zeroing completed on D:\SQLskills\LogClearTest_log.ldf
2010-04-13 13:20:28.11 spid53 Starting up database 'LogClearTest'.
2010-04-13 13:20:28.12 spid53 FixupLogTail() zeroing D:\SQLskills\LogClearTest_log.ldf from 0x5000 to 0x6000.
2010-04-13 13:20:28.12 spid53 Zeroing D:\SQLskills\LogClearTest_log.ldf from page 3 to 63 (0x6000 to 0x7e000)
2010-04-13 13:20:28.14 spid53 Zeroing completed on D:\SQLskills\LogClearTest_log.ldf
2010-04-13 13:20:28.16 spid53 Setting database option RECOVERY to SIMPLE for database LogClearTest.
2010-04-13 13:20:29.49 spid53 Setting database option RECOVERY to SIMPLE for database LogClearTest.
上面測試代碼中ALTER DATABASE是作為日誌中這部分的開始和結束標記。在兩個Alter Database命令中的CheckPoint並不會引起填0操作。如果你需要進一步驗證這點,在Checkpoint之前和之後分別使用DBCC SQLPERF (LOGSPACE)來查看記錄檔的大小,你會發現雖然記錄檔大小沒有變,但是日誌的使用空間百分比會大大減少。
(是譯者測試的結果):