Skip to main content

Guid.CreateVersion7() is NOT a sequential guid for SQL Server

I think that is the clearest blog title I used in years. Why do I mention this? Let me explain...

Some time ago we stumbled over a performance issue in our applications. The root cause was a fragmented index, caused by the usage of a standard guid instead of a sequential guid.

While looking for the right fix, I had to revisit one of my own posts: Sequential GUIDs with .NET 9. In that post I mentioned that you can use Guid.CreateVersion7() to create a sequential guid. That is technically correct, but it is NOTa solution for SQL Server.

Why does a random guid hurt?

A clustered index in SQL Server is a sorted B-tree. When the key is random, every insert lands on a random page. If that page is full, SQL Server has to split it, which leaves you with half-empty pages, a fragmented index and more I/O. A sequential key always appends at the end, so pages fill up and stay put.

Why is a version 7 guid not sequential for SQL Server?

A UUID version 7 (RFC 9562) starts with a 48-bit Unix timestamp in milliseconds. The rest is random. Here are three values, generated a few milliseconds apart:

01a0ba47-e8d0-7198-9f36-28a04dcb9a21
01a0ba47-e8d6-771b-979a-a1ccc7581c77
01a0ba47-e8d9-7621-8d8b-bf27581ec544

Read from left to right, they are in creation order. A database that compares a uuid byte by byte from left to right (PostgreSQL, for example) appends these values at the end of the index. SQL Server doesn't.

SQL Server does not compare a uniqueidentifier from left to right. The byte numbers below refer to the layout you get from Guid.ToByteArray(), which is also how SQL Server stores the value. From most to least significant, SQL Server compares:

  • bytes 10-15 (last group): random
  • bytes 8-9 (fourth group): variant + random
  • bytes 6-7 (third group): version + random
  • bytes 4-5 (second group): lower part of the timestamp
  • bytes 0-3 (first group): upper part of the timestamp

The timestamp ends up in the least significant part of the sort key, and the random bits in the most significant part. From SQL Server's perspective, a v7 guid behaves like a random guid.

What are the options?

  • Let EF Core generate the id. For a Guid primary key, the SQL Server provider uses SequentialGuidValueGenerator by default, which generates guids optimized for clustered keys in SQL Server. If you assign Guid.CreateVersion7() yourself in your code, you bypass that default.
  • Let SQL Server generate the id through NEWSEQUENTIALID():
CREATE TABLE Orders
(
    Id UNIQUEIDENTIFIER NOT NULL DEFAULT NEWSEQUENTIALID() PRIMARY KEY,
    OrderDate DATETIME2 NOT NULL
);
  • Generate the id yourself, in SQL Server order. Libraries like RT.Comb do this. SequentialGuid offers GuidV7.NewSqlGuid(), which rearranges the bytes to match SQL Server's sorting rules. You can also do the reshuffling yourself.

Sidetrack: Reshuffling a version 7 guid

The idea is simple: what if we could still generate a v7 guid and move the bytes around, so the timestamp lands in the bytes SQL Server looks at first.

public static Guid CreateVersion7ForSqlServer()
{
    Span<byte> rfc = stackalloc byte[16];
    Guid.CreateVersion7().TryWriteBytes(rfc, bigEndian: true, out _);

    Span<byte> sql = stackalloc byte[16];
    // SQL Server compares bytes 10-15 first, then 8-9, 6-7, 4-5 and finally 0-3
    rfc[0..6].CopyTo(sql[10..]);   // 48-bit timestamp becomes the most significant part
    rfc[6..8].CopyTo(sql[8..]);
    rfc[8..10].CopyTo(sql[6..]);
    rfc[10..12].CopyTo(sql[4..]);
    rfc[12..16].CopyTo(sql[0..]);
    return new Guid(sql);
}

new Guid(ReadOnlySpan<byte>) uses the same byte layout as SQL Server, so the value you generate is the value you find in the table. The timestamp now sits in the last group:

59ad233b-03fc-d091-77b3-01a0ba47e8dc
a16717c9-bf50-85a7-7aa6-01a0ba47e8df
68d1fa99-9500-90b4-72c8-01a0ba47e8e2

Let's see this in action

I simulated a clustered index that receives one insert per millisecond, and counted how often a new value sorts after all previous values (meaning it is appended at the end of the index). I used SqlGuid for the comparison, as it follows SQL Server's ordering rules:

static double PercentAtEnd(Func<Guid> generate, int count = 5_000)
{
    var max = new SqlGuid(generate());
    var atEnd = 0;
    for (var i = 0; i < count; i++)
    {
        Thread.Sleep(1); // roughly one insert per millisecond
        var next = new SqlGuid(generate());
        if (next > max) { max = next; atEnd++; }
    }
    return 100.0 * atEnd / count;
}

Console.WriteLine($"Guid.NewGuid():         {PercentAtEnd(Guid.NewGuid):F1}%");
Console.WriteLine($"Guid.CreateVersion7():  {PercentAtEnd(Guid.CreateVersion7):F1}%");
Console.WriteLine($"Reshuffled version 7:   {PercentAtEnd(CreateVersion7ForSqlServer):F1}%");

The result:

Guid.NewGuid():         0.1%
Guid.CreateVersion7():  0.2%
Reshuffled version 7:   100.0%

Guid.CreateVersion7() is no better than a random guid here. The reshuffled version always appends.

Remark: The reshuffled value is no longer a valid UUIDv7 in its text form. Don't hand it to systems that expect the RFC 9562 layout or read the timestamp from it.

Not something that I would use in my applications, but a good test to understand why the CreateVersion7() is not what we need.

Check your own indexes

Changing the id generation only fixes new inserts. Existing fragmentation stays until you rebuild the index:

SELECT i.name, ps.avg_fragmentation_in_percent, ps.avg_page_space_used_in_percent, ps.page_count
FROM sys.dm_db_index_physical_stats(DB_ID(), OBJECT_ID('dbo.Orders'), NULL, NULL, 'SAMPLED') AS ps
JOIN sys.indexes AS i ON i.object_id = ps.object_id AND i.index_id = ps.index_id;

ALTER INDEX ALL ON dbo.Orders REBUILD;

That's it! Sequential is only sequential for the system that does the sorting. For SQL Server, that is not the order of RFC 9562.

More information

Sequential GUIDs with .NET 9

Avoiding index fragmentation with sequential guids

Using GuidCOMB in SQL Server and PostgreSQL

SequentialGuidValueGenerator Class | Microsoft Learn

NEWSEQUENTIALID (Transact-SQL) | Microsoft Learn

Add API to correctly insert GuidV7 to SQLServer · dotnet/SqlClient Discussion #2999

GitHub - richardtallent/RT.Comb

SequentialGuid/README.md at master · buvinghausen/SequentialGuid

Popular posts from this blog

Podman– Command execution failed with exit code 125

After updating WSL on one of the developer machines, Podman failed to work. When we took a look through Podman Desktop, we noticed that Podman had stopped running and returned the following error message: Error: Command execution failed with exit code 125 Here are the steps we tried to fix the issue: We started by running podman info to get some extra details on what could be wrong: >podman info OS: windows/amd64 provider: wsl version: 5.3.1 Cannot connect to Podman. Please verify your connection to the Linux system using `podman system connection list`, or try `podman machine init` and `podman machine start` to manage a new Linux VM Error: unable to connect to Podman socket: failed to connect: dial tcp 127.0.0.1:2655: connectex: No connection could be made because the target machine actively refused it. That makes sense as the podman VM was not running. Let’s check the VM: >podman machine list NAME         ...

Cache stampede: when our cache turned against us

While investigating some performance issues, we ran into an ASP.NET Core API that cached a fairly expensive aggregation query for 60 seconds. Under normal load, that was fine: one request rebuilds the cache, everyone else reads from it. Under peak load, dozens of requests would arrive in that same expiry window, all see a cache miss, and all fire the same expensive query in parallel. The database didn't like that. That was the moment when our caching layer stopped helping and started hurting. A burst of requests comes in at the same time, all miss the cache, and all go hammer the database or the downstream API at once. That's a cache stampede . The cache was supposed to protect our backend, and for a few hundred milliseconds it did the opposite. Why this happens IMemoryCache.GetOrCreate (and its async sibling) looks like it protects you, but it doesn't add any locking on its own. Look at the naive version: public async Task<Report> GetReportAsync(string key) ...

The role of ActivitySource in OpenTelemetry for .NET

While doing some pair programming to integrate OpenTelemetry tracing to a .NET application, we had a discussion on how to use the ActivitySource . It looks simple. You new one up, give it a name, start an activity, done. The discussion started when we added a second ActivitySource with the exact same name in a different class. This made us wonder: "Are we duplicating traces now? Is this a memory leak? Do we need a singleton?" So we decided to dig deeper. This post is what we learned… What ActivitySource actually is ActivitySource is part of System.Diagnostics , not part of the OpenTelemetry NuGet packages. Microsoft built tracing primitives directly into the BCL, and OpenTelemetry's .NET SDK simply listens to them. This is why you can add distributed tracing to a library without taking a dependency on OpenTelemetry at all. An ActivitySource is a factory for Activity objects, and an Activity is .NET's name for what OpenTelemetry calls a span.(don’t ask m...