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
Idin 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.
// Before: random, and v7 is no better on SQL Servervar entry = new Entry { Id = Guid.NewGuid(), ... };
// After: leave Id unset; EF Core generates a sequential GUID on Addvar entry = new Entry { ... };
// Or let the database assign itbuilder.Property(e => e.Id).HasDefaultValueSql("NEWSEQUENTIALID()");