Should the primary key be an auto-increment integer or a UUID? It's one of the longest-running schema-design debates, and the answer used to be "it depends". Now that time-ordered UUIDs (UUIDv7, standardized in RFC 9562) are available, the trade-offs are much clearer.
What was wrong with UUIDs: randomness, not size
The classic complaint about UUID keys was performance, and the cause was UUIDv4, which is 122 bits of pure randomness.
B-tree indexes are sorted. With an increasing key (1, 2, 3… or a timestamp), every insert lands on the rightmost leaf page, which is always in memory, and pages fill up neatly. With random keys, each insert lands on a random page somewhere in the index:
- the working set is the entire index, so once it no longer fits in RAM, inserts turn into random disk reads;
- pages split in the middle and end up half-full, making the index bigger;
- in PostgreSQL, the first change to each page after a checkpoint writes a full-page image to the WAL, so scattered writes produce many times more WAL;
- in MySQL/InnoDB and SQL Server, the table is the clustered index, so the table data itself gets fragmented.
Benchmarks show a clear pattern: random-v4 insert throughput degrades as the table grows, while sequential keys stay flat.
UUIDv7 fixes the random part
UUIDv7 puts a 48-bit millisecond timestamp at the front, followed by random bits:
0192 3f4a - 8b1c - 7d2e - 9f10 - 4a5b6c7d8e9f
└──── timestamp ───┘ ver └──── random ──────┘
New values sort after old ones, so inserts append at the right edge of the index like an auto-increment would. You keep the benefits that made people want UUIDs in the first place:
- generate IDs anywhere: in the app, offline, on a phone, in five services, with no round trip to the database;
- merge data from shards or tenants without collisions;
- don't leak business data:
/orders/10442tells competitors how many orders you have; a UUID doesn't.
Watch out for one thing: v7 contains its creation time. If the time an ID was created is itself sensitive, don't expose v7 IDs publicly.
What it still costs
A UUID is 16 bytes against a bigint's 8. That doubles the size of the primary key and of every foreign key and index entry that refers to it. In InnoDB and SQL Server, every secondary index also stores the clustering key, so the cost multiplies. Expect indexes to be noticeably bigger, and joins slightly slower, than with bigint. The difference is usually in the tens of percent, not the multiples you saw with v4.
Store UUIDs in a native type, never as text. char(36) takes 36+ bytes and compares character by character.
| Database | Type |
|---|---|
| PostgreSQL | uuid (uuidv7() built in from PostgreSQL 18) |
| SQL Server | uniqueidentifier (note: sorts by byte groups in an unusual order, so generate sequential values with NEWSEQUENTIALID() or a library that targets SQL Server's sort order) |
| MySQL | BINARY(16) with UUID_TO_BIN(uuid, 1) for v1, or app-generated v7 bytes |
PostgreSQL example
-- PostgreSQL 18+
CREATE TABLE orders (
id uuid PRIMARY KEY DEFAULT uuidv7(),
customer_id bigint NOT NULL,
created_at timestamptz NOT NULL DEFAULT now()
);
-- Integer alternative
CREATE TABLE orders (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
...
);
Before PostgreSQL 18, generate v7 in the application (most languages have a library for it) or with the pg_uuidv7 extension.
The decision
- Single database, and IDs can come from the database? Use
bigintidentity. It's the smallest and fastest option, and nothing beats it. - IDs generated outside the database (distributed services, offline clients, client-side optimistic creates), multi-region merges, or public IDs that shouldn't be guessable? Use UUIDv7 in a native column.
- UUIDv4? Only for tokens and secrets, where unpredictability is the point. Not as a clustered primary key on a large, write-heavy table.
A popular hybrid gets both: a bigint primary key for joins, plus a unique UUIDv7 public_id column that is the only ID the API exposes.
Checklist
- The pain came from UUIDv4's randomness, not from UUIDs as such.
- Use UUIDv7 when IDs must be created outside the database.
- Always store UUIDs in a native 16-byte type.
- Default to
bigintwhen a single database hands out the IDs.
Get the weekly commit
New database deep dives every week.
