Quick revision
The short answer
InnoDB always stores a table as a B-tree ordered by a clustering key: the primary key if there is one, otherwise the first unique index whose columns are all NOT NULL, otherwise a hidden 6-byte row id that InnoDB generates from a counter shared by all such tables and that queries cannot see. Every secondary index entry stores the primary key value as its pointer to the row, so a wide primary key, such as a CHAR(36) UUID, is repeated in every secondary index and makes them all larger. Tables without an explicit primary key also cause slow row-based replication and are refused by Group Replication.
What to remember
In this answer, we saw that InnoDB always clusters a table by a key: the primary key, else the first NOT NULL unique index, else a hidden 6-byte row id from a global counter that queries cannot use. Every secondary index stores the primary key as its row pointer, so a wide key like CHAR(36) is repeated in every secondary entry and costs disk and buffer pool memory. Short, increasing keys keep inserts at the end of the tree and avoid page splits, secondary lookups cost two descents unless the index covers the query, and tables without a primary key slow row-based replication and are refused by Group Replication, which sql_require_primary_key helps prevent.
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 always stores a table as a B-tree ordered by a clustering key: the primary key if there is one, otherwise the first unique index whose columns are all NOT NULL, otherwise a hidden 6-byte row id that InnoDB generates from a counter shared by all such tables and that queries cannot see. Every secondary index entry stores the primary key value as its pointer to the row, so a wide primary key, such as a CHAR(36) UUID, is repeated in every secondary index and makes them all larger. Tables without an explicit primary key also cause slow row-based replication and are refused by Group Replication.
Why this question is asked
In many databases a primary key is mostly a constraint. In InnoDB it is the physical layout of the table, which is why its choice affects storage size, insert speed, lookup cost and even replication. Interviewers ask this to see whether we understand the clustered index model and can reason about its costs from first principles. In this answer, we will recap the clustered index, see what InnoDB does without a primary key, measure how the primary key's width multiplies into secondary indexes, look at insert order and page splits, explain the double lookup, and finish with replication.
1A Quick Recap of the Clustered Index
InnoDB stores each table as a B-tree, a sorted tree of 16 KB pages. The leaf pages of this tree hold the complete rows, sorted by the clustering key. This tree is called the clustered index: the index and the table are the same structure. Looking up a row by its primary key means walking down this one tree, usually three or four page reads for millions of rows.
Because the rows live in exactly one place, ordered by the primary key, every other index needs a way to point to them. In InnoDB, that pointer is the primary key value itself, not a physical address.
2What InnoDB Does Without a Primary Key
InnoDB cannot store a table without a clustering key, so it picks one. If the table has a primary key, that is it. If not, InnoDB uses the first UNIQUE index whose columns are all NOT NULL. If there is none, it creates a hidden clustered index called GEN_CLUSTERED_INDEX on a hidden 6-byte column, DB_ROW_ID, filled from a counter.
Two details matter. The hidden column is invisible: we cannot query it, index it or use it to find a row, so the table effectively has no usable key. And the counter that generates row ids is global, shared by every table in the server that uses a hidden key, so heavy inserts into several such tables contend on it.
MySQL 8.0.30 and later can add an invisible primary key automatically when sql_generate_invisible_primary_key is on: it creates an auto-increment column named my_row_id that SELECT * does not show. That gives the table a real, usable key without changing what the application sees.
3Why the Primary Key Width Multiplies
Every secondary index entry consists of the indexed columns plus the primary key columns. When InnoDB finds a match in a secondary index, it uses that stored primary key to look up the full row in the clustered index. So the primary key's width is paid once in the clustered index and once more in every entry of every secondary index.
| Primary key | Entry holds | Entry size (about) |
|---|---|---|
| BIGINT id | customer_id (8) + id (8) | 16 bytes + overhead |
| BINARY(16) UUID | customer_id (8) + uuid (16) | 24 bytes + overhead |
| CHAR(36) UUID | customer_id (8) + uuid text (36 or more) | 44+ bytes + overhead |
Notice that the cost grows with both the number of rows and the number of indexes. Larger indexes take more disk, but more importantly they take more buffer pool memory, so fewer of them fit in cache and more lookups go to disk. A UUID stored as BINARY(16) instead of CHAR(36) already cuts the cost by more than half.
4Insert Order and Page Splits
The clustered index is sorted by the primary key, so where a new row goes depends on its key. With a short, monotonically increasing key such as an AUTO_INCREMENT BIGINT, every new row belongs at the end of the tree. InnoDB fills the last page, starts a new one, and leaves earlier pages full and untouched.
With a random key, such as a version 4 UUID, each new row belongs somewhere in the middle. InnoDB must read that page, and if it is full, split it into two half-empty pages. Over time the table is full of half-used pages, inserts touch random pages all over the tree, and the working set becomes the whole index. If UUIDs are required, storing them as BINARY(16) with a time-ordered layout, such as UUID_TO_BIN(UUID(), 1) for MySQL's time-based UUIDs or version 7 UUIDs generated by the application, keeps inserts near the end.
5The Double Lookup and Covering Indexes
Because secondary indexes point by primary key, a lookup through a secondary index costs two tree descents: one in the secondary index to find the primary key, and one in the clustered index to fetch the row. For a query returning 1,000 rows, that can mean 1,000 extra clustered index lookups.
5 messages in order. Play them, or step through them one at a time.
The first query is covered: everything it needs, including id, which is stored in every secondary entry anyway, is in the index, so InnoDB skips the second descent. That is a useful side effect of the design, and a reason to add a column to an index when a hot query needs only a few.
6Tables Without a Primary Key and Replication
With row-based replication, the default, the source sends each changed row as an event with its before and after images. The replica must find each row to update or delete. With a primary key, that is one index lookup per row. Without one, and without a suitable unique index, the replica may have to scan the whole table for every row in the event, so a DELETE of 10,000 rows on a 5-million-row table can mean 10,000 table scans on the replica, and replication falls hours behind.
Group Replication, and the InnoDB Cluster built on it, requires every table to have a primary key or an equivalent unique key and refuses changes to tables without one. To stop such tables being created at all, we set sql_require_primary_key = ON, so CREATE TABLE without a primary key fails.
7Follow-up questions
Why can MySQL replicas fall far behind on tables without a primary key?
Because row-based replication sends each changed row, and the replica's applier must locate each row before changing it. Without a primary key or a usable unique index, it may scan the table for every row, so one statement that changes many rows on the source turns into many full scans on the replica. The source did one scan; the replica does thousands. Adding a primary key, or at least an index the applier can use, fixes it.
Is a UUID primary key always a bad idea?
No, but it has costs. Store it as BINARY(16), not CHAR(36), and prefer a time-ordered form so inserts go to the end of the tree. Some teams keep a BIGINT AUTO_INCREMENT primary key for storage and add the UUID as a unique secondary column for external use.
Does InnoDB pick a NOT NULL unique index as the clustered index even if I add a primary key later?
Once we add a primary key, InnoDB rebuilds the table clustered by it, and the unique index becomes an ordinary secondary index. That rebuild rewrites the whole table, so on large tables it should be planned, for example with an online schema change tool.
8Conclusion
In this answer, we saw that InnoDB always clusters a table by a key: the primary key, else the first NOT NULL unique index, else a hidden 6-byte row id from a global counter that queries cannot use. Every secondary index stores the primary key as its row pointer, so a wide key like CHAR(36) is repeated in every secondary entry and costs disk and buffer pool memory. Short, increasing keys keep inserts at the end of the tree and avoid page splits, secondary lookups cost two descents unless the index covers the query, and tables without a primary key slow row-based replication and are refused by Group Replication, which sql_require_primary_key helps prevent.
Open with
In InnoDB the table is the clustered index: a B-tree whose leaves hold the rows, sorted by the clustering key. If there is a primary key, that is the key.
Then, if they want more
If not, InnoDB uses the first unique index with all columns NOT NULL, and if there is none, it creates a hidden clustered index on a 6-byte row id, GEN_CLUSTERED_INDEX, from a global counter shared across such tables, which I cannot query or use.
Secondary indexes do not store a physical address; each entry stores the primary key value, and a lookup through a secondary index finds the primary key and then descends the clustered index, so two tree walks unless the index covers the query.
That is why the primary key width matters: a CHAR(36) UUID is repeated in every entry of every secondary index, which can add gigabytes on a big table and pushes indexes out of the buffer pool.
I prefer a short, increasing key like a BIGINT auto-increment, which also keeps inserts at the end of the tree and avoids page splits; if I need UUIDs, I store them as BINARY(16) and time-ordered.
Tables without a primary key are also bad for replication: with row-based replication the replica may scan the table for every changed row and fall behind, and Group Replication requires a primary key. So I would turn on sql_require_primary_key.
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 "In InnoDB, what happens if a table has no primary key? Why does the choice of primary...": https://scrivio-interview-library.netlify.app/databases/mysql/1e632c5a4e/