Difficulty: Intermediate
What are the drawbacks of using UUIDs as primary keys compared to auto-increment integers, and how can you reduce them?
UUIDs are 16 bytes instead of 4 or 8, and the common random version (UUIDv4) inserts at random positions in the primary key index, which causes page splits, poor cache use and extra write I/O, especially in databases that cluster rows by primary key such as MySQL InnoDB. They are also harder for humans to read and debug. Using time-ordered UUIDv7, storing them in a native 16-byte type, and keeping a compact internal key where it matters removes most of the pain.
A B-tree primary key with auto-increment ids always appends to the rightmost leaf page. The hot page stays in memory and pages fill completely.
With UUIDv4, each new key lands at a random point in the tree:
In InnoDB the table itself is the clustered primary key index, so random keys scatter the actual row data, not just an index. PostgreSQL stores rows in an unordered heap, so the effect is limited to the indexes, but it is still measurable on large, write-heavy tables.
| Key type | Size | Notes |
|---|---|---|
INT | 4 bytes | Max about 2.1 billion; risky for big tables |
BIGINT | 8 bytes | Practical default for auto-increment |
UUID native / BINARY(16) | 16 bytes | Twice a BIGINT |
UUID as CHAR(36) | 36 bytes plus overhead | Common mistake, avoid |
The primary key is repeated in every foreign key column and, in InnoDB, in every secondary index entry. A wide key multiplies across the schema.
0192f5c4-... is not.ORDER BY id is not creation order./orders/1001, /orders/1002.uuidv7() function; on older versions generate it in the application or with an extension.UUID (16 bytes); in MySQL use BINARY(16) with UUID_TO_BIN(), whose swap flag reorders time fields for UUIDv1.BIGINT identity as the internal primary key for joins, and add a unique public_id UUID column for APIs and URLs.| BIGINT identity | UUIDv4 | UUIDv7 | |
|---|---|---|---|
| Size | 8 bytes | 16 bytes | 16 bytes |
| Insert pattern | Sequential | Random | Roughly sequential |
| Generate without the DB | No | Yes | Yes |
| Guessable in URLs | Yes | No | Hard, but leaks creation time |
-- Random UUIDv4 (built in since PostgreSQL 13)
CREATE TABLE sessions (
id UUID PRIMARY KEY DEFAULT gen_random_uuid()
);
-- Time-ordered UUIDv7 (built in since PostgreSQL 18)
CREATE TABLE events (
id UUID PRIMARY KEY DEFAULT uuidv7(),
payload JSONB
);
-- Hybrid: compact internal key, opaque public id
CREATE TABLE orders (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
public_id UUID NOT NULL DEFAULT gen_random_uuid() UNIQUE,
amount_paise BIGINT NOT NULL
);
The hybrid design joins on an 8-byte key internally while exposing a non-guessable UUID in URLs and APIs.
CREATE TABLE payments (
id BINARY(16) PRIMARY KEY, -- 16 bytes, not CHAR(36)
amount_paise BIGINT NOT NULL
);
-- Swap flag 1 reorders UUIDv1 time fields so inserts are sequential
INSERT INTO payments (id, amount_paise)
VALUES (UUID_TO_BIN(UUID(), 1), 49900);
SELECT BIN_TO_UUID(id, 1) AS id, amount_paise FROM payments;
MySQL's UUID() returns version 1. Storing it as BINARY(16) with the swap flag keeps the clustered index append-friendly.
UUID, UUIDv7, Clustered Index, Page Splits, Auto-increment Keys