B+tree in 3D: how InnoDB finds a row

The real shape of a clustered index and a secondary index, drawn page by page.

Index access 3D view MySQL 8.4 · MariaDB 11.4

Drag to turn · scroll or pinch to zoom · right-drag to pan · keys: arrows pan, Shift+arrows turn, + and − zoom

    Learn more: what this model assumes

    How full the pages are. Like the 2D lesson, the numbers are a first-order estimate. Each page holds as many records as fit after about 120 bytes of page headers and the two system records (the page directory and the 8 byte trailer are left out). Clustered leaf pages keep one sixteenth of the page free: when rows arrive in key order, InnoDB splits a page rather than fill that space, so later updates have room. Pages that take random inserts, like the leaves of idx_users_email when users arrive in id order, split in the middle and are often much emptier, so a real secondary index can have more leaf pages than shown.

    What a record costs. A clustered leaf record is the row's column data plus a record header of at least 5 bytes and two hidden columns, the 6 byte transaction id and the 7 byte rollback pointer. The header also holds null flags and a length byte or two for each variable-length column; count those in the row and email sizes. A pointer in a non-leaf page is the key plus the 5 byte header and a 4 byte child page number. A secondary index record holds the email and the primary key, and so do its pointer records, which is why a long primary key makes every secondary index bigger.

    The second lookup starts at the root. A secondary index leaf stores the primary key, not where the row lives. InnoDB fetches the row by searching the clustered index for that key from its root, so a non-covering lookup reads both trees from top to bottom. The adaptive hash index can jump straight to a leaf page for keys it has seen often; it is off by default in MySQL 8.4 and MariaDB 11.4.

    Neighbors are linked. Every page's file header records the previous and next page on the same level. A range scan uses the leaf level's links to move to the next page. InnoDB hands rows to the server in small batches and remembers where it stopped; if another session changed that page in the meantime, it finds its place again from the root.

    A tiny table is one page. When every row fits in one page, that page is the root and the leaf. The 2D lesson always draws at least two levels to keep its picture simple.

    How the picture is drawn. Levels with more than 20,000 pages are drawn as one block per parent page; the blocks the query touches open up into single pages. The readout counts are exact for the model either way.