在SQL Server 2008中,我试图在此处看到的顺序与非顺序GUID键上的聚簇索引实验中重现结果 http://sqlblog.com/blogs/denis_gobo/archive/2009/02/05/11743.aspx 但是我没有经历过我期望的插入(以及作者体验)的显着加速。使用顺序GUID可以明显提高页面利用率,但由于某些原因,插入10,000行的速度仅提高约100毫秒(超过10,300毫秒)。
我使用以下代码:
CREATE TABLE TestGuid1 (Id UNIQUEIDENTIFIER not null DEFAULT newid(),
SomeDate DATETIME, batchNumber BIGINT)
CREATE TABLE TestGuid2 (Id UNIQUEIDENTIFIER not null DEFAULT newsequentialid(),
SomeDate DATETIME, batchNumber BIGINT)
CREATE CLUSTERED INDEX ix_id1 ON TestGuid1(id)
CREATE CLUSTERED INDEX ix_id2 ON TestGuid2(id)
go
SET NOCOUNT ON
INSERT TestGuid1 (SomeDate,batchNumber) VALUES (GETDATE(),3)
go 10000
SET NOCOUNT ON
INSERT TestGuid2 (SomeDate,batchNumber) VALUES (GETDATE(),3)
go 10000
DBCC showcontig ('TestGuid1') WITH tableresults
DBCC showcontig ('TestGuid2') WITH tableresults
SELECT batchNumber,DATEDIFF(ms,MIN(SomeDate),MAX(SomeDate))
FROM TestGuid1
GROUP BY batchNumber
SELECT batchNumber,DATEDIFF(ms,MIN(SomeDate),MAX(SomeDate))
FROM TestGuid2
GROUP BY batchNumber
有人可以解释为什么我在TestGuid2上没有经历更显着的加速加速吗?
后续: 根据以下主题中的要求,我扩展了测试:测试结果随时间变化趋于显着,因此现在实验重复N次,并报告总时间和平均时间。我还添加了第三个测试,即连续整数列上的主键。这应该是所有三种方法中最快和最紧凑的,因为整数类型较小且IDENTITY(1,1)是(或至少应该)快。至少通过我的直觉。 平均执行时间现在是顺序GUID的好处,但令人惊讶的是,第三个实验中的插入(使用顺序整数键)比顺序GUID 更慢。我没有解释这个。 以下是新实验的代码:
SET NOCOUNT ON
CREATE TABLE TestGuid1 (Id UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID() PRIMARY KEY,
SomeDate DATETIME, batchNumber BIGINT, FILLER CHAR(100))
CREATE TABLE TestGuid2 (Id UNIQUEIDENTIFIER NOT NULL DEFAULT NEWSEQUENTIALID() PRIMARY KEY,
SomeDate DATETIME, batchNumber BIGINT, FILLER CHAR(100))
CREATE TABLE TestInt (Id Int NOT NULL identity(1,1) PRIMARY KEY,
SomeDate DATETIME, batchNumber BIGINT, FILLER CHAR(100))
DECLARE @BatchCounter INT = 1
DECLARE @Numrows INT = 100000
WHILE (@BatchCounter <= 20)
BEGIN
BEGIN TRAN
DECLARE @LocalCounter INT = 0
WHILE (@LocalCounter <= @NumRows)
BEGIN
INSERT TestGuid1 (SomeDate,batchNumber) VALUES (GETDATE(),@BatchCounter)
SET @LocalCounter +=1
END
SET @LocalCounter = 0
WHILE (@LocalCounter <= @NumRows)
BEGIN
INSERT TestGuid2 (SomeDate,batchNumber) VALUES (GETDATE(),@BatchCounter)
SET @LocalCounter +=1
END
SET @LocalCounter = 0
WHILE (@LocalCounter <= @NumRows)
BEGIN
INSERT TestInt (SomeDate,batchNumber) VALUES (GETDATE(),@BatchCounter)
SET @LocalCounter +=1
END
SET @BatchCounter +=1
COMMIT
END
DBCC showcontig ('TestGuid1') WITH tableresults
DBCC showcontig ('TestGuid2') WITH tableresults
DBCC showcontig ('TestInt') WITH tableresults
SELECT batchNumber,DATEDIFF(ms,MIN(SomeDate),MAX(SomeDate)) AS [NEWID()]
FROM TestGuid1
GROUP BY batchNumber
SELECT batchNumber,DATEDIFF(ms,MIN(SomeDate),MAX(SomeDate)) AS [NEWSEQUENTIALID()]
FROM TestGuid2
GROUP BY batchNumber
SELECT batchNumber,DATEDIFF(ms,MIN(SomeDate),MAX(SomeDate)) AS [IDENTITY()]
FROM TestInt
GROUP BY batchNumber
DROP TABLE TestGuid1
DROP TABLE TestGuid2
DROP TABLE TestInt
平均执行时间:
NEWID() 3064
NEWSEQUENTIALID() 1977
IDENTITY() 2223
页面用法如下:
Table Pages AveragePageDensity
----------------------------------------
TestGuid1 50871 68,4
TestGuid2 35089 99,2
TestInt 32259 98,7
我没有看到,为什么这些页面统计信息(最适合TestInt)并不意味着实验三是最快的。
答案 0 :(得分:2)
您可以尝试修改此脚本并发布结果吗?
SET NOCOUNT ON
CREATE TABLE TestGuid1 (Id UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID() PRIMARY KEY,
SomeDate DATETIME, batchNumber BIGINT, FILLER CHAR(100))
CREATE TABLE TestGuid2 (Id UNIQUEIDENTIFIER NOT NULL DEFAULT NEWSEQUENTIALID() PRIMARY KEY,
SomeDate DATETIME, batchNumber BIGINT, FILLER CHAR(100))
DECLARE @BatchCounter INT = 1
WHILE (@BatchCounter <= 20)
BEGIN
BEGIN TRAN
DECLARE @LocalCounter INT = 0
WHILE (@LocalCounter <= 100000)
BEGIN
INSERT TestGuid1 (SomeDate,batchNumber) VALUES (GETDATE(),@BatchCounter)
SET @LocalCounter +=1
END
SET @LocalCounter = 0
WHILE (@LocalCounter <= 100000)
BEGIN
INSERT TestGuid2 (SomeDate,batchNumber) VALUES (GETDATE(),@BatchCounter)
SET @LocalCounter +=1
END
SET @BatchCounter +=1
COMMIT
END
DBCC showcontig ('TestGuid1') WITH tableresults
DBCC showcontig ('TestGuid2') WITH tableresults
SELECT batchNumber,DATEDIFF(ms,MIN(SomeDate),MAX(SomeDate)) AS [NEWID()]
FROM TestGuid1
GROUP BY batchNumber
SELECT batchNumber,DATEDIFF(ms,MIN(SomeDate),MAX(SomeDate)) AS [NEWSEQUENTIALID()]
FROM TestGuid2
GROUP BY batchNumber
DROP TABLE TestGuid1
DROP TABLE TestGuid2
我发现个别运行之间的结果差别很大(在我的笔记本电脑而不是服务器上!),但顺序更快的明确趋势。
NEWID()
平均5168.9
batchNumber NEWID()
-------------------- -----------
1 4270
2 2480
3 2706
4 3333
5 7480
6 5346
7 4306
8 7713
9 7313
10 4760
11 4680
12 4113
13 3433
14 2686
15 4963
16 8040
17 5313
18 8160
19 9533
20 2750
NEWSEQUENTIALID()
平均3000.85
batchNumber NEWSEQUENTIALID()
-------------------- -----------------
1 2016
2 1820
3 1886
4 1870
5 4873
6 3473
7 3730
8 3690
9 1983
10 2020
11 1906
12 5596
13 2100
14 1950
15 2096
16 1876
17 5196
18 2110
19 2113
20 7713
答案 1 :(得分:2)
自从我写了这篇原创博文后,我决定运行你的代码,这就是我得到的
3 8726 -- newid()
3 12550 -- newsequantialID
请记住,我在具有32 GB RAM和8个触发器的服务器上运行它,而不是在笔记本电脑上运行
在我的本地机器上,我几乎看不出两者之间有什么区别
请记住,除了插入之外,读取速度会慢得多,因为表格是碎片式的
这是我在服务器上运行Martin脚本时得到的结果
batchNumber NEWID()
17 1696
19 1706
14 1680
16 1706
5 1660
6 1890
7 1650
8 1663
13 1673
15 1683
2 1656
9 1673
20 1750
1 2033
3 1673
10 1673
12 1670
4 1650
11 1690
18 1696
batchNumber NEWSEQUENTIALID()
2 1276
9 1260
20 1290
13 1266
15 1280
17 1266
19 1266
5 1260
6 1266
7 1260
8 1260
1 1243
3 1256
10 1270
12 1263
14 1266
16 1276
4 1256
11 1270
18 1270
以下是我的桌面上发生的事情,文件大小不是BTW
batchNumber NEWID()
1 9470
2 4446
3 5996
4 3860
5 4170
6 2403
7 3283
8 3573
9 1883
10 3980
11 2580
12 2780
13 1643
14 2836
15 3250
16 4303
17 3250
18 3376
19 8723
20 2616
batchNumber NEWSEQUENTIALID()
1 2566
2 1336
3 1256
4 3123
5 3023
6 1166
7 2396
8 1180
9 2386
10 3896
11 3790
12 3066
13 1396
14 2010
15 1183
16 3110
17 4060
18 4260
19 1896
20 2013