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

FeatureUUID v4UUID v7
Structure122 random bits48-bit timestamp + random
SortableNoYes (chronological)
DB index perfPoor (random inserts)Excellent (sequential)
Reveals timeNoYes (ms precision)
RFCRFC 9562 (was 4122)RFC 9562
Best forTokens, noncesPrimary 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.

Built by Michael Lip. 100% client-side — no data leaves your browser.