Home›Learn›MySQL 1: why MySQL
Course · Inside one serverLesson 6 of 330 read

MySQL · InnoDB

Why is a MySQL table also its primary key, and what are the two logs for?

Chapter 1 · Why MySQL

Why so much of the web runs on MySQL

MySQL is the database I meet most often in large web companies, and it earned that place by being fast, simple to run, and very good at one thing: lots of small reads and writes by primary key.

The table is the index.

InnoDB stores every row inside the primary key's B-tree. A lookup by primary key goes straight to the row. Choose the key well and most queries are cheap.

Replication is built in and well understood.

The binlog has fed replicas, backups and change-data pipelines for decades. Huge fleets are built on it.

Operations are predictable.

Clear memory settings, a small set of logs, and tooling at every layer, from MySQL Shell to orchestrator and Vitess.

Where I would look elsewhere.

Complex analytics with many joins, rich data types, or extensions like PostGIS: PostgreSQL usually fits better.

This track tells MySQL as one story: what InnoDB is, how it stores your data, what happens on every write, how it stays up, how I run it, and what to do when it breaks.

This page is about one file per table. I show you how InnoDB stores a table as its own primary-key index, and how two logs split the jobs: redo for crashes, binlog for replicas. After it you will be able to find any table's .ibd file, read checkpoint age, check binlog retention, and tell whether undo is piling up.

MySQL · InnoDB: in one line

A relational database. Each table file is its primary-key index.

Good at: High-volume websites. Lookups by ID. Simple queries at huge scale. Operationally simple read replicas. Two logs: redo for crashes, binlog for replicas.

Runs as: one mysqld process, a thread per connection

Printable one-page cheat sheet for MySQL →

Follow it

Follow one read

Step through a primary key lookup in InnoDB, then compare it with PostgreSQL using the switch.

How many pages does InnoDB read to find one row by its primary key, in a table of a million rows?

Three: root, branch and leaf. In InnoDB the leaf holds the row itself, so there is no separate table page to read. PostgreSQL needs one more.

Your turn

You add an index on email to a large InnoDB table whose primary key is a UUID stored as CHAR(36). The new index is much bigger than the emails alone. Why, and what would you have done differently?

Write your answer down first. Then open mine.

Show my answer

Why: in InnoDB, every secondary index entry stores the primary key as its pointer to the row. Each email entry carries the 36-character UUID with it. A lookup by email finds the primary key in the secondary index, then walks the clustered index to get the row.

The other cost: random UUIDs land all over the clustered index, so inserts split pages and the buffer pool holds more pages than it needs to.

What I would do: store the UUID as BINARY(16) with UUID_TO_BIN(uuid, 1), which also reorders a version 1 UUID by time. Or use a BIGINT auto-increment primary key and keep the UUID as a unique secondary column for the outside world. Every secondary index gets smaller either way. Watch the difference in Watch an index grow.

Next · chapter 2How it stores your dataThreads, the buffer pool, files and logs
Connected

Where this shows up

The same mechanism, replication and failover, migrations and upgrades, sharding and partitioning, elsewhere on the site: in real incidents, in measurements, and in designs.

Check yourself · 4 questions

Check yourself

Pick an answer. You will see whether it is right, and why.

1. How are rows ordered inside an InnoDB table file?

2. Which log does crash recovery replay?

3. One snapshot stays open while other sessions update rows. Which number grows?

4. Why do random UUID primary keys slow inserts down?

One database decision a weekA short email each week: one real decision, the trade-off, and the command to check it. Unsubscribe in one click.

Part 1 of 4 · Learn · 56 pagesAll pages

How do databases actually work? From one row on disk to transactions: the words you need, PostgreSQL, MySQL and MongoDB from the files up, then Redis, Cassandra and vector search, side by side, and the query and concurrency traps.

Start 1: the basicsStart 2: ten conceptsStart 3: how a query travelsStart 4: napkin mathThe courseShort videosPostgreSQL 1: why PostgreSQLPostgreSQL 2: how it stores your dataPostgreSQL 3: what happens on every writePostgreSQL 4: high availability with PatroniPostgreSQL 5: running it as a DBREPostgreSQL 6: when it breaksPostgreSQL on Kubernetes: CloudNativePGMySQL 1: why MySQLMySQL 2: how it stores your dataMySQL 3: what happens on every writeMySQL 4: high availability with InnoDB ClusterMySQL 5: running it as a DBREMySQL 6: when it breaksMySQL failover with OrchestratorMySQL on Kubernetes: the Percona operatorsMongoDB 1: why MongoDBMongoDB 2: how it stores your dataMongoDB 3: what happens on every writeMongoDB 4: high availability with replica setsMongoDB 5: running it as a DBREMongoDB 6: when it breaksMongoDB on Kubernetes: the Percona operatorB-tree and LSM treeRedis 1: why RedisRedis 2: how it stores your dataRedis 3: high availabilityRedis 4: running it as a DBRERedis 5: when it breaksCassandra 1: why CassandraCassandra 2: how it stores your dataCassandra 3: high availabilityCassandra 4: running it as a DBRECassandra 5: when it breaksVector databases 1: why vector searchVector databases 2: how it stores your dataVector databases 3: high availabilityVector databases 4: running it as a DBREVector databases 5: when it breaksDatabase familiesEngines side by sideQueries that look fine and are notLost updates and write skewMany servers 1: keep a copy runningMany servers 2: who is the leaderMany servers 3: CAP as settingsMany servers 4: unique IDsMany servers 5: split the dataMany servers 6: across servicesMany servers 7: a thousand databasesStaff path, zero to architect