Scrivio Interview library
DebuggingIntermediate7 min read · updated 12 November 2026

After switching primary keys from bigint identity to random UUIDv4 stored as CHAR(36) in MySQL 8.4 InnoDB, insert throughput drops by half and the buffer pool hit rate falls. Explain why and fix it.

InnoDB stores the table itself as a B-tree ordered by primary key, so random UUIDv4 keys scatter every insert across the whole table, causing page splits and random reads that push useful pages out of the buffer pool. Storing them as CHAR(36) makes it worse, because every secondary index repeats the primary key. The fix is BINARY(16) with time-ordered values (UUIDv7, or UUID_TO_BIN(uuid, 1) for version 1), or a bigint clustered key with the UUID as a unique secondary column.

Why this question is asked

This is a change that looks harmless in a code review: we swap one key type for another to get globally unique ids. Weeks later the database is twice as slow and nobody connects it to the key. Interviewers ask this because the answer depends on how InnoDB physically stores rows, which is different from PostgreSQL. In this answer, we will recap the clustered index, see what random keys do to it, measure the size cost of CHAR(36), and then compare three fixes and how to prove them.

1A Quick Recap of InnoDB's Clustered Index

In InnoDB, the table is the primary key index. The full rows live in the leaf pages of a B-tree sorted by primary key, in pages of 16 KB. This is called a clustered index. There is no separate heap of rows as in PostgreSQL.

Every secondary index stores, in each entry, the indexed columns plus the primary key value of the row. To find a row through a secondary index, InnoDB looks up the primary key there and then searches the clustered index. So the primary key is copied into every secondary index of the table.

The buffer pool is InnoDB's memory cache of pages. The hit rate is the share of page requests served from memory instead of disk. A falling hit rate means the working set no longer fits.

2What Random Keys Do to the Table

With a bigint auto-increment key, every new row goes at the end of the clustered index. InnoDB fills the last page, starts a new one, and the pages being written are always the same few hot pages in memory. When a page at the end fills, InnoDB does not split it in half; it just starts a fresh page, so pages end up almost full.

With random UUIDv4 keys, each new row belongs at a random position in the sorted table. On a table much larger than the buffer pool, the target page is usually not in memory, so InnoDB must read it from disk before inserting. When that page is full, it splits into two half-full pages. The result is three problems at once: random reads on every insert, more pages because they are only partly full, and a buffer pool filled with pages that each received one insert, pushing out the pages that reads need. That matches both symptoms: throughput halves and the hit rate falls.

Step throughOne insert with a random UUIDv4 key
  1. Compute position in key orderrandom, anywhere in the table
  2. Is the target leaf page in the buffer pool?
    YesInsert in memoryNoRead 16 KB page from disk first
  3. Is the page full?
    YesSplit into two half-full pagesNoInsert the row
  4. Update each secondary indexeach entry stores the 36-byte key
  5. Evict a cold page to make roomoften one that reads needed

5 steps in order. Play them, or step through them one at a time.

3Why CHAR(36) Makes It Worse

A UUID is 16 bytes of data, but as text it is 36 characters. With the utf8mb4 character set InnoDB may reserve more than one byte per character for CHAR columns, so the key can cost 36 bytes or more in every clustered index entry. Then every secondary index repeats it. On a table with four secondary indexes, the key appears five times per row.

Comparisons are also slower, because comparing strings goes through the collation rules, while comparing binary values is a plain byte comparison.

SQL
SELECT index_name,
ROUND(stat_value * @@innodb_page_size / 1024 / 1024) AS size_mb
FROM mysql.innodb_index_stats
WHERE database_name = 'shop' AND table_name = 'orders' AND stat_name = 'size';
-- PRIMARY 41210 (before: 22874 with bigint)
-- idx_customer 9630 (before: 3410)
-- idx_created_at 9420 (before: 3290)

The figures in the comments are an illustration, but the shape is typical: the primary index grows because pages are half full after splits, and the secondary indexes grow because each entry carries a 36-character key instead of 8 bytes.

4Fix 1: BINARY(16) With Time-Ordered Values

The first fix keeps UUIDs but stores them properly and makes them arrive in order. BINARY(16) stores the 16 raw bytes. For order, we want the bytes that change slowly at the front. UUIDv7 does this by design, with a millisecond timestamp in the first 48 bits. MySQL 8.4 has no built-in UUIDv7 function, so we generate it in the application. If we use MySQL's own UUID(), which creates version 1 values, UUID_TO_BIN(uuid, 1) swaps the time fields so the high-order time comes first.

SQL
ALTER TABLE orders_new MODIFY id BINARY(16) NOT NULL; -- in the new table
INSERT INTO orders_new (id, customer_id) VALUES (UUID_TO_BIN(?), ?); -- ? = UUIDv7 from Java
SELECT BIN_TO_UUID(id) FROM orders_new WHERE id = UUID_TO_BIN(?); -- readable form
-- with MySQL's version 1 UUIDs: move time-high to the front
SELECT HEX(UUID_TO_BIN(UUID(), 1)); -- 11F0... bytes start with the slow-changing time

Notice that we must use the same swap flag when reading and writing: BIN_TO_UUID(id, 1) undoes UUID_TO_BIN(uuid, 1). Mixing them gives back a different string. Do not apply the swap flag to UUIDv7, which is already ordered.

5Fix 2: Keep a bigint Clustered Key

The second fix separates two jobs. Internally, the table keeps a bigint AUTO_INCREMENT primary key, so the clustered index stays sequential and secondary indexes carry only 8 bytes. Externally, a public_id BINARY(16) column with a unique index holds the UUID that APIs, events and other services use.

This costs one extra unique index of 16-byte entries, but inserts are sequential in the clustered index and only the public id index sees random positions. With UUIDv7 in that column, even that index stays mostly sequential. Foreign keys inside the database use the bigint, which keeps joins small.

6Measuring Before and After

We should prove the fix rather than assume it. We restore a copy of production-size data, replay a realistic insert load with a tool such as sysbench or a small Java harness, and record inserts per second, p99 insert latency, the buffer pool hit rate from SHOW ENGINE INNODB STATUS or performance_schema, and index sizes from mysql.innodb_index_stats.

Then we rebuild the table with the chosen fix and run the same load. A typical result is that throughput returns close to the bigint baseline and the index sizes shrink by half or more. We also rebuild the existing table once (ALTER TABLE ... FORCE or a copy with an online schema change tool), because the old half-empty pages stay half empty until the table is rebuilt.

7Follow-up questions

Would the same change hurt PostgreSQL as much?

Less, but still noticeably. PostgreSQL stores rows in a heap in insertion order, so only the primary key index suffers random inserts, not the whole table, and secondary indexes point to row locations rather than repeating the key. Random keys still cause index page splits and extra WAL there, so UUIDv7 is still preferred.

Can we change the key in place on a large table?

Changing a primary key type rewrites the whole table and every secondary index, and every foreign key that points to it must change too. On a large table we use an online schema change tool such as gh-ost or pt-online-schema-change, or build a new table and backfill it, during a planned window.

Why not just add more buffer pool memory?

That hides the problem only until the table grows again. Random keys make the whole table the working set for inserts, so the required memory grows with the table rather than with the recent data.

8Conclusion

In this answer, we saw that InnoDB clusters rows by primary key, so random UUIDv4 values scatter inserts across the whole table, causing page splits, random disk reads and a falling buffer pool hit rate, and that CHAR(36) inflates every secondary index because each one stores the primary key. The fix is BINARY(16) with time-ordered UUIDs (UUIDv7, or UUID_TO_BIN with the swap flag for version 1), or a bigint clustered key with a unique UUID column, proven with an insert benchmark and index sizes before and after.

What to say in the interviewAbout 80 seconds aloud

Open with

The cause is how InnoDB stores tables. The table is a clustered B-tree ordered by primary key, so with auto-increment every insert goes to the last page, which is always hot in memory.

Then, if they want more

With random UUIDv4 every insert lands at a random place in the whole table, so InnoDB has to read cold pages from disk, split full pages into half-empty ones, and fill the buffer pool with pages that got one row each, which pushes out the pages our reads need. That explains both the halved throughput and the falling hit rate.

Storing the value as CHAR(36) adds to it, because every secondary index stores the primary key, so a 36-character key is copied into each index instead of 8 bytes.

To fix it I would store the id as BINARY(16) and make the values time-ordered: UUIDv7 generated in the application, or UUID_TO_BIN with the swap flag if we use MySQL's version 1 UUIDs. Another option is to keep a bigint auto-increment primary key and add the UUID as a unique public_id column for APIs.

Either way I would benchmark inserts, check the buffer pool hit rate and index sizes before and after, and rebuild the table so the half-empty pages are compacted.

About this answer

Last updated
12 November 2026
Versions it names
MySQL 8.4
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.
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 "After switching primary keys from bigint identity to random UUIDv4 stored as CHAR(36) in...": https://scrivio-interview-library.netlify.app/databases/data-modeling/c7c087652a/