UUID vs Auto Increment Primary Key: Performance
Random UUID keys insert into random pages, cause page splits and fill memory. Sequential keys append at the end. When to use UUIDv7 or ULID.
In this chapter
Random or sequential primary keys: where does the next row go?
UUID vs auto-increment keys: common questions
Are UUID primary keys slow?
Random UUIDv4 keys are, in clustered-index databases like InnoDB: each insert lands on a random page. Time-ordered UUIDv7 or ULID keys avoid most of the cost.
More on Indexes: how they are built
Keep learning
Overview
AUTO_INCREMENT BIGINT UUIDv4 as CHAR(36) Every secondary index entry grows by 36 bytes UUIDv4 as BINARY(16) Same randomness, less than half the bytes Still random inserts UUIDv7 / ULID as BINARY(16) Mostly appends (new ids sort after old ones) Snowflake BIGINT Appends, compact Worker ids and clock discipline needed Business key (email, SKU) Order follows the data, usually random chapters · indexes Random or sequential primary keys: where does the next row go? In InnoDB the table is the primary-key B-tree, so the key you choose decides where each new row is physically placed. A growing number lands on the last page. A random id lands anywhere. That difference shows up as page splits, half-empty pages, extra disk reads, and larger secondary indexes.
1. Watching the pages fill
With sequential keys, InnoDB recognizes the append: when the last page is full it just opens a new one, and the old page stays packed. The working set of a write-heavy table is one page. With random keys, the target page is rarely in memory for a big table, so each insert is a read followed later by a write, plus an occasional split that copies half a page and leaves two pages roughly half full. Over time pages average about 69% full (some say 50 to 70% in practice), so the same rows take roughly 40% more space and more buffer-pool memory. The B-tree chapters cover fill and splits at the page level in insert internals.
2. Every secondary index carries the primary key
The same repeated bytes also lower fan-out in the upper levels, so a fat key can add a level in a large index. See B-tree height for the arithmetic.
3. Choosing an id scheme
The fix for random inserts is not to give up unique ids, it is to make new ones sort after old ones. UUIDv7 (RFC 9562), ULID and Snowflake-style ids put a timestamp in the high bits, so ids created now are larger than ids created earlier and inserts append. They differ in size, in how they avoid collisions between generators, and in what they leak. Toggle your needs. Two common patterns stay compatible with all of this. Keep a compact internal key (BIGINT AUTO_INCREMENT) as the primary key and add a separate unique public_id for URLs and APIs: lookups by public id cost one extra index walk, and nothing else in the schema sees the wide value. Or generate time-ordered ids in the application with a library that guarantees monotonic order inside a millisecond. Whatever you choose, store binary forms rather than text. Business keys (email, SKU, national id) as primary keys are tempting but risky: they can change (an update that moves a clustered row to a new position), they are wide, they are rarely time-ordered, and they are copied into every secondary index. Prefer a surrogate key and a unique constraint on the business value. PostgreSQL differs here: its tables are heaps, so a random primary key does not fragment the table itself, though it still fragments the primary key index.
Reference: primary key options for InnoDB
Size and insert behaviour of common keys Insert pattern Watch out for
Recap
InnoDB stores the table in primary-key order, so the key decides where a new row lands. Increasing keys append: pages stay full, one hot page in memory. Random keys split pages, leave them about 69% full, and touch many cold pages. The primary key is copied into every secondary index entry. A 36-character text key can multiply total index size. For distributed ids prefer time-ordered ones (UUIDv7, ULID, Snowflake), stored as BINARY(16) or BIGINT. Hot right edge and the auto-increment lock are real but rarely the first bottleneck. Keep public ids in their own column when you need unguessable values.
More courses
- Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
- Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
- React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
- System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
- NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
- Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
- Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
- AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
- Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
- TypeScript in depth — The type system as a design tool. Inference, narrowing, generics, and the errors that mean the model is wrong — not a catalog of utility types.
- CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
- Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
- Docker — What an image is, why layers cache, and how containers find each other on a network.
- Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
- Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
- AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
- Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.