4. 跟踪标记 (Trace Flag) 610 对索引组织表(IOT)最小化日志

跟踪标记:610sql

功能:数据库

  • 用批量导入操做(Bulk Import Operations)加载数据时,对于索引组织表(即有汇集索引的表) 最小化日志;

 

上图为simple/bulk-logged恢复模式下,最小化日志的几种操做,其中包含了批量导入操做,而批量导入操做的最小化日志有一些前提条件,归纳以下:less

1. 目标表未用于复制;ide

2. 目标表上指定了TABLOCK性能

3. 目标表上的索引状况,这条规则最复杂,见下表:测试

从表格能够看出:优化

(1) 堆表的数据页一直能够最小化日志;ui

(2) 汇集索引和非汇集索引,一直是彻底记录日志的,除了在空表的状况下(即索引也是空的),第一个批次(batch)导入的数据能够最小化日志,从第二个批次(batch)起就再也不是最小化日志,缘由就是第一个批次(batch)结束后,就再也不是空表了,跟踪标记610也正是由于这个而出现;spa

 

用途:日志

  • 提高索引组织表(即有汇集索引的表)批量导入操做的性能;

 

备注:

(1) 从SQL Server 2008 开始,引入了跟踪标记610;

(2) 从SQL Server 2016开始,跟踪标记610所具有的功能,已经被数据库引擎所默认,不须要再额外手动开启跟踪标记 (一样的,也就没有开关去关闭) ;

 

测试:观察[Log Record Length]这列的变化和区别

-- Set Recover model to SIMPLE/BULK_LOGGED
ALTER DATABASE testing SET RECOVERY SIMPLE;

/**************************START of CREATE TEST TABLES******************************/
USE testing
GO

IF OBJECT_ID('SrcHeap') IS NOT NULL 
 DROP TABLE SrcHeap;

IF OBJECT_ID('TarHeap') IS NOT NULL 
 DROP TABLE TarHeap;

IF OBJECT_ID('TarTable') IS NOT NULL 
 DROP TABLE TarTable;

CREATE TABLE SrcHeap (col1 INT ,col2 CHAR(4000),col3 CHAR(1000) ) ; 

CREATE TABLE TarHeap( col1 INT ,col2 CHAR(4000),col3 CHAR(1000) ) ;

CREATE TABLE TarTable (col1 INT ,col2 CHAR(4000),col3 CHAR(1000) ); 
create clustered index IX_01 on TarTable(col1);

--Insert row into source table
WITH Nums (col)
AS 
(
 SELECT 1 col
 UNION ALL 
 SELECT col + 1 FROM Nums
 WHERE col+1 <= 10000
)
INSERT INTO SrcHeap(col1,col2,col3) 
 SELECT col,replicate('A',4000),replicate('B',1000) FROM Nums 
 OPTION (MAXRECURSION 10000)
/**************************END of CREATE TEST TABLES******************************/

/**************************START of HEAP testing******************************/
--Insert rows to Target Table with (TABLOCK) Minimally logged
INSERT INTO TarHeap WITH(TABLOCK)
 SELECT * FROM SrcHeap 

-- Check Log Entries
SELECT TOP 10 operation [MINIMALLY LOGGED OPERATION],context, [log record fixed length], [log record length], AllocUnitId, AllocUnitName
 FROM fn_dblog(null, null)
 WHERE allocunitname='dbo.TarHeap'
 ORDER BY [Log Record Length] DESC;
--Note That [Log Record length] is small 

--Insert rows to Target Table without (TABLOCK) fully logged
INSERT INTO TarHeap 
 SELECT * FROM SrcHeap WITH(NOLOCK);

-- Check Log Entries
SELECT TOP 10 operation [FULLY LOGGED OPERATION],context, [log record fixed length], [log record length], AllocUnitId, AllocUnitName
 FROM fn_dblog(null, null)
 WHERE allocunitname='dbo.TarHeap'
 ORDER BY [Log Record Length] DESC;
--Note That [Log Record length] is big 
/**************************END of HEAP testing******************************/

/**************************START of INDEXED TABLES testing WITHOUT 610******************************/
--Insert rows to Target Table with clustered index and trace flag 610 off 
--Fully logged from second batch

--First Batch
INSERT INTO TarTable  WITH(TABLOCK)
 SELECT * FROM SrcHeap WITH(NOLOCK);

CHECKPOINT;
--first batch with or without 610
select * 
 FROM fn_dblog(null, null)
 WHERE allocunitname LIKE '%TarTable%' --4582 rows
  and operation = 'LOP_INSERT_ROWS'--0 rows

--Second Batch
INSERT INTO TarTable  WITH(TABLOCK)
 SELECT col1+10000,col2,col3 FROM SrcHeap WITH(NOLOCK);

CHECKPOINT
--from second batch without 610, tested twice
SELECT * 
 FROM fn_dblog(null, null)
 WHERE allocunitname LIKE '%TarTable%' --114308 rows, 114293 rows
  and operation = 'LOP_INSERT_ROWS'--20090 rows, 20088 rows
  and (context  = 'LCX_CLUSTERED'   --10000 rows (actual rows)
       or 
       context = 'LCX_INDEX_INTERIOR' --44 rows (description)
      )
 ORDER BY [Log Record Length] DESC
/**************************END of INDEXED TABLES testing WITHOUT 610******************************/

CHECKPOINT;
GO
DBCC TRACEON(610);
TRUNCATE TABLE TarTable;
GO

/**************************START of INDEXED TABLES testing WITH 610******************************/
--Insert rows to Target Table with clustered index and trace flag 610 on 
--Minimally logged for all batches
--with 610 enables + with TABLOCK, the first bath logged less than second batch
--with 610 enables + without TABLOCK, the first batch processes as same as begining with second batch
INSERT INTO TarTable  --WITH(TABLOCK)
 SELECT * FROM SrcHeap WITH(NOLOCK);

INSERT INTO TarTable  --WITH(TABLOCK)
 SELECT col1+10000,col2,col3 FROM SrcHeap WITH(NOLOCK);

CHECKPOINT
--from second batch with 610
SELECT * 
 FROM fn_dblog(null, null)
 WHERE allocunitname LIKE '%TarTable%' --54995 rows
  and operation = 'LOP_INSERT_ROWS'--10090 rows
  and (context  = 'LCX_CLUSTERED'   --0 rows (autual rows)
       or 
       context = 'LCX_INDEX_INTERIOR' --44 rows (description)
      )
 ORDER BY [Log Record Length] DESC
/**************************END of INDEXED TABLES testing WITH 610******************************/

DBCC TRACEOFF(610)
DBCC TRACESTATUS(-1)

 

小结:

(1) 条件容许状况下,批量导入操做仍是跑在堆表上性能最佳;

(2) 跟踪标记610被开启后,对于汇集索引,只有新分配的数据页才会最小化日志,数据插入已有数据页,仍然是fully logged,因此建表时还得考虑汇集索引键的选择;

(3) 跟踪标记610被开启后,对于非汇集索引,并不必定能够最小化日志,这取决于查询优化器对执行计划的选择;

(4) 跟踪标记610被开启后,对于堆表,仍然要指定TABLOCK;对于索引组织表,可不指定TABLOCK,也仍然能够最小化日志,每一个批次(batch)最小化日志方式一致;

 

参考:

Operations That Can Be Minimally Logged

https://technet.microsoft.com/en-us/library/ms191244(v=sql.105).aspx

 

Prerequisites for Minimal Logging in Bulk Import

https://technet.microsoft.com/en-us/library/ms190422(v=sql.105).aspx

 

DBCC TRACEON - Trace Flags (Transact-SQL)

https://docs.microsoft.com/en-us/sql/t-sql/database-console-commands/dbcc-traceon-trace-flags-transact-sql

 

The Data Loading Performance Guide

https://technet.microsoft.com/en-us/library/dd425070(v=sql.100).aspx

相关文章
相关标签/搜索