📁 Sangam Dhakal - Portfolio v2.0 - Netscape Navigator 4.0
_
□
×

📚 Interview question

Why is a random GUID a poor clustered index key in SQL Server, and how would you measure the damage?

Mid-Level Theory Intermediate

🎯 Key points

  • Clustered index = rows stored in key order
  • Random keys → inserts on random pages → page splits and logged moves
  • Low page density (~50–70%) and fragmentation → more pages, more I/O, more buffer pool
  • Measure: sys.dm_db_index_physical_stats (fragmentation, page density, page count)
  • Page Splits/sec, log growth, insert latency in Query Store
  • Alternatives: IDENTITY/bigint, NEWSEQUENTIALID(), EF Core sequential GUID generator, Guid.CreateVersion7()
  • Or random GUID as non-clustered unique key with a narrow clustered key
  • Wide 16-byte key is copied into every non-clustered index

⏱ 30-second answer

A clustered index stores rows in key order. A random Guid.NewGuid() key sends every insert to a random page, so full pages split (half the rows move to a new page, both writes are logged), page density drops towards ~50–70%, and the index is fragmented. More pages means more I/O and buffer-pool memory for every scan and range seek, and inserts touch pages spread over the whole index instead of a hot last page that stays in cache. Measure it with sys.dm_db_index_physical_stats (avg_fragmentation_in_percent, avg_page_space_used_in_percent, page_count), the Page Splits/sec counter, transaction log growth and insert latency in Query Store. Fixes: an IDENTITY/bigint clustered key, sequential GUIDs (EF Core's default SequentialGuidValueGenerator for SQL Server, NEWSEQUENTIALID(), or Guid.CreateVersion7()), or keep the random GUID as a non-clustered unique key and cluster on something else.

🗣 Full answer (senior)

How I would answer this in an interview

"The clustered index is the table, ordered by its key. With an increasing key, inserts land on the last page; it stays in memory, fills to 100% and the next page is allocated. With Guid.NewGuid(), each insert goes wherever the random value falls. If that page is full, SQL Server splits it: allocates a new page, moves about half the rows, and logs all of it. Over time pages sit around half to two-thirds full and the logical order no longer matches the physical order.

The damage shows up in three places. Inserts are slower and generate more log. Reads touch more pages, because the same rows are spread over more of them, and range scans can't read ahead efficiently. And the buffer pool holds more mostly-empty pages, so other data gets evicted. The key is also 16 bytes and every non-clustered index carries it.

To measure, I'd look at sys.dm_db_index_physical_stats for fragmentation, page density and page count, compare with a sequential key, watch Page Splits/sec and log write volume, and check insert duration in Query Store. The fixes are an identity or sequence key, a sequential GUID (EF Core already generates sequential GUIDs client-side for SQL Server key properties, and Guid.CreateVersion7() is time-ordered), or keeping the random GUID as a non-clustered public identifier."


Measuring

SELECT index_id, avg_fragmentation_in_percent, avg_page_space_used_in_percent, page_count
FROM sys.dm_db_index_physical_stats(DB_ID(), OBJECT_ID('dbo.Orders'), NULL, NULL, 'SAMPLED');

Options

Key Insert pattern Size Notes
int/bigint IDENTITY Append 4/8 bytes Last-page contention only at very high insert rates
Sequential GUID Mostly append 16 bytes Generated client-side, globally unique
Random GUID clustered Random 16 bytes Splits, low density
Random GUID non-clustered + identity clustered Append on clustered 16 + 8 bytes Public id without the split cost

Production example

An Events table with random GUID keys reached [99%] fragmentation and [58%] page density at [40 million] rows; switching new rows to sequential keys and rebuilding cut its size by [~35%] and insert p95 from [25 ms] to [6 ms].

🔍 What the interviewer is checking

Checks link between key design and storage engine behaviour.

↩ Usually asked after

More in EF Core Fundamentals

Last updated 23 Nov 2026.

🌐 Sangam's Webring

⏪ Previous 🏠 Main Next ⏩
💬 Chat with me