
Why UUIDv7 Keys Still Fragment SQL Server Clustered Indexes
- كاتب موثّق
- SQL Server 2025
محتويات المقال
Guid.NewGuid() with Guid.CreateVersion7() for every primary key, because time-ordered GUIDs are supposed to be index-friendly. A month later, the orders table's is as fragmented as before. In a test on SQL Server 2022, 200,000 single-row inserts with CreateVersion7() keys left the index 99.2% fragmented, its pages 68.6% full on average, almost identical to NEWID(). NEWSEQUENTIALID() on the same table left it 0.7% fragmented with pages 99.8% full.
The common misconception
uuidv7() function its docs call time-ordered. Even a quick check in C# agrees: sort CreateVersion7() values with Guid's own comparison and they come out in time order. Nothing warns you that one engine reads the bytes from the other end.
What actually happens
What UUIDv7 puts where
01a1a2f6-cc28-763a-... was generated 246 ms before 01a1a2f6-cd1e-7193-.... Microsoft's docs for Guid.CreateVersion7 say it follows RFC 9562's version 7 format, takes the time from DateTimeOffset.UtcNow, and fills the rand_a and rand_b fields with random data.
SQL Server reads it from the other end
uniqueidentifier values with the last six bytes most significant, and their example sorts three GUIDs by their last group alone. The test showed the full order: the last group first, then the fourth, then the third, second and first, so the group where the timestamp starts is compared last. Microsoft documents this comparison for every version up to SQL Server 2025, whose list of new features doesn't change it; the runs here were on SQL Server 2022. So SQL Server orders a UUIDv7 by rand_b first: random. Sorting those two test values, SQL Server returned the later one first, and the three lowest keys in its order had been generated 171,910th, 175,342nd and 51,983rd.
Random positions mean page splits
uniqueidentifier takes 16 bytes instead of 4 for int or 8 for bigint, and isn't ever-increasing unless the values are generated sequentially. "Sequentially" means in SQL Server's order, not the RFC's.
bigint IDENTITY | ||||
NEWID() | ||||
Guid.CreateVersion7() from .NET | ||||
NEWSEQUENTIALID() | ||||
What is sequential for SQL Server
NEWSEQUENTIALID() returns a GUID greater than any it generated on that computer since Windows started, and in the test "greater" held in SQL Server's order. It can only be a column default, and after a restart it can start again from a lower range. EF Core's SQL Server provider does the same on the client: for GUID primary keys it generates sequential values, similar to NEWSEQUENTIALID, and the values it produced in the test kept their increasing part in the last eight bytes. That's why the two rows in the table match.
Generate keys in the order the engine sorts
public class Order{ // Time-ordered per RFC 9562, but SQL Server // compares the random last six bytes first public Guid Id { get; init; } = Guid.CreateVersion7(); public DateTimeOffset CreatedAt { get; init; }}// Tested: 99.2% fragmented, pages 68.6% fullpublic class Order{ // No initializer: EF Core's SQL Server provider // generates GUIDs sequential in SQL Server's order public Guid Id { get; set; } public DateTimeOffset CreatedAt { get; init; }}// Tested with its generator: 0.7% fragmented, pages 99.8% fullCREATE TABLE dbo.Orders ( Id bigint IDENTITY CONSTRAINT PK_Orders PRIMARY KEY CLUSTERED, PublicId uniqueidentifier NOT NULL CONSTRAINT UQ_Orders_PublicId UNIQUE NONCLUSTERED, CreatedAt datetime2(3) NOT NULL);PublicId, the table stayed 0.5% fragmented and 97.8% full. The splits didn't disappear, and the log written was about the same, 89 MB: they moved to a 1,030-page unique index, where a random order costs far less than in the table.
How to apply it in your project
Find GUID clustered keys List tables whose clustered index leads with a uniqueidentifier, and check how their values are generated:NEWID(),Guid.NewGuid(),Guid.CreateVersion7(), or something sequential.Measure the damage Query sys.dm_db_index_physical_statsforavg_fragmentation_in_percentandavg_page_space_used_in_percenton those indexes. Pages far below full on a table that only receives inserts point to random keys.Pick a key SQL Server sorts in order Let EF Core generate GUID keys, use NEWSEQUENTIALID()as a default, or cluster on abigint IDENTITYand keep the GUID in a unique nonclustered index.Keep UUIDv7 where it fits Use it where values are compared as bytes in RFC order, or where nothing is clustered on it. In SQL Server, treat it like a random GUID.
Key takeaways
UUIDv7 is time-ordered from its first byte, and SQL Server compares uniqueidentifiervalues starting from the last six.In the test, Guid.CreateVersion7()keys fragmented a clustered index exactly likeNEWID(): 99.2%, pages 68.6% full.NEWSEQUENTIALID()and EF Core's default GUID generator are sequential in SQL Server's order: 0.7%, pages 99.8% full.If you need UUIDv7, cluster on a narrow increasing key and put the GUID in a unique nonclustered index. Check generated keys with SqlGuidin tests and withsys.dm_db_index_physical_statsin production.
التعليقات (0)