Comparison
UUID v7 vs UUID v4
Version 4 hides when it was created. Version 7 puts the millisecond in the identifier, which changes sort order and can change how an index grows.
| UUID v4 | UUID v7 | |
|---|---|---|
| Specification | RFC 9562 version 4 | RFC 9562 version 7 |
| Time | None | 48-bit Unix milliseconds |
| Random bits | 122, after version and variant | 74 on this site (12 + 62) |
| Text sort | Not ordered by time | Ordered by millisecond |
| Same millisecond | Not applicable | Random, not a counter, on this site |
| Reveals a clock | No | Yes, to millisecond precision |
A B-tree on random version 4 keys tends to insert all over the index. Version 7 keys from a moving clock tend to arrive toward the recent side. That can mean fewer scattered page splits, but it depends on the table, the fill factor, and how many writers you have. It is not a guarantee that queries get faster.
Because the time is visible, do not put a v7 in a place where the creation time itself is sensitive. Neither version is a secret.
Questions
Which one is random?
Version 4 uses random bits apart from the version and variant. Version 7 on this site uses a millisecond timestamp plus 74 random bits.
Which one sorts by time?
Only version 7 puts time in the high bits, so the canonical text sorts by millisecond. Version 4 does not.
Does v7 replace v4?
No. Version 4 remains a normal choice when you do not want the time to be visible in the identifier.