Menu

A Million v7 GUIDs and Half Were Still Out of Order
📰

A Million v7 GUIDs and Half Were Still Out of Order

DEV Community: dotnet·Sukhpinder Singh·about 15 hours ago
#OAh68KFY
#devtodotnet#devto#dev#guid#order
Reading 0:00
15s threshold

I moved a table's primary key off random GUIDs a little while back. The reason is the usual one: a clustered index hates random inserts. Every new row wants a spot in the middle of the index instead of the end, pages split, the thing fragments, and your inserts get slower the bigger the table gets. Version 7 GUIDs are the tidy fix. They carry a millisecond timestamp in their leading bytes, so new keys sort roughly in creation order and land near the end where the index wants them.

Before I trusted that in production I wanted to see the ordering with my own eyes. So I did the dumbest thing that would show it: generate a pile of keys and count how many came out in order. The answer made me feel a bit silly.

What v7 actually promises

A v7 GUID isn't random all the way down. The first 48 bits are a Unix millisecond timestamp, then a handful of version and variant bits, then the rest is random. Two keys created in different milliseconds sort by those timestamps.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.