Quick revision
The short answer
With PRIMARY KEY (id), the customer query finds the right entries in the secondary index quickly but then makes one primary key lookup per order, and for older orders those lookups are random reads from disk. With PRIMARY KEY (customer_id, created_at, id), a customer's orders sit next to each other in the clustered index, so the query is one short range scan. The price is more expensive inserts, wider secondary indexes and an extra unique index for lookups by id, so we choose by measuring our read and write mix on realistic data.
What to remember
In this answer, we saw that InnoDB stores rows inside the primary key, so the key decides the physical layout. With id as the key, the customer query is a quick index scan followed by one random lookup per order, which hurts once older orders are not cached. Clustering by customer turns it into one range scan, at the price of page splits, wider secondary indexes and a unique index on id. We choose by read and write ratio and memory size, and confirm with a realistic load test.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
With PRIMARY KEY (id), the customer query finds the right entries in the secondary index quickly but then makes one primary key lookup per order, and for older orders those lookups are random reads from disk. With PRIMARY KEY (customer_id, created_at, id), a customer's orders sit next to each other in the clustered index, so the query is one short range scan. The price is more expensive inserts, wider secondary indexes and an extra unique index for lookups by id, so we choose by measuring our read and write mix on realistic data.
Why this question is asked
At two billion rows, the choice of primary key in InnoDB stops being a naming detail and becomes the physical layout of the table on disk. Interviewers ask it to see whether we can reason about the cost on both sides. In this answer, we will recap how the clustered index works, follow the customer query through each design, look at what the second design costs on writes and storage, and finish with how we would decide and test.
1A Quick Recap of the Clustered Index
InnoDB stores every table as a B+tree ordered by the primary key. This tree is called the clustered index, because the rows themselves live in its leaf pages, clustered together in key order.
A secondary index is a separate B+tree. Its leaf entries do not hold the whole row; they hold the indexed columns plus the primary key value of the row. So when a query finds an entry in a secondary index and needs columns that the index does not have, InnoDB takes that primary key value and descends the clustered index to fetch the row. This second step is often called a lookup back to the clustered index, and it is the heart of this question.
2Design A: PRIMARY KEY (id) With a Secondary Index
In the first design, id is an AUTO_INCREMENT BIGINT, and orders are stored in the order they were created. A customer who has ordered for five years has their orders scattered across five years of the table, mixed with the orders of millions of other customers.
The first step is cheap; the second is the problem. Each of the 50 ids points to a different place in the clustered index. Recent orders are probably in the buffer pool, the memory cache InnoDB keeps for pages, because new rows are always added at the right edge of the tree. Older orders are not. If the table is around 400 GB and the buffer pool is 128 GB, most of those pages for a customer's older history must be read from disk, one random read each. At 1 ms per read on a network volume, that is 50 ms for one page of results.
Notice that writes in this design are as cheap as InnoDB gets. Every new order appends to the last, always cached leaf page; only the secondary index takes inserts in the middle.
3Design B: Clustering by (customer_id, created_at, id)
In the second design, we make the access pattern the physical order of the table. The rows of customer 42 are stored together and already sorted by date, so the query becomes a single range scan on the clustered index with no second step at all.
| Concern | PK (id) + index | PK (customer_id, created_at, id) |
|---|---|---|
| Customer history, 50 rows | 1 index range + up to 50 random lookups | 1 clustered range scan, 1 or 2 pages |
| Where inserts land | right edge of PRIMARY | inside each customer's range |
| Page splits on insert | rare in PRIMARY | frequent, pages end up partly empty |
| PK stored in each secondary entry | 8 bytes | about 23 bytes |
| Lookup by id alone | PRIMARY directly | ux_id, then PRIMARY |
We keep id at the end of the key because two orders of the same customer can share a timestamp, and a primary key must be unique. We also add a unique index on id. The reason is twofold: other services, payment callbacks and support tools look orders up by id alone, and the clustered key cannot help them because id is its last column, and InnoDB requires an AUTO_INCREMENT column to be the first column of some index.
4What Clustering by Customer Costs
The first cost is on writes. In design A, all inserts go to one hot spot at the end of the tree. In design B, every customer has their own insertion point at the end of their range, so with millions of active customers the inserts land all over the clustered index. A leaf page that is full must split: InnoDB moves about half its rows to a new page. Splits cost extra I/O and redo, and they leave pages half empty, so the same data takes more pages and needs more buffer pool. Worse, an insert into a customer whose page is cold must first read that page from disk.
The second cost is storage. Every secondary index entry carries the primary key. With an 8-byte id, that is 8 bytes per entry. With (customer_id, created_at, id) it is 8 + 7 + 8 = 23 bytes, because DATETIME(3) takes 5 bytes plus 2 for the milliseconds. At two billion rows, 15 extra bytes per entry is roughly 30 GB more per secondary index, and those bigger indexes compete for the same buffer pool.
5Choosing and Proving the Choice
The decision depends on the read to write ratio and on how much of the table fits in memory. If customers read their history far more often than orders are created, say 20 reads per insert, and the table is several times larger than the buffer pool, design B usually wins because it turns 50 random reads into one or two. If the system takes thousands of inserts per second and the hot part of the table fits in memory anyway, design A is simpler and its random lookups are mostly served from cache.
There is also a middle path for design A: a covering index. If the history page only shows id, created_at, status and total_cents, an index on (customer_id, created_at, status, total_cents) answers the query without any lookup, at the cost of a wider secondary index.
Whatever we pick, we prove it. We load a copy with realistic data, including the skew where a few customers have thousands of orders, replay a production-like mix of reads and inserts, and compare the numbers that matter: p99 latency of the history query, insert latency, Innodb_buffer_pool_reads, and page splits from information_schema.INNODB_METRICS (the index_page_splits counter, which must be enabled first; check your version).
6Follow-up questions
Why not use PRIMARY KEY (customer_id, id) instead?
Because ids grow over time, (customer_id, id) also keeps a customer's orders together in creation order, and it is narrower than including created_at. We include created_at only when orders can be backdated or imported out of order, or when queries filter on date ranges within a customer.
Can InnoDB read the clustered index backwards for ORDER BY created_at DESC?
Yes. InnoDB leaf pages are linked in both directions, so a backward range scan is efficient and EXPLAIN shows Backward index scan. MySQL 8.0 also supports real descending index columns if we need mixed sort directions.
7Conclusion
In this answer, we saw that InnoDB stores rows inside the primary key, so the key decides the physical layout. With id as the key, the customer query is a quick index scan followed by one random lookup per order, which hurts once older orders are not cached. Clustering by customer turns it into one range scan, at the price of page splits, wider secondary indexes and a unique index on id. We choose by read and write ratio and memory size, and confirm with a realistic load test.
Open with
In InnoDB the primary key is the clustered index, so the rows live in primary key order. With PRIMARY KEY id and a secondary index on customer_id and created_at, the customer query scans the secondary index cheaply, but then does one lookup into the clustered index per order.
Then, if they want more
A customer's orders are spread over years of inserts, so for older orders those are random disk reads, maybe 50 for one page of history. If I cluster by customer_id, created_at, id, a customer's orders are stored together, and the same query is a single range scan touching one or two pages.
That costs me on writes: inserts land inside every customer's range, so I get page splits and half-empty pages. Every secondary index also carries the wider key, about 23 bytes instead of 8, which at two billion rows is tens of gigabytes per index.
And lookups by order id need a unique index on id, which InnoDB also requires for the AUTO_INCREMENT column.
I would decide based on the read to write ratio and how much of the table fits in the buffer pool, consider a covering index as a middle path, and prove it with a load test on realistic, skewed data, comparing p99 read latency, insert latency, buffer pool misses and page split counts.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- MySQL 8.0
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "You are designing an orders table in MySQL that will reach 2 billion rows. The most...": https://scrivio-interview-library.netlify.app/databases/mysql/c3737152b8/