UUID v4 generates 122 purely random bits so IDs reveal nothing about creation time or order, whereas UUID v7 (standardized in RFC 9562) puts a 48-bit Unix millisecond timestamp in front of 74 random bits so IDs sort chronologically. For database primary keys, UUID v7 avoids the B-tree index fragmentation that slows down UUID v4 inserts at scale.
You can generate up to 50 UUID v4 values at once and validate any UUID format with our free UUID Generator & Validator. Below is the exact bit-level comparison under RFC 9562, how each version affects PostgreSQL and MySQL B-tree indexes, and when an auto-incrementing integer is still the better key.
UUID v4 vs. UUID v7 under RFC 9562
Both UUID v4 and UUID v7 are 128-bit identifiers (16 bytes stored in binary, or 36 characters in canonical hyphenated hex form 8-4-4-4-12). The difference lies entirely in how those 128 bits are allocated.
| Property | UUID v4 | UUID v7 (RFC 9562) |
|---|---|---|
| Bit layout | 122 random bits + 6 version/variant bits | 48-bit Unix ms timestamp + 74 random/counter bits + 6 version/variant bits |
| Chronological sorting | No (purely random order) | Yes (lexicographic and binary order match creation time) |
| B-tree index locality | Poor at high volume (random page inserts and splits) | High (append-mostly inserts at the right edge of the B-tree) |
| Timestamp privacy | Leaks zero timing information | Exposes creation timestamp to millisecond precision |
| Storage size | 16 bytes (UUID/BINARY(16)) or 36 bytes (CHAR(36)) |
16 bytes (UUID/BINARY(16)) or 36 bytes (CHAR(36)) |
| Best suited for | Public tokens, opaque URLs, security-sensitive identifiers | Database primary keys, event logs, distributed tables |
Why UUID v4 slows down B-tree indexes at scale
Relational databases such as PostgreSQL and MySQL (InnoDB) store primary keys in B-tree structures ordered by key value. When you insert sequential keys (like auto-incrementing integers or UUID v7), new rows land cleanly on the rightmost leaf page, which stays hot in memory.
When you insert random UUID v4 values into a large table with millions of rows, every new row lands on a random page across the entire index. That forces random disk reads, fills pages unevenly and triggers frequent page splits and write amplification. Switching primary keys to UUID v7 keeps distributed client-side generation while restoring sequential B-tree append behavior.
How to pick the right primary key strategy
- Use a 64-bit auto-incrementing integer (
BIGINT) for internal single-database tables. At 8 bytes instead of 16, it is half the size of a UUID, keeps secondary indexes smaller and is effortless to debug. - Use UUID v7 when you need globally unique, client-generated or distributed primary keys. It merges across shards without coordination and keeps B-tree inserts sequential.
- Use UUID v4 for public, unguessable URLs and opaque tokens. If exposing the exact millisecond a user signed up or an invoice was created matters for privacy or business secrecy, pair an internal integer or UUID v7 primary key with a separate public UUID v4 slug (or cryptographically random token).
- Store UUIDs as native binary types whenever possible. Use PostgreSQL's native
uuidtype (16 bytes) or MySQL'sBINARY(16)instead of a 36-character UTF-8VARCHAR(36)column to cut index memory usage by more than half. - Inspect timestamps and validate strings during debugging. When working with millisecond Unix timestamps inside UUID v7 or API payloads, verify epoch dates with our Timestamp Converter and check UUID formatting with the UUID Generator & Validator.
For the full version breakdown (v1 through v8) and collision math, read our UUID Generator guide and our explainer on Unix epoch timestamps.
Frequently asked questions
What is the main difference between UUID v4 and UUID v7?
UUID v4 is completely random (122 random bits), while UUID v7 begins with a 48-bit Unix millisecond timestamp followed by 74 random bits. That leading timestamp makes UUID v7 naturally sortable by creation time and much friendlier to B-tree database indexes.
Can UUID v7 still collide when many IDs are generated in the same millisecond?
Collisions are practically impossible with a compliant generator. Even within a single millisecond, UUID v7 provides 74 bits of randomness (often combined with a monotonic sub-millisecond counter under RFC 9562), allowing billions of IDs per millisecond before collision risk becomes meaningful.
Is it safe to expose UUID v7 in public URLs?
UUID v7 cannot be guessed like sequential integers because of its 74 random bits, so it prevents ID enumeration. However, anyone can decode its first 48 bits to read the exact creation date and time down to the millisecond. If creation timing is sensitive, use UUID v4 for public URLs instead.
Should I use UUID v4 or UUID v7 for passwords or API secrets?
Neither is a replacement for dedicated cryptographic secrets. While UUID v4 uses random bits, API keys, session tokens and passwords should be generated directly with a cryptographically secure random byte generator (CSPRNG) of appropriate length. For user passwords, use our Password Generator.
How much database storage does a UUID take compared with a BIGINT?
A UUID takes 16 bytes in native binary form (twice the 8 bytes of a 64-bit BIGINT), or 36 bytes if stored inefficiently as a hyphenated text string. Using native binary UUID columns keeps index size and memory overhead low.