【TechTarget中国原创】我用ApexSQL Log工具分析了事务日志内容,结果显示,每次我看事务日志内容都会得到不同的更新数,这同我在事务日志大小上遇到的问题一样;即使不断增长与减少,但总大小依旧保持不变。
为了解事务日志的行为,我运行了一个事务,UPDATE命令修改了行值:
-- Truncate the table truncate table ExpandDB go -- Truncate the T-Log backup transaction ShrinkDB with truncate_only go -- Shrink T-Log back to 2MB: DBCC SHRINKFILE (N'ShribkDB_Log' , 0, TRUNCATEONLY) Go -- Insert one row into the table insert into ExpandDB select replicate ('a',3) --================================================================ -- Update the table until T-Log reaches 8MB -- Big transaction begin tran while (select size from sysfiles where fileid = 2) <= 1024 update ExpandDB set a = case len(a) when 7999 then a else a + 'b' end commit tran go -- Check status of T-log dbcc sqlperf(logspace) go |
在这一情况下,事务日志增长到4MB,使用空间在很长一段时间内保持在94%。然而我没有收到一个错误指示字符串截断或溢出。当事务日志停止增长时,被ApexSQL Log工具监视的事务日志内容始终停留在大约13,380行。此外,SQL Profiler监视语句表明在事务日志停止增长之后没执行过任何命令,没出过任何错。我取消事务后,在错误日志中没有出现任何特殊信息表明autogrowth操作被取消或中止,事务似乎被卡住了。 在类似实例中,我在错误日志中有时会看到下面的信息:
"Autogrow of file '
但无论我执行多少次上面的代码,它都不会出现在测试中。
取消事务之后,事务日志中的使用空间减少到大约45%。
在大量行数情况下,我决定运行以下UPDATE命令:
-- Truncate the T-Log backup transaction ShrinkDB with truncate_only go -- Check the size of the T-Log file (RESULT = 2MB) select size from sysfiles where fileid = 2 go -- Check % free space in T-Log dbcc sqlperf(logspace) go -- Update 10000 rows at a time update ExpandDB set a case len(a) when 7999 then a else a 'b' end go update ExpandDB set a case len(a) when 7999 then a else a 'b' end go update ExpandDB set a case len(a) when 7999 then a else a 'b' end go update ExpandDB seta caselen(a) when 7999 then a else a 'b' end go update ExpandDB set a case len(a) when 7999 then a else a 'b' end go update ExpandDB set a case len(a) when 7999 then a else a 'b' end go update ExpandDB set a case len(a) when 7999 then a else a 'b' end go -- Check % free space in T-Log dbcc sqlperf(logspace) go |
结果表明,如果我一个接一个的运行升级,每次等待一到两秒,事务日志不会增长,空间像原来一样填满清空。然而当同时运行两个更新,在第二个事务运行之前空间不会填满或清空。这一情况下,事务日志将增长。
INSERT命令和事务日志增长
在这个测试中,我的目标是使事务日志增长到8MB,然后将数据文件增长比同事务日志文件增长比做一个比较。数据文件设定为没有autogrowth,大小为200G;事务日志文件设定为2MB大小,没有autogrowth,然后运行以下代码:
-- Truncate the table truncate table ExpandDB go -- Truncate the T-Log backup transaction ShrinkDB with truncate_only go -- Shrink T-Log back to 2MB: DBCC SHRINKFILE (N'ShrinkDB_Log' , 0, TRUNCATEONLY) Go -- Check the size of the T-Log file select size from sysfiles where fileid go -- Insert narrow rows into the table while (select size from sysfiles where fileid = 2) insert into ExpandDB select replicate ('a',3) go At the same time, I also executed the following code: select getdatedbcc sqlperf (logspace) go waitfor delay '00:00:02' go select getdatedbcc sqlperf (logspace) go select getdatego dbcc sqlperf (logspace) go waitfor delay '00:00:02' Go |
我的循环运行了很长时间,数据文件慢慢填充并清空,但事务日志文件始终保持在2MB。以上代码得到如下结果:

显然地,空闲空间减少了。在向测试插入45分钟之前,我的表中有一百八十万行,事务日志仍然是2MB。在取消之后,事务日志还是2MB而且只有39.88%的空间被占用。数据文件31%被占用。
然后在每次插入一行的大量事务中测试:
-- Truncate the table truncate table ExpandDB go -- Truncate the T-Log backup transaction ShrinkDB with truncate_only go -- Shrink T-Log back to 2MB: DBCC SHRINKFILE (N'ShrinkDB_Log' , 0, TRUNCATEONLY) Go -- Insert 100000 rows -- Big transaction begin tran while (select size from sysfiles where fileid = 2) insert into ExpandDB select replicate ('a',3) go commit tran go dbcc sqlperf(logspace) go select count(*) from ExpandDB go |
事务日志增长到9MB,最后表中有14,572行:

为测试大量更新,我向表中插入10,000行并运行以下代码:
-- Truncate the T-Log backup transaction ShrinkDB with truncate_only go -- Check the size of the T-Log file (RESULT = 2MB) select size from sysfiles where fileid = 2 go -- Check % free space in T-Log dbcc sqlperf(logspace) go -- Insert 10000 rows at a time Insert into ExpandDB select * from ExpandDB Go -- Check % free space in T-Log dbcc sqlperf(logspace) go -- Insert 10000 rows at a time Insert into ExpandDB select * from ExpandDB Go -- Check % free space in T-Log dbcc sqlperf(logspace) go -- Insert 10000 rows at a time Insert into ExpandDB select * from ExpandDB Go -- Check % free space in T-Log dbcc sqlperf(logspace) go -- Insert 10000 rows at a time Insert into ExpandDB select * from ExpandDB Go -- Check % free space in T-Log dbcc sqlperf(logspace) go |
这次,每次插入后,事务日志都加倍了。