誤區 #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.
(下圖是譯者測試的結果):