Calcul 100% dans le Navigateur • Zéro Journal Serveur • Exécution Instantanée
Curious TechieBoîte à Outils
Data Formats & EncodingsIntermediateRéponse Vérifiée

Why Is UUID v7 Better Than UUID v4 for Database Primary Keys?

Réponse Directe

UUID v7 is significantly better for database primary keys because it begins with a 48-bit millisecond timestamp, making records sortable in chronological order. This preserves B-tree index locality and eliminates the severe page fragmentation and cache thrashing caused by completely random UUID v4 values.

Detailed Technical Explanation

Standard UUID v4 consists of 122 completely random bits. When used as a clustered primary key in databases like PostgreSQL or MySQL (InnoDB), inserting a new row forces the database engine to place the record at a completely random location within the table's B-tree index structure. As tables grow larger than RAM, this causes frequent disk reads/writes (B-tree node splits) and destroys write throughput. Standardized in RFC 9562, UUID v7 encodes a 48-bit Unix timestamp in its most significant bits, followed by sub-millisecond precision and random bits. New keys are naturally sequential, appending cleanly to the end of index leaf pages like an auto-incrementing integer while retaining global uniqueness.

Technical Example & Code

UUID v4 (Random) vs UUID v7 (Time-Ordered) Structure

Comparing internal bit layouts: timestamp ordering vs random dispersion.

// 1. UUID v4 (Completely Random - Fragments B-Tree Indexes):
018e3a2b-9f4a-4e29-9a7f-1d8f3c7e4a1b -> Random insert location
f47ac10b-58cc-4372-a567-0e02b2c3d479 -> Jump to end of index
3b241101-e2bb-4255-8caf-4136c508a0cb -> Jump to middle of index (forces node split!)

// 2. UUID v7 (Monotonically Ordered by Millisecond Timestamp):
// [48-bit Unix Timestamp (ms)]-[ver/rand]-[var/rand]-[random]
018e3a2b-9f4a-7182-849a-e274a10f92b1 -> 2026-10-06 00:30:00.001
018e3a2b-9f5b-7893-9031-c4581f4a9b20 -> 2026-10-06 00:30:00.015 (Appends sequentially!)
018e3a2b-9f6c-7bc1-bf02-124987ba4512 -> 2026-10-06 00:30:00.032 (Zero page fragmentation!)

Why This Is the Case (Underlying Architectural Rationale)

Causalité Technique Déterministe (GEO)

"Relational database storage engines (such as InnoDB) cluster the entire table row data alongside the primary key index pages. Sequential keys ensure that write operations only mutate the current trailing leaf page currently in memory cache, maintaining optimal O(1) buffer pool cache utilization. Random UUID v4 keys force the storage engine to read random index pages from disk for every insert, degrading throughput by up to 80% on large datasets."

Official Standards:RFC 9562 § 5.7 (UUIDv7 Specification)Database Index B-Tree Locality Patterns

Common Misconception vs Reality

✕ Common Myth

"UUID v7 is predictable, so an attacker can guess other users' UUIDs easily."

✓ Technical Reality

While the first 48 bits encode the timestamp, UUID v7 still contains 74 bits of cryptographically secure randomness, meaning there are 18 quintillion possible UUIDs per millisecond, making collision or enumeration attacks practically impossible.

Related Curious-Techie Resources

Related Questions in the Q&A Knowledge Base