Skip to content

MySQL and InnoDB in depth · Part 6 of 10

InnoDB indexes: B+Tree, clustered, secondary and covering

Every InnoDB table is a B+Tree ordered by its primary key. Understand clustered and secondary indexes, the extra lookup they cause, and how covering indexes remove it.

· 4 min read

mysqlinnodbindexing

An index is a sorted copy of some columns that lets the database find rows without reading the whole table. In InnoDB the structure behind every index is a B+Tree, and the way InnoDB uses it for the table itself is what makes its performance rules different from other engines.

B+Tree in one paragraph

A B+Tree is a balanced, shallow tree. Internal pages hold keys and pointers, and the leaf pages hold the actual entries in sorted order, linked to each other. Finding a key walks from the root to a leaf in a handful of page reads, even for hundreds of millions of rows, which is why lookups, ranges and ordered scans are all logarithmic. InnoDB pages are 16 KB by default, so each page holds many entries and the tree stays only a few levels deep.

The clustered index is the table

InnoDB stores the table rows inside the B+Tree of the primary key. This is the clustered index: its leaf pages contain the complete rows. A table has exactly one, because the rows can only be physically ordered one way. If you do not define a primary key, InnoDB uses the first unique non-null index, or invents a hidden 6-byte row id.

Two consequences follow. Looking up by primary key is the fastest access path, because one tree walk reaches the whole row. And rows with neighbouring primary keys live on neighbouring pages, so ordered range scans on the key are cheap.

Secondary indexes store the primary key

Any other index is a secondary index. Its leaf entries contain the indexed columns plus the primary key value of the row, not a physical address. A query that uses a secondary index and needs a column that is not in it does two steps: find the primary key in the secondary index, then walk the clustered index with that key to fetch the row. This second step is often called a lookup or bookmark lookup, and it is random I/O.

The primary key is therefore copied into every secondary index. A wide primary key, such as a long string, makes every index larger and slower, so keep it short.

Covering indexes

If a secondary index contains every column a query needs, InnoDB can answer entirely from the index and skip the lookup. This is a covering index, and EXPLAIN shows Using index in the Extra column. The primary key is always available inside a secondary index, which helps.

SQL
CREATE TABLE payments (
  id         bigint PRIMARY KEY,
  wallet_id  bigint NOT NULL,
  status     varchar(20) NOT NULL,
  amount     bigint NOT NULL,
  created_at timestamp NOT NULL
);

CREATE INDEX idx_wallet_created ON payments (wallet_id, created_at, amount);

-- answered from the index alone: wallet_id, created_at, amount and id are all in it
SELECT created_at, amount FROM payments
WHERE wallet_id = 7 AND created_at >= '2026-01-01';

Covering indexes are powerful for hot read paths, but every extra column makes the index larger and every write more expensive. Add them for queries that matter, not for all of them.

Choosing a primary key

  • Short. It is copied into every secondary index, so a bigint is much cheaper than a 36-character string.
  • Increasing. An auto-increment or time-ordered id appends to the right edge of the tree. Random keys such as UUID version 4 insert into random pages, cause page splits and fragmentation, and keep far more of the tree hot in memory.
  • Stable. Updating a primary key moves the whole row and rewrites every secondary index entry for it.
  • If you need globally unique ids, prefer a time-ordered scheme (a snowflake-style id or UUID version 7) over random UUIDs, or store the UUID in a separate unique column.

Takeaway

In InnoDB the table is its primary-key B+Tree, and secondary indexes point back to rows through the primary key. That explains the double lookup, the value of covering indexes and the case for short, increasing keys. When a query is slow, ask how many tree walks it performs and whether the index can answer it alone.

A knowledge article, drafted with AI assistance and checked with automated tests. It is general background, not a description of my own experience or of any client's work.

Comments

Leave a comment

No account or email needed. Comments appear after review. A one-way hash of your IP address is kept for 30 days to fight spam, then erased.