Java Interview Questions

Why MySQL InnoDB Uses B+ Trees for Indexes

"Why does MySQL InnoDB use a B+ tree for its index, and not a B-tree, hash, or binary search tree?"

Why Interviewers Ask This

This is THE defining MySQL interview question at every backend interview. Interviewers want to hear that you understand disk access patterns, the shape of the B+ tree, and why leaf-linked pages enable range scans — not just that "B+ tree is good."

How to Answer

  1. 1

    Frame it around disk I/O: the cost of index operations is dominated by page reads. Any index design is about minimizing the number of disk page accesses.

  2. 2

    Compare alternatives: binary search tree has too many levels (log base 2 of N pages) — a million rows would be ~20 levels. B-tree stores data in every node so each node holds fewer keys. B+ tree stores keys in internal nodes and data only in leaves, so each internal node fits many more keys, making the tree wider and shallower.

  3. 3

    Show the scale: with 16KB pages and ~15 bytes per key-pointer pair, one page holds ~1000 keys. A 3-level B+ tree indexes ~1 billion rows with at most 3 page reads — and the top 1-2 levels are always cached in the InnoDB buffer pool.

  4. 4

    Explain the leaf link: all leaves are connected by a doubly linked list in key order. Range queries (BETWEEN, ORDER BY) traverse leaves sequentially without re-walking the tree — critical for SQL performance.

  5. 5

    Cover InnoDB specifics: the clustered index stores the full row in the leaf (so primary key lookup is one tree walk); secondary indexes store the primary key in the leaf (so they require a second walk back through the clustered index — the "back to table" cost). Covering indexes avoid this by including all needed columns directly.

Sample Answer

The choice of B+ tree comes down to optimizing disk I/O. Index operations on a real database are dominated by how many 16KB pages the engine has to read, so the shape of the tree matters enormously. A hash index is O(1) for point lookups but useless for range queries or ORDER BY, which is what most SQL workloads need. A binary search tree has log base 2 depth — a million rows means about 20 levels, so 20 disk reads per query worst case. A B-tree improves on that by fanning out per node, but stores actual row data in every node, which limits how many keys each node can hold. The B+ tree's key innovation is that internal nodes contain only keys and child pointers — no data — so each 16KB page fits around 1000 keys. With that fanout, three levels index roughly a billion rows, and the top one or two levels stay resident in the InnoDB buffer pool, so a primary key lookup typically costs one actual disk read. The second crucial property is the leaf linked list — every leaf page points to the next, so range scans like WHERE id BETWEEN 100 AND 200 or ORDER BY id just walk the leaves sequentially without climbing back up the tree. In InnoDB specifically, the primary key defines the clustered index — the table itself IS the B+ tree, with full rows stored in the leaves. Secondary indexes store the primary key as the leaf value, which means a query using a secondary index first walks the secondary tree to find the PK, then walks the clustered tree to get the row — the "back to table" cost. A covering index sidesteps this by including all the columns the query needs directly in the secondary index, so the query never touches the clustered tree. That's why composite and covering indexes are such an important tuning tool.

Pro Tips

  • Always quantify — "3-level B+ tree can index 1 billion rows" lands much harder than "it's shallow".

  • Be ready for the follow-up: "what's a covering index?" and "what's back to table (回表)?"

  • Mention the top levels of the B+ tree staying in buffer pool — shows you understand real-world performance.

  • If you blank on the specific numbers in a live interview, HireMe AI can surface the 16KB page and ~1000 keys per page fact.

Frequently Asked Questions

Can MySQL use hash indexes?

MEMORY engine uses hash indexes, and InnoDB has Adaptive Hash Index internally for hot pages, but the primary index is always B+ tree.

Why is primary key lookup faster than secondary index lookup?

Primary key is the clustered index — one tree walk returns the row. Secondary index requires an extra walk back to the clustered index (back to table).

How do I design a covering index?

Include all columns the query SELECTs and filters on in a single composite index, following left-prefix order matching the WHERE clause.

Worried About Going Blank in Your Interview?

HireMe AI listens to your interview in real-time, detects questions, and generates instant answer suggestions — so you never freeze up again.

Try HireMe AI Free