Quick revision
The short answer
A random UUID primary key makes every insert land on a random leaf page of InnoDB's clustered index. Once the index is larger than the buffer pool, most inserts must read a page from disk and many split pages, so throughput collapses. CHAR(36) also puts a 36 byte key into every secondary index entry. The fix is a time-ordered key, UUIDv7 stored as BINARY(16), or a BIGINT auto-increment key with the UUID as a unique secondary column, migrated in batches with an online schema change tool.
What to remember
In this answer, we saw that a random UUID primary key spreads inserts across the whole clustered index, so once the table outgrows the buffer pool, most inserts read pages from disk and split them, and throughput drops sharply. A CHAR(36) key also inflates every secondary index. We fixed it with a time-ordered key, UUIDv7 as BINARY(16) or a BIGINT surrogate with the UUID as a unique column, migrated in batches with an online schema change tool, and we verified it with buffer pool hit ratio, page splits and insert latency.
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.
A random UUID primary key makes every insert land on a random leaf page of InnoDB's clustered index. Once the index is larger than the buffer pool, most inserts must read a page from disk and many split pages, so throughput collapses. CHAR(36) also puts a 36 byte key into every secondary index entry. The fix is a time-ordered key, UUIDv7 stored as BINARY(16), or a BIGINT auto-increment key with the UUID as a unique secondary column, migrated in batches with an online schema change tool.
Why this question is asked
This is a classic production story: the design worked well in testing and in the first months, and then one day insert latency jumps without any code change. Interviewers ask it because the explanation connects key choice to the physical storage model and to memory, and because the fix involves a careful migration rather than a quick patch. In this answer, we will recap how InnoDB stores rows, see why random keys break once the table outgrows memory, measure the secondary index cost, choose a new key, and plan the migration and its verification.
1A Quick Recap of InnoDB's Clustered Index
InnoDB stores the table inside its primary key B-tree. Leaf pages, 16 KB each by default, hold complete rows in primary key order. The buffer pool is InnoDB's memory cache of these pages. As long as the pages an insert needs are in the buffer pool, an insert is a memory operation plus a log write; when a needed page is not there, InnoDB must read it from disk first.
Every secondary index stores, next to its own key, the primary key value of the row. That is how it finds the row, and it means the primary key's size is paid again in every secondary index.
2Why Random Keys Collapse After the Buffer Pool Fills
With an auto-increment or time-ordered key, every new row goes to the right-most leaf page. That page is always in memory, it fills to the end, and then a new page starts. The working set of an insert workload is a handful of pages.
With a random UUIDv4, the next key can belong anywhere in the tree. While the whole tree fits in memory, this costs little. But when the table grows past the buffer pool, say a 300 GB index and a 64 GB buffer pool, the chance that the target leaf page is cached falls to about one in five. Most inserts now wait for a random disk read. And when a target page is full, InnoDB splits it into two half-full pages, which spreads data over more pages, wastes space and makes the next reads less likely to hit the cache. That is the 70 percent drop: the workload changed from sequential memory writes to random disk reads.
| Sequential key (BIGINT, UUIDv7) | Random key (UUIDv4) | |
|---|---|---|
| Target leaf page | always the right-most page | any page in the tree |
| Page in buffer pool? | yes, it is hot | rarely, once tree > memory |
| Page splits | only at the end, pages fill to about 15/16 | everywhere, pages left half full |
| Secondary index key size | 8 or 16 bytes | 36 bytes as CHAR(36), more with utf8mb4 |
3The Secondary Index Cost of CHAR(36)
A UUID as text is 36 characters. With a utf8mb4 column, InnoDB may reserve more bytes per character in some row formats, and comparisons go through a collation, which is slower than comparing bytes. Every secondary index entry carries this value. With five secondary indexes and a billion rows, each extra 28 bytes compared with BINARY(16) or BIGINT adds about 28 GB per index, so roughly 140 GB of index that competes for the same buffer pool.
4Choosing the New Key
Option one is UUIDv7, a UUID whose first 48 bits are a millisecond timestamp, so new values are almost always larger than old ones and inserts go to the end of the tree again. Stored as BINARY(16), it is compact and still globally unique, which matters if services generate ids without asking the database. Java has no built-in UUIDv7 generator in the JDK up to 25, so we use a small library or our own generator.
Option two is a BIGINT AUTO_INCREMENT surrogate key as the primary key, with the existing UUID kept as a unique secondary column for external references. The clustered index becomes as compact as possible, and secondary indexes carry 8 bytes. The UUID's unique index is still random, but it is one index rather than the whole table. Note that MySQL's UUID_TO_BIN(uuid, 1) swaps the time fields of a version 1 UUID to make it ordered; it does not help version 4 values.
5Planning the Migration
Changing a primary key on a large, busy table is the risky part, so we do it in steps. We add the new column, nullable at first, and make new rows fill it. We backfill old rows in batches of a few thousand, ordered by the current key, with pauses so replicas keep up. Then we switch the primary key with an online schema change tool such as gh-ost or pt-online-schema-change, which copies the table in the background and swaps it at the end, instead of a blocking ALTER. Services that store or join on the old key need a coordinated change: they keep using the UUID as the public identifier while the internal key changes.
- Add new key columnBIGINT or BINARY(16) UUIDv7, nullable
- Fill it for new rowsapplication or default
- Backfill old rows in batches5,000 rows per batch, watch replica lag
- Rebuild with gh-ost or pt-oscnew column becomes PRIMARY KEY, UUID becomes UNIQUE
- Switch services that join on the keypublic API keeps the UUID
- Measure before and afterhit ratio, page splits, insert p99
6 steps in order. Play them, or step through them one at a time.
6Measuring Before and After
We capture the baseline before changing anything. The buffer pool hit ratio comes from Innodb_buffer_pool_read_requests compared with Innodb_buffer_pool_reads, page splits from the index counters in information_schema.INNODB_METRICS (some must be enabled first; check your version), and insert latency at p50 and p99 from the application. After the change we expect the hit ratio to rise back towards 99 percent for the insert workload, page splits to drop to near zero except at the end of the tree, and the secondary indexes to shrink.
7Follow-up questions
What are the downsides of auto-increment keys in a multi-writer setup?
Auto-increment values are generated by one database, so two primaries or several shards will produce the same numbers unless we configure offsets with auto_increment_increment and auto_increment_offset, which is fragile when nodes are added. Ids also leak information, such as how many orders we take per day, if they are exposed. And clients cannot know the id before the insert, which complicates idempotent retries and batching. That is why many systems keep a sequential internal key and a UUIDv7 or Snowflake-style id for anything that crosses system boundaries.
Would UUIDv7 make the table exactly as fast as BIGINT?
Close, but not equal. It still inserts at the end of the tree, so it removes the random I/O and page splits, which is most of the problem. It is twice the size of BIGINT, so secondary indexes are somewhat larger, and values generated on many servers interleave slightly within the same millisecond, which costs little.
8Conclusion
In this answer, we saw that a random UUID primary key spreads inserts across the whole clustered index, so once the table outgrows the buffer pool, most inserts read pages from disk and split them, and throughput drops sharply. A CHAR(36) key also inflates every secondary index. We fixed it with a time-ordered key, UUIDv7 as BINARY(16) or a BIGINT surrogate with the UUID as a unique column, migrated in batches with an online schema change tool, and we verified it with buffer pool hit ratio, page splits and insert latency.
Open with
In InnoDB the primary key is the clustered index, so the key decides where each row is physically written. With a random UUID, every insert targets a random leaf page.
Then, if they want more
That is fine while the whole tree fits in the buffer pool, but once the table outgrows memory, most target pages are not cached, so inserts wait on random disk reads, and full pages split and leave half-empty pages, which makes the cache even less effective. That explains a 70 percent drop that appears suddenly at a certain size.
The CHAR(36) type makes it worse because every secondary index entry stores the primary key, so each index carries 36 bytes per row and compares with a collation.
For the fix, I would move to a sequential key: either UUIDv7 stored as BINARY(16), which keeps global uniqueness and inserts at the end of the tree, or a BIGINT auto-increment primary key with the UUID kept as a unique secondary column for external references.
The migration is staged: add the column, fill it for new rows, backfill in batches while watching replica lag, then swap the key with gh-ost or pt-online-schema-change, and coordinate with services that join on the old key. I would capture the buffer pool hit ratio, page splits and insert p99 before and after to prove it worked.
About this answer
- Last updated
- 12 November 2026
- 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
- PostgreSQL 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 "An InnoDB events table uses a random UUID (CHAR(36)) primary key. Insert throughput fell...": https://scrivio-interview-library.netlify.app/databases/indexing/edcf6b36ec/