Which primary key should I use, and why does it matter?
Three keys, animated and measured.
Press Play, or step with the arrows. Switch between the three keys, or show them side by side.
System design lessons treat unique IDs as a coordination problem: how do many servers make IDs that never collide. That matters. But the bigger cost is usually inside the database, in the index those IDs go into.
Insert 200,000 rows three times, once per key type, then compare the size of each primary key index. Each insert takes a second or two in the browser.
INSERT INTO seq_keys (n) SELECT g FROM generate_series(1, 200000) g;
INSERT INTO v4_keys (n) SELECT g FROM generate_series(1, 200000) g;
INSERT INTO v7_keys (n) SELECT g FROM generate_series(1, 200000) g;
SELECT relname AS index,
pg_size_pretty(pg_relation_size(oid)) AS size
FROM pg_class
WHERE relname LIKE '%keys_pkey'
ORDER BY pg_relation_size(oid);| Primary key | PostgreSQL index | Leaf pages full | PostgreSQL insert | MySQL table | MySQL insert |
|---|---|---|---|---|---|
| bigint identity / AUTO_INCREMENT | 21.4 MB | 90.0% | 3.4 s | 137.7 MB | 6.2 s |
| UUIDv7 / UUID_TO_BIN(UUID(), 1) | 30.1 MB | 90.0% | 3.2 s | 145.8 MB | 5.7 s |
| UUIDv4 / 16 random bytes | 38.4 MB | 70.6% | 4.6 s | 236.0 MB | 55.5 s |
1,000,000 rows in 100 commits of 10,000, stock settings, one GitHub runner. Transcript and method in Notebook 10. Watch the splits happen in Watch an index grow.
| Approach | How it works | Watch out for |
|---|---|---|
| One sequence | The database hands out 1, 2, 3 | One writer only; guessable; leaks your row count |
| Sequence ranges | Each server takes a block, or auto_increment_increment and offset in MySQL | Gaps; reconfigure when servers are added |
| UUIDv4 | 122 random bits, no coordination | Random inserts: size and speed, as measured above |
| UUIDv7 | 48-bit millisecond time, then random bits | 16 bytes wide; reveals creation time |
| Snowflake style | 64-bit: time, machine number, a counter | Each machine needs a unique number; clocks must not go backwards |
For most new systems: bigint identity when one database writes, UUIDv7 when many writers or clients create IDs. PostgreSQL 18 has uuidv7() built in. In MySQL, UUID_TO_BIN(UUID(), 1) reorders a v1 UUID so the time comes first, which gives the same effect.
-- PostgreSQL 18 CREATE TABLE orders (id uuid DEFAULT uuidv7() PRIMARY KEY, ...);
-- MySQL 8 CREATE TABLE orders (id BINARY(16) DEFAULT (UUID_TO_BIN(UUID(), 1)) PRIMARY KEY, ...);
The same mechanism, keys and ids, sharding and partitioning, elsewhere on the site: in real incidents, in measurements, and in designs.
Pick an answer. You will see whether it is right, and why.
1. Why did random 16-byte keys make MySQL inserts ten times slower in Notebook 10?
InnoDB clusters rows by primary key. Random keys spread inserts over every page, and once the tree is bigger than the buffer pool most inserts need a disk read.
2. Why was the UUIDv7 index bigger than the bigint one in PostgreSQL?
Both were 90% full. The size difference is only the key width.
3. Which ID keeps inserts at the right edge of the index without a central counter?
Its leading timestamp makes new IDs sort after old ones.
How do databases actually work? From one row on disk to transactions: the words you need, PostgreSQL, MySQL and MongoDB from the files up, then Redis, Cassandra and vector search, side by side, and the query and concurrency traps.