误区 #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)来查看日志文件的大小,你会发现虽然日志文件大小没有变,但是日志的使用空间百分比会大大减少。
(下图是译者测试的结果):