Scrivio Interview library
DesignAdvanced7 min read · updated 12 November 2026

Teams across your company use different primary key styles in MySQL: auto-increment BIGINT, CHAR(36) UUIDs, and composite natural keys. Propose a standard and the migration approach for existing tables.

I would standardise on a BIGINT auto-increment key, or a time-ordered UUIDv7 stored as BINARY(16) when ids must be created outside the database, and discourage random UUIDs stored as CHAR(36). Public identifiers can be a separate column when ids must not be guessable, and composite natural keys are allowed only when they are stable and match the main access pattern, with the reason written down. Existing tables move only where we can show a real cost, using online schema change tools and expand and contract steps, and shared libraries make the standard the easy path.

Why this question is asked

Primary key style looks like a matter of taste until a table reaches a few hundred million rows. In InnoDB the primary key is the physical order of the table and is copied into every secondary index, so a poor choice shows up as slow inserts, bloated indexes and a buffer pool that no longer holds the working set. Interviewers ask this at senior level because it combines database internals with the harder part: getting many teams to change habits without a risky big-bang migration. In this answer, we will recap why the key matters so much in InnoDB, compare the three styles, propose the standard, look at public ids and natural keys, and then plan the migration and the tooling.

1Why the Primary Key Shapes an InnoDB Table

InnoDB keeps every table in a B+tree ordered by its primary key, called the clustered index, and the rows live in its leaf pages. Two consequences follow. First, the order in which key values arrive decides where inserts land: increasing values append to the last page, while random values land anywhere in the tree and force pages to split. Second, every secondary index entry stores the primary key value as its pointer to the row, so a wide key makes every index on the table wider.

So the key decides three things we can measure: index size, insert latency, and how much buffer pool, InnoDB's page cache, the table needs.

2Comparing the Three Styles in Use Today

The table below compares the three styles our teams use with the UUIDv7 option we will propose. The important difference between the two UUID kinds is ordering: a version 4 UUID is random, while a version 7 UUID begins with a 48-bit millisecond timestamp, so values created later sort later, much like an auto-increment number.

Primary key styles in InnoDB
StyleSizeInsert patternNotes
BIGINT AUTO_INCREMENT8 bytesappend at the endsimple; guessable; created by the database
CHAR(36) random UUID36 bytes or morerandom, many page splitsdiscouraged
BINARY(16) UUIDv716 bytesnearly appendcreated anywhere; leaks creation time
Composite natural keyvariesdepends on the columnsonly if stable and matches main query

Notice the CHAR(36) row. A string UUID takes 36 bytes instead of 16, which is more than twice the size in every secondary index entry, and string comparison with a collation is slower than comparing raw bytes. Combined with random insert positions, this is the style that most often turns into buffer pool pressure as a table grows.

3The Proposed Standard

The default for new tables is a BIGINT UNSIGNED AUTO_INCREMENT primary key. When a service must create ids before it talks to the database, for example to make retries idempotent, to merge data from several shards, or to publish an event with the id already in it, the standard is a UUIDv7 stored as BINARY(16).

SQL
CREATE TABLE payment (
id BINARY(16) NOT NULL, -- UUIDv7 created by the application
account_id BIGINT UNSIGNED NOT NULL,
amount BIGINT NOT NULL,
PRIMARY KEY (id),
KEY ix_account (account_id)
) ENGINE=InnoDB;
-- Reading it back in a readable form:
SELECT BIN_TO_UUID(id) FROM payment LIMIT 1; -- 01932c4e-7a1b-7c3d-9f00-2a6b8e1d4c55
-- Do not pass the swap flag (UUID_TO_BIN(x, 1)) for UUIDv7: it reorders the
-- fields of time-based version 1 UUIDs and would break the v7 ordering.

We generate the value in the application because MySQL's UUID() returns a version 1 UUID and, as far as the versions we run go, there is no built-in UUIDv7 generator; check your version. The note about the swap flag matters in practice: it was designed for version 1 values, whose timestamp fields are stored in the wrong order for sorting. UUIDv7 is already in the right order, so we store its bytes as they are.

4Public Identifiers and Natural Keys

Sometimes the internal key must not appear in URLs. Sequential ids let anyone guess the next customer's id and estimate our order volume from two receipts. The standard handles this with a separate public identifier: a public_id column with its own unique index, filled with a random value, while the BIGINT stays the clustered key and the target of foreign keys. Notice that UUIDv7 is not a good public id when secrecy matters, because its first 48 bits reveal when the row was created.

Composite natural keys are allowed as an exception, not as a default. A table like order_line keyed by (order_id, line_no), or a time series keyed by (device_id, ts), stores rows exactly in the order the dominant query reads them, which can save many random reads. The conditions are that the values never change, because changing a primary key rewrites the row and every secondary index entry, and that the team writes down in a short design record which query the key serves and what it costs on inserts.

5Migrating Existing Tables Only Where It Pays

Changing the primary key of a large live table is one of the riskiest schema changes there is, so the standard applies to new tables at once, and to existing tables only when we can show a cost. We look for evidence: tables whose indexes no longer fit the buffer pool (a falling hit ratio and rising Innodb_buffer_pool_reads), index sizes from mysql.innodb_index_stats that are several times the data size, or insert p99 latency that grows with the table.

When a table does qualify, the key change itself runs through an online schema change tool such as gh-ost or pt-online-schema-change, which copy the table in chunks while traffic continues. The harder part is everything that references the old key: other tables, caches, events and other services. For those we use expand and contract steps, so every step is reversible and each deploy stays compatible with the one before.

Step throughExpand and contract for a key change
  1. Add the new columnnullable, with its own unique index
  2. Backfill in batchesthrottled on replica lag
  3. Write both on every insertapplication release 1
  4. Add new foreign key columnsreferencing tables backfilled too
  5. Switch readers to the new keyrelease 2, behind a flag
  6. Any reads of the old column for 2 weeks?
    YesFix the caller and waitNoContract: swap the PK, drop old columns

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

Old and new identifiers live side by side until every reader has moved, and only the last step is irreversible.

6Making the Standard the Easy Path

A standard that needs every engineer to remember the details will drift. We provide a small shared library that creates UUIDv7 values, since the JDK's UUID.randomUUID() produces version 4, and a ready JPA mapping so a team gets the right column type without thinking about it. We also add a check to migration review that flags new CHAR(36) keys and tables without a primary key.

Java
@Entity
class Payment {
@Id
@JdbcTypeCode(SqlTypes.BINARY) // stored as BINARY(16), not as a string
@Column(columnDefinition = "BINARY(16)")
private UUID id = Uuids.v7(); // our shared helper; returns a time-ordered UUID
private long accountId;
private long amount;
}
// Uuids.v7().version() // 7

Recent Hibernate versions also offer a time-ordered UUID generator style, which removes the need for the helper; check your version before standardising on it.

7Follow-up questions

Why not just use random UUIDv4 values stored as BINARY(16)?

BINARY(16) fixes the size problem but not the ordering problem. Random values still land in random leaf pages, so a large table suffers page splits, half-full pages and a working set that is the whole index. UUIDv7 keeps the size of 16 bytes and inserts close to the end of the tree, so it gets almost the same insert behaviour as an auto-increment key.

Is auto-increment a problem for sharded or multi-writer setups?

It can be, because two shards would create the same values, and Group Replication in multi-primary mode needs auto_increment_increment and offset settings to avoid clashes. That is exactly the case where the standard allows UUIDv7: every node creates unique, ordered ids without coordination.

8Conclusion

In this answer, we saw why the primary key matters so much in InnoDB: it orders the table and is copied into every secondary index. We proposed BIGINT auto-increment as the default and UUIDv7 in BINARY(16) when ids must be created outside the database, discouraged CHAR(36) random UUIDs, kept public ids separate when ids must not be guessable, and allowed documented, stable natural keys. Existing tables migrate only where measurements show a cost, through online schema change tools and expand and contract steps, and shared libraries make the standard easy to follow.

What to say in the interviewAbout 90 seconds aloud

Open with

I would start from how InnoDB works: the primary key is the clustered index and it is copied into every secondary index, so its size and its insert order matter.

Then, if they want more

The default I would propose is BIGINT auto-increment, because it is 8 bytes and appends at the end of the tree. When a service has to create ids itself, for idempotent retries, events or sharding, the standard is UUIDv7 stored as BINARY(16), which is 16 bytes and time ordered, so it inserts almost like a sequence.

I would discourage random UUIDs stored as CHAR(36), which are more than twice as wide and insert all over the tree. If ids must not be guessable, I keep the internal key and add a separate public id with a unique index, and I would not use UUIDv7 for that because it leaks creation time.

Composite natural keys are allowed when they are stable and match the main access pattern, with the reason documented. For existing tables, I would not migrate everything.

I would migrate only where we see real cost, like buffer pool pressure, oversized indexes or rising insert latency, using gh-ost or pt-online-schema-change for the table and expand and contract steps for every reference. Finally I would ship a shared UUIDv7 helper, a JPA mapping and a migration lint rule, so following the standard is the easiest thing to do.

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
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 "Teams across your company use different primary key styles in MySQL: auto-increment...": https://scrivio-interview-library.netlify.app/databases/mysql/89386bbf4a/