If you've used UUIDs as database keys, you've probably used v4: 122 random bits, no meaning, no order. That's great for "nobody can guess the next one" and terrible for one thing: index locality.

Why random keys hurt B-tree indexes

Most databases store the primary key in a B-tree. Sequential keys always go to the right-most page, so inserts are cheap and pages stay full. Random v4 keys land anywhere, so the database keeps touching old pages, splitting them, and your index grows bigger and colder in cache than it needs to be. On small tables you won't notice. On tens of millions of rows with heavy inserts, you will.

What v7 changes

UUID v7 (RFC 9562, 2024) puts a 48-bit Unix timestamp in milliseconds at the front, then random bits. So:

  • new IDs sort roughly by creation time
  • inserts go near the end of the index, like an auto-increment
  • you still get enough randomness to generate IDs on many machines without coordination

Bonus: you can read the creation time back out of the ID, which is handy when debugging.

When to still use v4

  • the ID is public and you don't want to leak when a record was created
  • you need IDs that reveal nothing at all (tokens, invite codes – though for secrets, use a proper random token, not a UUID)

And v5?

v5 is deterministic: the same namespace + name always gives the same UUID (SHA-1 based). Useful for "the ID for this URL/email should always be the same" without storing a lookup table.

Quick rules

  • Database primary key, internal: v7
  • Public-facing ID where creation time is sensitive: v4
  • Stable ID derived from a name: v5
  • Store as a native uuid / binary(16) column, not a 36-char string, if your database supports it.

Disclosure: I run GoToolsOnline. While writing this I used our free UUID generator, which makes v4, v7 and v5 (up to 1,000 at once) and has a checker that shows a UUID's version, variant and, for v7, its timestamp: https://gotoolsonline.com/tools/uuid-generator