Every database table needs an ID, and "just use a UUID" is common advice. But which version? The differences matter for performance, privacy and design. Here is a practical comparison of v4, v7 and v5, with v1 and v6 mentioned briefly.
UUID v4: random
A v4 UUID is 128 bits, of which 122 are random (the rest mark the version and variant). Collisions are astronomically unlikely, and you can generate them anywhere without coordination.
The catch is ordering. Random values land all over a B-tree index. On insert-heavy tables, every new row can touch a different index page, which causes page splits, more cache misses and a bloated index.
UUID v7: time-ordered
v7 is defined in RFC 9562 (2024). It starts with a 48-bit Unix timestamp in milliseconds, followed by random bits. IDs generated later sort after earlier ones, so inserts append near the end of the index much like an auto-increment key would.
Trade-off: a v7 ID reveals roughly when the row was created. That is harmless for many systems, but think twice before exposing it in public URLs if creation time is sensitive.
UUID v5: deterministic
v5 is the SHA-1 hash of a namespace UUID plus a name. The same input always gives the same UUID, which is handy for stable IDs derived from a URL, an email or an external key, and for idempotent imports.
It is not random and not secret: anyone who knows the namespace and the name can recompute it. Do not use it for tokens or anything that must be unguessable.
import uuid
print(uuid.uuid4())
print(uuid.uuid5(uuid.NAMESPACE_URL, 'https://example.com'))
# Same input, same output, every time
v1 and v6, briefly
v1 mixes a timestamp with a MAC address (a privacy leak) and its bytes are not in sortable order. v6 reorders v1's timestamp so it sorts, but v7 is generally the better choice for new designs.
Practical advice
- New table, high insert rate, IDs generated by your app or database: prefer v7 if your language or database supports it.
- IDs that must not leak creation time, or are used as unguessable identifiers: use v4.
- Stable ID derived from existing data: use v5, and keep the namespace constant.
- Store UUIDs in a native uuid or 16-byte binary column, not a 36-character string, to save space and speed up comparisons.
- Never treat any UUID as a security secret on its own; use proper session or API tokens for that.
None of these is "best" in general. Pick by what the ID needs to do: be unique, be sortable, or be reproducible.
Disclosure: I run GoToolsOnline, which has a free UUID generator for v4, v7 and v5 in bulk, with a validator: https://gotoolsonline.com/tools/uuid-generator
Top comments (0)