الانتقال إلى المحتوى
قواعد البياناتSQL Server· 7 دقائق للقراءة

Why UUIDv7 Keys Still Fragment SQL Server Clustered Indexes

UUIDv7 is ordered from its first byte; SQL Server sorts uniqueidentifier values from the last six. To SQL Server, a UUIDv7 key arrives in random order.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
  • SQL Server 2025
محتويات المقال

A team moves to .NET 9 and replaces 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

The belief: a time-ordered GUID like UUIDv7 is always insert-friendly as a clustered primary key.

That's what the standard promises. RFC 9562 says UUIDv7 is designed so that implementations that require sorting, database indexes included, can sort the values as opaque raw bytes, and that time-ordered values give better index locality because new values land near each other. PostgreSQL 18 ships a 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

RFC 9562 defines UUIDv7 as a 48-bit big-endian Unix timestamp in milliseconds in the first six bytes, then the version, 12 random bits, the variant, and 62 more random bits filling bytes 8 to 15. In text, the first 12 hex digits are the timestamp: in the test, 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

Microsoft's ADO.NET docs state that SQL Server compares 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

Each new key lands at a random spot in the clustered index, usually inside a page that's already full, so the engine splits it in two. That's the cost the earlier article in this path measured for random-order indexes, and here it hits the table itself. Microsoft's index design guide lists the properties of a good clustered key, among them narrow and ever-increasing, and notes that a 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.

Clustered key, 200,000 single-row inserts (SQL Server 2022)Leaf pagesAvg. page fullnessFragmentationLog written
bigint IDENTITY4,08299.2%0.4%56 MB
NEWID()6,16768.9%99.2%87 MB
Guid.CreateVersion7() from .NET6,19368.6%99.2%87 MB
NEWSEQUENTIALID()4,25699.8%0.7%57 MB
EF Core's sequential GUID generator4,25699.8%0.7%57 MB

Each key was inserted one row at a time, in the order it was generated: the .NET values were generated first, then replayed in that order.

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

C#
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% full

When the GUID has to be a UUIDv7, because other systems or URLs depend on it, keep it out of the clustered key:

SQL orders.sql
CREATE 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);

In the test with UUIDv7 values in 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

  1. Find GUID clustered keysList tables whose clustered index leads with a uniqueidentifier, and check how their values are generated: NEWID(), Guid.NewGuid(), Guid.CreateVersion7(), or something sequential.
  2. Measure the damageQuery sys.dm_db_index_physical_stats for avg_fragmentation_in_percent and avg_page_space_used_in_percent on those indexes. Pages far below full on a table that only receives inserts point to random keys.
  3. Pick a key SQL Server sorts in orderLet EF Core generate GUID keys, use NEWSEQUENTIALID() as a default, or cluster on a bigint IDENTITY and keep the GUID in a unique nonclustered index.
  4. Keep UUIDv7 where it fitsUse 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 uniqueidentifier values starting from the last six.
  • In the test, Guid.CreateVersion7() keys fragmented a clustered index exactly like NEWID(): 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 SqlGuid in tests and with sys.dm_db_index_physical_stats in production.

📚 المصادر

  1. Comparing GUID and uniqueidentifier Values — ADO.NET, Microsoft Learn
  2. NEWSEQUENTIALID (Transact-SQL) — Microsoft Learn
  3. Guid.CreateVersion7 Method — .NET API, Microsoft Learn
  4. RFC 9562: Universally Unique IDentifiers (UUIDs) — RFC Editor
  5. SQL Server Provider: Value Generation — EF Core, Microsoft Learn

التعليقات (0)