Quick revision
The short answer
InnoDB stores the table itself as the primary key B+ tree, so a random UUIDv4 key sends every insert to a random leaf page; once the table is bigger than the buffer pool, most inserts must first read that page from disk. Random inserts also split pages in the middle and leave them partly empty, and CHAR(36) makes every key at least 36 bytes and copies it into every secondary index, so everything grows. The fix is a time-ordered key, UUIDv7 or an auto-increment bigint, stored as BINARY(16) if it is a UUID, so new rows always go to the hot rightmost page.
What to remember
In this answer, we saw that InnoDB stores rows in the primary key B+ tree, so random UUIDv4 keys spread inserts over random leaf pages, which miss the buffer pool once the table outgrows memory. Random inserts split pages in the middle and leave them partly empty, and a CHAR(36) key, copied into every secondary index, inflates all of them. Switching to a time-ordered key, UUIDv7 or an auto-increment bigint, stored compactly as BINARY(16) or BIGINT, sends new rows to the cached rightmost page. PostgreSQL's heap avoids the clustered effect, but its B-tree indexes on random UUIDs suffer the same random access.
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.
InnoDB stores the table itself as the primary key B+ tree, so a random UUIDv4 key sends every insert to a random leaf page; once the table is bigger than the buffer pool, most inserts must first read that page from disk. Random inserts also split pages in the middle and leave them partly empty, and CHAR(36) makes every key at least 36 bytes and copies it into every secondary index, so everything grows. The fix is a time-ordered key, UUIDv7 or an auto-increment bigint, stored as BINARY(16) if it is a UUID, so new rows always go to the hot rightmost page.
Why this question is asked
Random UUIDs became popular because any service can create one without asking the database, and on a small table they behave perfectly. The trouble appears months later, when the table passes the size of memory and inserts that took 0.2 milliseconds start taking 5. Interviewers ask this question to see whether we can explain a production slowdown from the structure of the index rather than guessing. In this answer, we will recap how InnoDB stores rows, follow a random insert through the tree, see why pages end up half empty and indexes oversized, and then fix the key, with a note on PostgreSQL.
1A Quick Recap: InnoDB Is a Clustered Index
In InnoDB, the table is not a separate file of rows with indexes beside it. The table is the primary key B+ tree: its leaf pages hold the complete rows, sorted by primary key. This is called a clustered index. Every secondary index is a separate B+ tree whose leaf entries store the indexed columns plus the primary key value, which InnoDB uses to find the row in the clustered index.
Two consequences matter here. Where a new row is physically written is decided entirely by its primary key value. And the size of the primary key is paid again in every secondary index.
2Following a Random Insert
A UUIDv4 is 122 random bits, so each new key falls at a random position in the key order. With a sequence, the new key is always the largest, and the insert goes to the rightmost leaf page, which is already in the buffer pool because the previous insert used it. With a random key, the insert goes to any one of millions of leaf pages.
While the table fits in the buffer pool, that does not matter: every page is in memory. But once the table is, say, 200 GB and the buffer pool 64 GB, roughly two thirds of the leaf pages are not in memory at any moment. So about two thirds of inserts must first read a 16 KB page from storage, and then the changed page must eventually be written back. Throughput is now limited by random I/O instead of CPU, which is the sharp slowdown the team sees.
5 messages in order. Play them, or step through them one at a time.
With a sequential key the target page is always the cached rightmost leaf, so no read is needed.
3Why the Table Is Larger Than Expected
When a leaf page is full and a new key belongs in the middle of it, InnoDB splits the page, moving about half of the rows to a new page. With sequential keys InnoDB notices the pattern and splits at the insertion point, so pages fill almost completely. With random keys, pages are split in the middle and then fill slowly and unevenly, so on average leaf pages sit somewhere between half and about two thirds full. The same rows need far more pages, which means more disk, more memory to cache them and more I/O to read them.
The key format adds to it. CHAR(36) stores the UUID as text with hyphens, 36 bytes for the ASCII characters at minimum, while the value is really 16 bytes. Comparisons also go through the column's collation rules, which is slower than comparing bytes. And because every secondary index stores the primary key, a table with five secondary indexes stores those 36 bytes six times per row.
4The Fix: A Time-Ordered Key Stored Compactly
The goal is to make new keys arrive in increasing order again. If the ids do not need to be generated outside the database, an AUTO_INCREMENT bigint is the simplest and smallest choice. If services must generate ids themselves, a UUIDv7 keeps that property: it begins with a 48-bit millisecond timestamp, so values created later sort later, and random bits after the timestamp keep them unique. New rows then go to the rightmost leaf, which stays in memory, and pages fill properly.
Store the UUID as BINARY(16), not text. In Java we generate the UUIDv7 and convert it to 16 bytes; in SQL, UUID_TO_BIN() and BIN_TO_UUID() convert between the two forms. Note that UUID_TO_BIN has a swap flag that reorders the time fields of version 1 UUIDs; a version 7 value is already in time order, so we use it without the swap.
| Key | Bytes | Insert position | Generated by |
|---|---|---|---|
| CHAR(36) UUIDv4 | 36+ | random leaf | anyone |
| BINARY(16) UUIDv4 | 16 | random leaf | anyone |
| BINARY(16) UUIDv7 | 16 | rightmost leaf | anyone |
BIGINT AUTO_INCREMENT | 8 | rightmost leaf | the database |
Migrating an existing large table is a project of its own: we add the new column, backfill it in batches, rebuild the indexes with an online schema change tool, and switch the application, testing each step on a copy first. Even without changing the key values, converting CHAR(36) to BINARY(16) and rebuilding the table removes the half-empty pages and shrinks every index.
5Does PostgreSQL Have the Same Problem?
PostgreSQL stores rows in a heap, an unordered table file, so a random primary key does not decide where the row goes and the table itself does not suffer the clustered-index effect. But its B-tree index on the UUID column does: each insert lands on a random index leaf page, with the same cache misses and page splits once the index outgrows memory, and the same holds for indexes on foreign keys that reference it. So the advice is the same: use the native uuid type, which is 16 bytes, and prefer time-ordered values. PostgreSQL 18 added a built-in uuidv7() function, so the database can also generate them (check your version).
6Follow-up questions
What are the trade-offs of exposing time-ordered ids to clients?
A UUIDv7 reveals when the record was created to the millisecond, and a series of them reveals how many records a business creates over time. The random part still makes them hard to guess, so enumeration is not the main concern; information leakage is. If creation time is sensitive, we keep the time-ordered id internal and give clients a separate public identifier, which also lets us change the internal key later without breaking anyone.
Would OPTIMIZE TABLE fix the slowdown?
Rebuilding the table packs pages again and shrinks it, which helps for a while, but random inserts start splitting pages again immediately. It treats the symptom; changing the key order treats the cause.
Is auto-increment always better than UUIDv7?
It is smaller and simplest when only the database creates ids. UUIDv7 is better when ids must be created by services before the insert, across databases or regions, or when we want ids that do not reveal row counts. Both give sequential inserts.
7Conclusion
In this answer, we saw that InnoDB stores rows in the primary key B+ tree, so random UUIDv4 keys spread inserts over random leaf pages, which miss the buffer pool once the table outgrows memory. Random inserts split pages in the middle and leave them partly empty, and a CHAR(36) key, copied into every secondary index, inflates all of them. Switching to a time-ordered key, UUIDv7 or an auto-increment bigint, stored compactly as BINARY(16) or BIGINT, sends new rows to the cached rightmost page. PostgreSQL's heap avoids the clustered effect, but its B-tree indexes on random UUIDs suffer the same random access.
Open with
InnoDB keeps the whole table in the primary key B+ tree; the leaf pages hold the rows in key order. So the primary key decides where each insert physically lands.
Then, if they want more
A random UUIDv4 lands on a random leaf page. While the table fits in the buffer pool that is fine, but once the table is several times larger than memory, most inserts need a leaf that is not cached, so each one first reads a 16 KB page from disk and later writes it back. That is the sharp slowdown.
Random inserts also split full pages in the middle, so pages end up roughly half to two thirds full, and the table needs many more pages than the data requires.
CHAR(36) makes it worse: the key is stored as 36 or more bytes of text instead of 16, compared through the collation, and every secondary index stores the primary key, so five secondary indexes means six copies per row.
My fix is a time-ordered key: an auto-increment bigint if only the database creates ids, otherwise UUIDv7 generated in the service and stored as BINARY(16). New keys then always go to the rightmost leaf, which stays cached, and pages fill properly. Migrating needs a backfilled column and an online rebuild, rehearsed on a copy.
PostgreSQL stores rows in a heap, so the table is not affected the same way, but a B-tree index on random UUIDs has the same random page access, so I would use the uuid type with v7 values there too, and PostgreSQL 18 has a uuidv7 function.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- PostgreSQL 18
- 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 "A MySQL InnoDB table uses a random UUID (version 4) stored as CHAR(36) as its primary...": https://scrivio-interview-library.netlify.app/databases/storage-internals/6d0ba37c45/