I'm in the middle of doing a bit of research, and I've come across an anomaly that I can't explain (and I've not been able to find anything on Google). Consider the following SQL:
CREATE TABLE MyGuid (
ID UNIQUEIDENTIFIER PRIMARY KEY DEFAULT NEWID()
)
GO
CREATE TABLE MyGuidSeq (
ID UNIQUEIDENTIFIER PRIMARY KEY DEFAULT NEWSEQUENTIALID()
)
GO
DECLARE @i INT, @noRecords INT
SET @noRecords = 1000000
-- MyGuid
SET @i = 1
WHILE (@i <= @noRecords)
BEGIN
INSERT INTO MyGuid DEFAULT VALUES
SET @i = @i + 1
END
-- MyGuidSeq
SET @i = 1
WHILE (@i <= @noRecords)
BEGIN
INSERT INTO MyGuidSeq DEFAULT VALUES
SET @i = @i + 1
END
exec sp_spaceused 'MyGuid', true
exec sp_spaceused 'MyGuidSeq', true
Results:
Table name No. Rows Reserved Space Actual space Index Size Unused Space
MyGuid 1,000,000 34,760 KB 34,552 KB 160 KB 48 KB
MyGuidSeq 1,000,000 24,968 KB 24,768 KB 176 KB 24 KB
Question
Can anyone explain why the reserved / actual space is considerably smaller when using NEWSEQUENTIALID() over NEWID()?
In Response To Answer
I ran the following test to check on the answer I was given by Luaan below:
CREATE TABLE MyGuid (
ID UNIQUEIDENTIFIER DEFAULT NEWID()
)
GO
CREATE TABLE MyGuidSeq (
ID UNIQUEIDENTIFIER DEFAULT NEWSEQUENTIALID()
)
GO
DECLARE @i INT, @noRecords INT
SET @noRecords = 1000000
-- MyGuid
SET @i = 1
WHILE (@i <= @noRecords)
BEGIN
INSERT INTO MyGuid DEFAULT VALUES
SET @i = @i + 1
END
-- MyGuidSeq
SET @i = 1
WHILE (@i <= @noRecords)
BEGIN
INSERT INTO MyGuidSeq DEFAULT VALUES
SET @i = @i + 1
END
exec sp_spaceused 'MyGuid', true
exec sp_spaceused 'MyGuidSeq', true
The difference being is that I've removed the primary key (meaning the table is now a heap table). This now results in exactly the same sizes for both tables. This proves that the table is physically organized by its clustered index.
name rows reserved data index_size unused
MyGuid 1000000 25992 KB 25976 KB 8 KB 8 KB
MyGuidSeq 1000000 25992 KB 25976 KB 8 KB 8 KB
NEWSEQUENTIALID
? "You can use NEWSEQUENTIALID() to generate GUIDs to reduce page contention at the leaf level of indexes." – Arraignment