What Is the Difference Between UUID v4 and UUID v7?
UUIDv4 is fully random (122 random bits). UUIDv7 has a 48-bit Unix timestamp prefix + random bits, so they sort chronologically. Use v7 for database primary keys. UUIDv7 was standardized in RFC 9562 and provides the best of both worlds: globally unique identifiers that also sort by creation time.
Comparison
| Feature | UUID v4 | UUID v7 |
|---|---|---|
| Structure | 122 random bits | 48-bit timestamp + random |
| Sortable | No | Yes (chronological) |
| DB index perf | Poor (random inserts) | Excellent (sequential) |
| Reveals time | No | Yes (ms precision) |
| RFC | RFC 9562 (was 4122) | RFC 9562 |
| Best for | Tokens, nonces | Primary keys, event IDs |
Examples
// UUID v4 (random)
550e8400-e29b-41d4-a716-446655440000
^--- version 4
// UUID v7 (timestamp + random)
018f6a7c-e8a0-7b12-8c34-56789abcdef0
^^^^^^^^^^^^^^^^--- timestamp ^--- version 7
Why v7 Is Better for Databases
B-tree indexes (used by PostgreSQL, MySQL, etc.) perform best with sequential inserts. UUIDv4's randomness causes scattered writes across the index, leading to page splits and fragmentation. UUIDv7's time-sorted structure produces near-sequential inserts, behaving more like auto-increment integers.
Try It Yourself
Generate UUIDs with our UUID v4 Generator or UUID v7 Generator.