Insert a few million rows with random primary keys and the database slows down. The CPU is idle, the disk is not saturated — the index is the problem. Every insert lands on a random page of a B-tree that would rather grow at the end.
The layouts
A UUID is 128 bits, written as 36 characters with hyphens. Version 4 fills 122 of those bits with randomness; the rest are version and variant markers. Version 7, standardized in RFC 9562, spends the first 48 bits on a Unix timestamp in milliseconds and keeps 74 bits for randomness.
| UUID v4 | UUID v7 | |
|---|---|---|
| Layout | 122 random bits | 48-bit ms timestamp + 74 random bits |
| 13th character | 4 |
7 |
| Sort order | none | time order, millisecond precision |
| Insert pattern | random pages across the index | appends near the end |
| Reveals creation time | no | yes, to the millisecond |
crypto.randomUUID() |
generates it | does not |
Spot the version in one character
The 13th hex character — index 14, counting from zero — is the version:
const { randomUUID } = require('node:crypto');
const id = randomUUID();
console.log(id); // 9f1c2a7e-3b4d-4e6f-8a1b-2c3d4e5f6a7b
console.log(id[14]); // '4' — version 4
What v7 buys, what it costs
You get index locality: inserts cluster at the end of the tree instead of scattering, which means fewer page splits and less bloat on large tables. You also get cursor pagination for free — sorting by ID approximates sorting by creation order, so WHERE id > $cursor ORDER BY id behaves like a chronological feed.
You pay in privacy and precision. A v7 ID tells anyone holding it when the row was created, down to the millisecond — order volume, signup time, all of it. And IDs generated in the same millisecond are still ordered randomly among themselves, so v7 is not a strict event sequence.
What the generator actually produces
The GUID/UUID generator calls crypto.randomUUID(), which is version 4 only — one ID per click, generated locally in your browser. Check the 13th character before you assume otherwise. If you need v7, you build it in application code, because crypto.randomUUID() cannot emit one.
Neither version is a security token. 122 random bits are not guessable, but an ID is an identifier, not an authorization check — enforce access control on the server, not by keeping URLs secret.
For append-heavy tables that are read in ID order, v7 is the better default. For sparse lookups, or when creation time itself is sensitive, stay with v4.