When to use UUIDs over auto-increment, the index fragmentation problem, and how UUID v7 solves it.
Database B-tree indexes store keys in sorted order. With auto-increment, each new ID is always larger than all existing ones — inserts go to the end of the index. Fast, sequential, cache-friendly.
With UUID v4 (random), each new ID is random — it inserts into a random position in the index. The database must: find the right position, potentially split an index page, update adjacent pages. At 10 million rows, this causes measurable performance degradation.
| Primary Key Type | Insert Performance (10M rows) | Storage Size |
|---|---|---|
| Integer (AUTO_INCREMENT) | Baseline (100%) | 4–8 bytes |
| UUID v4 (VARCHAR 36) | 30–50% of baseline | 36 bytes |
| UUID v4 (BINARY 16) | 40–60% of baseline | 16 bytes |
| UUID v7 (BINARY 16) | 80–90% of baseline | 16 bytes |
-- MySQL: Store efficiently, display as string CREATE TABLE users ( id BINARY(16) PRIMARY KEY DEFAULT (UUID_TO_BIN(UUID(), 1)), name VARCHAR(255) ); INSERT INTO users (id, name) VALUES (UUID_TO_BIN(UUID(), 1), 'Rahul'); SELECT BIN_TO_UUID(id), name FROM users; -- PostgreSQL: Native UUID type CREATE TABLE users ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), name TEXT );
For new applications starting in 2024+, use UUID v7. It combines the distributed generation benefits of UUID with near-sequential insertion performance. PostgreSQL 17 (2024) has native UUID v7 support. Most ORMs (Prisma, TypeORM, Sequelize) have v7 packages available.