Almanac of UUIDs · Part VI
The Database Performance Crisis
While UUIDv4 solved the uniqueness and complexity problems, it introduced a severe performance regression in relational databases. To understand why RFC 9562 introduced v7, we must understand the internals of database indexing.
6.1 The B-Tree Index Structure
Most Relational Database Management Systems (RDBMS) like PostgreSQL, MySQL (InnoDB), and SQL Server use B-Tree (Balanced Tree) structures for their Primary Key indexes. B-Trees are optimized for locality: data that is accessed together should be stored together.
In a B-Tree, data is stored in sorted order. When a new row is inserted with a sequential key (like 1, 2, 3 or a timestamp):
- The database finds the "rightmost" leaf page of the index.
- It appends the new value.
- When the page is full, it allocates a new page.
- Memory access is predictable; the "hot" page stays in the Buffer Pool (RAM).
6.2 The Random Write Penalty
When using UUIDv4 (Random) as a Primary Key:
- The new ID is random (e.g., starts with
2a..., thenf9..., then01...). - The database must insert this ID into a specific sorted position in the B-Tree.
- This position is likely on a "cold" page (one not currently in RAM).
- The database must fetch that page from the disk (I/O penalty).
- If the page is full, the database performs a Page Split: it creates two new pages, moves half the data to each, and updates the parent pointers.
- This process dirties multiple memory pages, triggering increased WAL (Write Ahead Log) activity and decreasing write throughput.
Benchmark Reality
Empirical tests show that in large datasets (exceeding RAM size), UUIDv4 writes can be 10x to 50x slowerthan sequential writes due to this "Write Amplification" and "Index Fragmentation."
References & Further Reading
- Why UUID7 is better than UUID4 as clustered index in RDBMS
- UUID v7 vs BIGSERIAL: I Ran The Benchmarks So You Don't Have To
- Benchmarking Random (v4) and Time-based (v7) UUIDs
- How UUIDv7 in PostgreSQL 18 Fixes the “Random UUIDs Are Slow” Problem
- PostgreSQL Documentation: UUID Functions
- Implement UUIDv7 in Amazon RDS for PostgreSQL
- Database and Migration Insights