Menu

UUID v4 vs v7: differences, database speed and which to pick
📰

UUID v4 vs v7: differences, database speed and which to pick

DEV Community: postgres·Günther Hintringer·1 day ago
#yjmmWZMw
#devtopostgresql#devto#dev#random#time
Long article (975 words) — no AI summary yet.
Reading 0:00
15s threshold

Both are 128-bit UUIDs that look alike and fit the same columns. The difference is the first 48 bits: random in v4, the creation time in v7. That one change decides how they behave in a database and what they reveal.

The short answer

Use v7 for database keys and for anything you want to sort by creation time. Use v4 when an ID must not reveal when it was made, for example in a public link. Both are unique enough that you never have to worry about duplicates.

If you can't decide, take v7. RFC 9562, the current UUID standard, recommends it for new systems.

Side by side

v4 v7

Content 122 random bits 48-bit Unix time in milliseconds, then random bits

Random bits 122 62 to 74, depending on the generator

Sorts by creation time No Yes

Shows when it was made No Yes, to the millisecond

New rows in a B-tree index Anywhere At the end

Defined in RFC 4122 (2005), now RFC 9562 RFC 9562 (2024)

Built in Almost everywhere Newer versions, for example PostgreSQL 18, .NET 9, Python 3.14

What the two look like

v4…

Continue reading — create a free account

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