Eu escrevi um script para testar as afirmações feitas nesta página http://technet.microsoft.com/en-us/library/dd425070(v=sql.100).aspx na tabela intitulada Resumindo as condições mínimas de registro sobre quando o registro mínimo ocorre ou não.
Usando esse script, acho que a soma dos comprimentos do registro de log para cada um dos diferentes tipos de inserções é a seguinte:
- Pilha vazia no tablock 60000
- Pilha vazia com tablock 56000
- Pilha não vazia no tablock 60000
- Pilha não vazia com tablock 56000
- Pilha mais índice vazio no tablock 126188
- Pilha mais índice vazio com tablock 114188
- Heap mais índice não vazio no tablock 138696
- Pilha mais índice não vazio com tablock 112000
- Cluster vazio não solicitado no tablock 64168
- Conjunto vazio encomendado com tablock 56168
- Cluster vazio não ordenado sem tablock 73388
- Cluster vazio não ordenado com tablock 65388
- Cluster não vazio sem tablock 63912
- Cluster não vazio com tablock 55944
- Cluster mais índice vazio sem tablock 124336
- Cluster mais índice vazio com tablock 108336
- Cluster mais índice não vazio no tablock 123876
- Cluster mais índice não vazio com o tablock 107924
Alguns desses números não parecem coincidir com a tabela na página do technet. Em particular:
- Parece não haver diferença no log entre a inserção em tabelas vazias e não vazias, mas a página afirma que deve haver log completo ao inserir em um cluster não vazio sem tablock
- Inserir com tablock em um heap ou cluster com e índice parece reduzir o log, mas a página afirma que deve haver log completo.
- Ao usar o método SELECT INTO de inserção, não há linhas no fn_dblog cuja operação é inserida, mas a página lista esse método como uma operação de carregamento em massa que deve ter o comportamento descrito na tabela
Para referência, isso foi executado em um banco de dados SQL Express e, quando executo o DBCC TRACESTATUS (610), tudo é 0.
Alguém pode ajudar a explicar por que estou vendo essas discrepâncias?
Para referência, o código está abaixo:
SET NOCOUNT ON
CREATE TABLE numbers (num INT)
CREATE TABLE numbersUnordered (num INT)
Declare @cnt int
Select @cnt=0
while (@cnt<500)
BEGIN
INSERT INTO NUMBERS(num) SELECT @cnt
SELECT @cnt=@cnt+1
END
Select @cnt=0
while (@cnt<250)
BEGIN
INSERT INTO numbersUnordered(num) SELECT @cnt*2
SELECT @cnt=@cnt+1
END
Select @cnt=0
while (@cnt<250)
BEGIN
INSERT INTO numbersUnordered(num) SELECT @cnt*2+1
SELECT @cnt=@cnt+1
END
---- heap empty without tablock
CREATE TABLE noKey1 (val INT)
INSERT INTO noKey1 (val)
SELECT * FROM numbers
DECLARE @heapEmptyNoTablock INT
SELECT @heapEmptyNoTablock = SUM([Log Record Length])
FROM sys.fn_dblog(null, null)
WHERE allocunitname like '%noKey1%'
AND operation like '%insert%'
---- heap empty with tablock
CREATE TABLE noKey2 (val INT)
INSERT INTO noKey2 WITH(TABLOCK) (val)
SELECT * FROM numbers
DECLARE @heapEmptyTablock INT
SELECT @heapEmptyTablock = SUM([Log Record Length])
FROM sys.fn_dblog(null, null)
WHERE allocunitname like '%noKey2%'
AND operation like '%insert%'
---- heap non empty without tablock
CREATE TABLE noKey3 (val INT)
INSERT INTO noKey3 WITH(TABLOCK) (val)
SELECT * FROM numbers
INSERT INTO noKey3 (val)
SELECT num+5 FROM numbers
DECLARE @heapNonEmptyNoTablock INT
SELECT @heapNonEmptyNoTablock = SUM(a.[Log Record Length])
FROM
(SELECT TOP 500 * FROM sys.fn_dblog(null, null)
WHERE allocunitname like '%noKey3%'
AND operation like '%insert%'
ORDER BY [Current LSN] desc) a
---- heap non empty with tablock
CREATE TABLE noKey4 (val INT)
INSERT INTO noKey4 WITH(TABLOCK) (val)
SELECT * FROM numbers
INSERT INTO noKey4 WITH (TABLOCK) (val)
SELECT num+5 FROM numbers
DECLARE @heapNonEmptyTablock INT
SELECT @heapNonEmptyTablock = SUM(a.[Log Record Length])
FROM
(SELECT TOP 500 * FROM sys.fn_dblog(null, null)
WHERE allocunitname like '%noKey4%'
AND operation like '%insert%'
ORDER BY [Current LSN] desc) a
--- heap plus index empty without tablock
CREATE TABLE noKey5 (val INT)
CREATE INDEX MSindex1
ON noKey5 (val)
INSERT INTO noKey5 (val)
SELECT * FROM numbers
DECLARE @heapIndexEmptyNoTablock INT
SELECT @heapIndexEmptyNoTablock = SUM([Log Record Length])
FROM sys.fn_dblog(null, null)
WHERE allocunitname like '%noKey5%'
AND operation like '%insert%'
--- heap plus index empty with tablock
CREATE TABLE noKey6 (val INT)
CREATE INDEX MSindex2
ON noKey6 (val)
INSERT INTO noKey6 WITH(TABLOCK) (val)
SELECT * FROM numbers
DECLARE @heapIndexEmptyTablock INT
SELECT @heapIndexEmptyTablock = SUM([Log Record Length])
FROM sys.fn_dblog(null, null)
WHERE allocunitname like '%noKey6%'
AND operation like '%insert%'
--- heap plus index non empty without tablock
CREATE TABLE noKey7 (val INT)
CREATE INDEX MSindex3
ON noKey7 (val)
INSERT INTO noKey7 WITH(TABLOCK) (val)
SELECT * FROM numbers
INSERT INTO noKey7 (val)
SELECT num+5 FROM numbers
DECLARE @heapIndexNonEmptyNoTablock INT
SELECT @heapIndexNonEmptyNoTablock = SUM(a.[Log Record Length])
FROM
(SELECT TOP 1000 * FROM sys.fn_dblog(null, null)
WHERE allocunitname like '%noKey7%'
AND operation like '%insert%'
ORDER BY [Current LSN] desc) a
--- heap plus index non empty with tablock
CREATE TABLE noKey8 (val INT)
CREATE INDEX MSindex4
ON noKey7 (val)
INSERT INTO noKey8 WITH(TABLOCK) (val)
SELECT * FROM numbers
INSERT INTO noKey8 WITH(TABLOCK) (val)
SELECT num+5 FROM numbers
DECLARE @heapIndexNonEmptyTablock INT
SELECT @heapIndexNonEmptyTablock = SUM(a.[Log Record Length])
FROM
(SELECT TOP 1000 * FROM sys.fn_dblog(null, null)
WHERE allocunitname like '%noKey8%'
AND operation like '%insert%'
ORDER BY [Current LSN] desc) a
--- cluster empty ordered without tablock
CREATE TABLE withKey1 (val INT PRIMARY KEY)
INSERT INTO withKey1 (val)
SELECT * FROM numbers
DECLARE @clusterEmptyNoTablock INT
SELECT @clusterEmptyNoTablock = SUM([Log Record Length])
FROM sys.fn_dblog(null, null)
WHERE allocunitname like '%withKey1%'
AND operation like '%insert%'
--- cluster empty ordered with tablock
CREATE TABLE withKey2 (val INT PRIMARY KEY)
INSERT INTO withKey2 WITH(TABLOCK) (val)
SELECT * FROM numbers
DECLARE @clusterEmptyTablock INT
SELECT @clusterEmptyTablock = SUM([Log Record Length])
FROM sys.fn_dblog(null, null)
WHERE allocunitname like '%withKey2%'
AND operation like '%insert%'
--- cluster empty unordered without tablock
CREATE TABLE withKey5 (val INT PRIMARY KEY)
INSERT INTO withKey5 (val)
SELECT * FROM numbersUnordered
DECLARE @clusterEmptyNoTablockUnordered INT
SELECT @clusterEmptyNoTablockUnordered = SUM([Log Record Length])
FROM sys.fn_dblog(null, null)
WHERE allocunitname like '%withKey5%'
AND operation like '%insert%'
--- cluster empty undordered with tablock
CREATE TABLE withKey6 (val INT PRIMARY KEY)
INSERT INTO withKey6 WITH(TABLOCK) (val)
SELECT * FROM numbersUnordered
DECLARE @clusterEmptyTablockUnordered INT
SELECT @clusterEmptyTablockUnordered = SUM([Log Record Length])
FROM sys.fn_dblog(null, null)
WHERE allocunitname like '%withKey6%'
AND operation like '%insert%'
--- cluster non empty no tablock
CREATE TABLE withKey7 (val INT PRIMARY KEY)
INSERT INTO withKey7 (val)
SELECT num FROM numbers
INSERT INTO withKey7 (val)
SELECT num+500 FROM numbers
DECLARE @clusterNonEmptyNoTablock INT
SELECT @clusterNonEmptyNoTablock = SUM(a.[Log Record Length])
FROM
(SELECT TOP 500 * FROM sys.fn_dblog(null, null)
WHERE allocunitname like '%withKey7%'
AND operation like '%insert%'
ORDER BY [Current LSN] desc) a
--- cluster non empty with tablock
CREATE TABLE withKey8 (val INT PRIMARY KEY)
INSERT INTO withKey8 WITH(TABLOCK) (val)
SELECT num FROM numbers
INSERT INTO withKey8 WITH(TABLOCK) (val)
SELECT num+500 FROM numbers
DECLARE @clusterNonEmptyTablock INT
SELECT @clusterNonEmptyTablock = SUM(a.[Log Record Length])
FROM
(SELECT TOP 500 * FROM sys.fn_dblog(null, null)
WHERE allocunitname like '%withKey8%'
AND operation like '%insert%'
ORDER BY [Current LSN] desc) a
--- cluster plus index empty no tablock
CREATE TABLE withKey9 (val INT PRIMARY KEY)
CREATE INDEX MSindex5
ON withKey9 (val)
INSERT INTO withKey9 (val)
SELECT * FROM numbers
DECLARE @clusterIndexEmptyNoTablock INT
SELECT @clusterIndexEmptyNoTablock = SUM([Log Record Length])
FROM sys.fn_dblog(null, null)
WHERE allocunitname like '%withKey9%'
AND operation like '%insert%'
--- cluster plus index empty with tablock
CREATE TABLE withKey10 (val INT PRIMARY KEY)
CREATE INDEX MSindex6
ON withKey10 (val)
INSERT INTO withKey10 WITH(TABLOCK) (val)
SELECT * FROM numbers
DECLARE @clusterIndexEmptyTablock INT
SELECT @clusterIndexEmptyTablock = SUM([Log Record Length])
FROM sys.fn_dblog(null, null)
WHERE allocunitname like '%withKey10%'
AND operation like '%insert%'
--- cluster plus index nonempty no tablock
CREATE TABLE withKey11 (val INT PRIMARY KEY)
CREATE INDEX MSindex7
ON withKey11 (val)
INSERT INTO withKey11 (val)
SELECT num FROM numbers
INSERT INTO withKey11 (val)
SELECT num+500 FROM numbers
DECLARE @clusterIndexNonEmptyNoTablock INT
SELECT @clusterIndexNonEmptyNoTablock = SUM(a.[Log Record Length])
FROM
(SELECT TOP 1000 * FROM sys.fn_dblog(null, null)
WHERE allocunitname like '%withKey11%'
AND operation like '%insert%'
ORDER BY [Current LSN] desc) a
--- cluster plus index nonempty with tablock
CREATE TABLE withKey12 (val INT PRIMARY KEY)
CREATE INDEX MSindex8
ON withKey12 (val)
INSERT INTO withKey12 WITH(TABLOCK) (val)
SELECT num FROM numbers
INSERT INTO withKey12 WITH(TABLOCK) (val)
SELECT num+500 FROM numbers
DECLARE @clusterIndexNonEmptyTablock INT
SELECT @clusterIndexNonEmptyTablock = SUM(a.[Log Record Length])
FROM
(SELECT TOP 1000 * FROM sys.fn_dblog(null, null)
WHERE allocunitname like '%withKey12%'
AND operation like '%insert%'
ORDER BY [Current LSN] desc) a
--- select into
/*SELECT *
INTO selectIntoTable
FROM numbers
SELECT * FROM sys.fn_dblog(null, null)
WHERE allocunitname like '%selectIntoTable%'
AND operation like '%insert%'
DROP TABLE selectIntoTable
*/
PRINT 'Heap empty no tablock ' + CAST(@heapEmptyNoTablock AS VARCHAR)
PRINT 'Heap empty with tablock ' + CAST(@heapEmptyTablock AS VARCHAR)
PRINT 'Heap non empty no tablock ' + CAST(@heapNonEmptyNoTablock AS VARCHAR)
PRINT 'Heap non empty with tablock ' + CAST(@heapNonEmptyTablock AS VARCHAR)
PRINT 'Heap plus index empty no tablock ' + CAST(@heapIndexEmptyNoTablock AS VARCHAR)
PRINT 'Heap plus index empty with tablock ' + CAST(@heapIndexEmptyTablock AS VARCHAR)
PRINT 'Heap plus index non empty no tablock ' + CAST(@heapIndexNonEmptyNoTablock AS VARCHAR)
PRINT 'Heap plus index non empty with tablock ' + CAST(@heapIndexNonEmptyTablock AS VARCHAR)
PRINT 'Cluster empty ordered no tablock ' + CAST(@clusterEmptyNoTablock AS VARCHAR)
PRINT 'Cluster empty ordered with tablock ' + CAST(@clusterEmptyTablock AS VARCHAR)
PRINT 'Cluster empty unordered no tablock ' + CAST(@clusterEmptyNoTablockUnordered AS VARCHAR)
PRINT 'Cluster empty unordered with tablock ' + CAST(@clusterEmptyTablockUnordered AS VARCHAR)
PRINT 'Cluster non empty no tablock ' + CAST(@clusterNonEmptyNoTablock AS VARCHAR)
PRINT 'Cluster non empty with tablock ' + CAST(@clusterNonEmptyTablock AS VARCHAR)
PRINT 'Cluster plus index empty no tablock ' + CAST(@clusterIndexEmptyNoTablock AS VARCHAR)
PRINT 'Cluster plus index empty with tablock ' + CAST(@clusterIndexEmptyTablock AS VARCHAR)
PRINT 'Cluster plus index non empty no tablock ' + CAST(@clusterIndexNonEmptyNoTablock AS VARCHAR)
PRINT 'Cluster plus index non empty with tablock ' + CAST(@clusterIndexNonEmptyTablock AS VARCHAR)
DROP TABLE numbers
DROP TABLE numbersUnordered
DROP TABLE noKey1
DROP TABLE noKey2
DROP TABLE noKey3
DROP TABLE noKey4
DROP TABLE noKey5
DROP TABLE noKey6
DROP TABLE noKey7
DROP TABLE noKey8
DROP TABLE withKey1
DROP TABLE withKey2
DROP TABLE withKey5
DROP TABLE withKey6
DROP TABLE withKey7
DROP TABLE withKey8
DROP TABLE withKey9
DROP TABLE withKey10
DROP TABLE withKey11
DROP TABLE withKey12