---
title: "Guid.CreateVersion7() keys are not insert-ordered in a SQL Server clustered index"
url: "https://www.dariuszparys.com/notes/guid-createversion7-not-insert-ordered-in-sql-server"
author: "Dariusz Parys"
published: 2026-11-18T00:00:00.000Z
description: "SQL Server sorts uniqueidentifier values by their last six bytes first, so version 7 GUIDs still land on random pages of a clustered index."
---

# Guid.CreateVersion7() keys are not insert-ordered in a SQL Server clustered index

## Context

Random GUID primary keys (`Guid.NewGuid()`) on a clustered index scatter inserts across pages: page splits, fragmentation, and a wide key copied into every nonclustered index. The usual suggested fix is `Guid.CreateVersion7()`, because v7 GUIDs put a timestamp first.

## Insight

On SQL Server that doesn't help. SQL Server sorts `uniqueidentifier` values by the **last 6 bytes first**, so the leading timestamp of a v7 GUID barely affects index order. New rows still land on random pages.

For insert-ordered keys on SQL Server:

- Don't set the `Id` in application code. Let EF Core assign it on insert: its default key generator on SQL Server, `SequentialGuidValueGenerator`, is ordered the way SQL Server sorts.
- Or give the column a `NEWSEQUENTIALID()` default.

It's only worth acting on fast-growing tables, roughly a million rows or more.

```csharp
// Before: random, and v7 is no better on SQL Server
var entry = new Entry { Id = Guid.NewGuid(), ... };

// After: leave Id unset; EF Core generates a sequential GUID on Add
var entry = new Entry { ... };

// Or let the database assign it
builder.Property(e => e.Id).HasDefaultValueSql("NEWSEQUENTIALID()");
```
