Databases 5 min read

How Many Levels Does a MySQL InnoDB B+ Tree Need for 20 Million Rows?

This article explains MySQL's InnoDB storage architecture, how data and indexes are organized in 16 KB pages, calculates the capacity of B+ tree nodes, and determines that a table with 20 million rows requires a three‑level B+ tree under typical assumptions.

Su San Talks Tech
Su San Talks Tech
Su San Talks Tech
How Many Levels Does a MySQL InnoDB B+ Tree Need for 20 Million Rows?

Hello, I am Su San.

MySQL's default storage engine is InnoDB, which supports transactions, row‑level locking, primary keys, foreign keys, and stores indexes using B+ trees.

InnoDB Storage

In InnoDB, data is stored as an index‑organized table; each index corresponds to a B+ tree. While a disk sector is 512 B and a filesystem block is 4 KB, InnoDB stores data in 16 KB pages.

InnoDB page structure
InnoDB page structure

B+ Tree Index

Unlike MyISAM, InnoDB uses a clustered index where leaf nodes store the row data. The primary‑key index leaf contains the primary key and all remaining columns; secondary index leaves store the index key and the primary‑key value as a pointer.

MyISAM storage structure
MyISAM storage structure

InnoDB primary‑key index leaf nodes contain the primary key and all other fields. Secondary index leaf nodes store the index key and primary‑key value, acting as a pointer to the row.

InnoDB index structure
InnoDB index structure

Non‑leaf (index) nodes store only the key and a pointer, enabling O(log n) search to quickly locate the data page.

B+ tree node layout
B+ tree node layout

If a row occupies 1 KB, each 16 KB page can hold 16 rows. Assuming a bigint key (8 B) and a fixed 6 B pointer, an index entry is 14 B, so a page can store 16 KB / 14 B ≈ 1170 index entries.

16 * 1024 B / 14 B = 1170.

Therefore, a 2‑level B+ tree can store 1170 * 16 = 18 720 rows; a 3‑level tree can store 1170 * 1170 * 16 ≈ 21 902 400 rows. For a table with 20 million rows, the B+ tree height is three layers (since 20 M < 21.9 M). This calculation assumes each row is 1 KB and each index entry is 14 B.

Summary

This classic interview question reveals InnoDB's storage structure, helping readers understand index organization and data lookup principles.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

performanceInnoDBmysqlB+Treedatabase indexing
Su San Talks Tech
Written by

Su San Talks Tech

Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.