# B树

B树（B-tree）是一种平衡的多路搜索树，其中每个节点都保存许多键和子节点。这种设计使得在大型有序索引中进行搜索、插入和删除时，只需对磁盘等慢速存储进行少量访问。按标准定义，每个非根内部节点拥有 ⌈m/2⌉ 到 m 个子节点，其中固定整数 m > 1 称为阶；叶节点没有子节点；根节点遵循单独规则，可以少至 2 个子节点。由于 m 很大时树高——也就是访问次数——仍然很小，这种结构适合主要存放在磁盘上的数据。<sup>[1](https://xlinux.nist.gov/dads/HTML/btree.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 定义 | 平衡搜索树；每个非根内部节点有 ⌈m/2⌉ 到 m 个子节点，m 为阶，叶节点没有子节点，根节点可少至 2 个子节点<sup>[1](https://xlinux.nist.gov/dads/HTML/btree.html)</sup> |
| 起源 | 鲁道夫·拜尔（Rudolf Bayer）与爱德华·M·麦克雷特（Edward M. McCreight），波音科学研究实验室（Boeing Scientific Research Laboratories），《大型有序索引的组织与维护》（"Organization and Maintenance of Large Ordered Indices"），1970 年研讨会论文，期刊版载于《Acta Informatica》第 1 卷，第 173–189 页，1972 年<sup>[2](https://rtheunissen.github.io/bst/docs/references/1972_bayer_mccreight.pdf)</sup><sup> • </sup><sup>[3](https://datasystem.school/data-system-papers-distilled/papers/btree.html)</sup> |
| 名称 | 作者从未解释“B”的含义；可能的解读包括平衡（balanced）、宽广（broad）、繁茂（bushy）、波音（Boeing）或拜尔（Bayer）<sup>[1](https://xlinux.nist.gov/dads/HTML/btree.html)</sup> |
| B+树（B+ tree）不变式 | 数据记录只在叶节点；一个叶节点保存 ⌈L/2⌉ 到 L 条记录；所有叶节点在同一层；内部节点有 ⌈M/2⌉ 到 M 个子节点（根为 2 到 M）<sup>[4](https://cseweb.ucsd.edu/~kube/cls/100/Lectures/lec15.btree/lec15-16.html)</sup> |
| 数据库中的使用 | PostgreSQL 的 `CREATE INDEX` 默认索引类型；处理 <、<=、=、>=、>、`BETWEEN`、`IN`、`IS NULL` 以及左锚定 `LIKE` 模式<sup>[5](https://www.postgresql.org/docs/18/indexes-types.html)</sup> |
| 页大小 | InnoDB 默认索引页 16 KB；PostgreSQL 索引项限制在约三分之一页<sup>[6](https://dev.mysql.com/doc/refman/26.7/en/innodb-physical-structure.html)</sup><sup> • </sup><sup>[7](https://www.postgresql.org/docs/current/btree.html)</sup> |
| 搜索成本 | 约为 \( O(\log_f b_i) \) 次块访问，其中 f 是每个节点的扇出，\( b_i \) 是索引项数<sup>[8](https://www2.seas.gwu.edu/~simhaweb/dbase_notes_complete.pdf)</sup> |

## 起源

拜尔和麦克雷特要解决的问题是：在索引远超主存容量、必须驻留在磁盘或磁鼓上的情况下，如何为一个随时间变化的随机访问文件维护索引。他们的解决方案——论文自己的摘要中有所描述——是一种页大小的、始终平衡的有序索引，在磁盘上搜索和更新都保持低成本。<sup>[3](https://datasystem.school/data-system-papers-distilled/papers/btree.html)</sup> 1972 年，两位作者都在波音科学研究实验室；这项工作先在 1970 年发表在 ACM SIGFIDET 数据描述与访问研讨会（ACM SIGFIDET Workshop on Data Description and Access）上，后于 1972 年以《Acta Informatica》版本发表。<sup>[2](https://rtheunissen.github.io/bst/docs/references/1972_bayer_mccreight.pdf)</sup><sup> • </sup><sup>[3](https://datasystem.school/data-system-papers-distilled/papers/btree.html)</sup>

作者从未解释“B”的含义。1979 年综述《无处不在的B树》（"The Ubiquitous B-Tree"）的作者道格拉斯·科默（Douglas Comer）指出，“平衡”（balanced）、“宽广”（broad）或“繁茂”（bushy）都可能适用，也有人认为 B 代表 Boeing（波音），不过他认为，鉴于拜尔的贡献，把 B 树视为“拜尔”树是恰当的。<sup>[1](https://xlinux.nist.gov/dads/HTML/btree.html)</sup>

## 结构与不变式

B树节点对应一个磁盘块，树中存储的指针是包含子节点的块号，通常解释为相应文件中的偏移量。<sup>[9](https://opendsa-server.cs.vt.edu/ODSA/Books/jmu/cs240/fall-2019/Kirkpatrick/html/BTree.html)</sup> 搜索操作只访问从根到包含目标键的叶节点路径上的磁盘块；范围操作则可能继续穿过多个叶块；更新在重新分配或合并期间可能访问兄弟块，这就是为什么较大的阶 m 能让典型索引的访问次数降到一两次。<sup>[9](https://opendsa-server.cs.vt.edu/ODSA/Books/jmu/cs240/fall-2019/Kirkpatrick/html/BTree.html)</sup><sup> • </sup><sup>[1](https://xlinux.nist.gov/dads/HTML/btree.html)</sup><sup> • </sup><sup>[16](https://opendsax.cs.vt.edu/ODSA/Books/CS3/html/BTree.html)</sup>

B+树变体是大多数数据库系统实际部署的形式，它只把数据记录保存在叶节点中，内部节点纯粹用于引导搜索。其不变式为：一个叶节点总是保存 ⌈L/2⌉ 到 L 条数据记录；所有叶节点位于同一层；一个内部节点有 ⌈M/2⌉ 到 M 个子节点，根节点除外，根节点可以有 2 到 M 个子节点；内部节点的键数比子节点数少一，每个存储的键等于以对应子节点为根的子树中的最小键。内部节点中的键保存在有序数组中。<sup>[4](https://cseweb.ucsd.edu/~kube/cls/100/Lectures/lec15.btree/lec15-16.html)</sup> 叶节点之间的兄弟指针支持范围搜索，即使叶页在磁盘上未必物理连续。<sup>[10](https://www3.cs.stonybrook.edu/~sas/courses/cse305/lectures/ch9.pdf)</sup>

**插入与删除。** 查找键很直接；插入和删除更复杂，因为它们必须保持不变式。大多数插入和删除都很简单，只有插入遇到满节点，或删除使节点不足半满时才会变复杂。<sup>[4](https://cseweb.ucsd.edu/~kube/cls/100/Lectures/lec15.btree/lec15-16.html)</sup><sup> • </sup><sup>[11](https://www.auhd.edu.ye/upfiles/elibrary/Azal2020-01-22-11-22-01-90751.pdf)</sup> 在拜尔和麦克雷特的方案中，只有当相邻的两个兄弟页都满时，页才分裂；否则兄弟页之间的溢出可避免分裂，而下溢不会传播。<sup>[2](https://rtheunissen.github.io/bst/docs/references/1972_bayer_mccreight.pdf)</sup> 他们的成本分析表明，混合插入与删除之间的相互干扰相对于纯工作负载最多使性能降至原来的三分之一；在他们的实验中，带溢出的方案比不带溢出的方案表现更好。<sup>[2](https://rtheunissen.github.io/bst/docs/references/1972_bayer_mccreight.pdf)</sup>

## 数据库系统中的B树

对于需要插入、删除和键范围搜索的应用，B树或其某种变体是标准文件组织方式。<sup>[9](https://opendsa-server.cs.vt.edu/ODSA/Books/jmu/cs240/fall-2019/Kirkpatrick/html/BTree.html)</sup> 在 PostgreSQL 中，`CREATE INDEX` 命令默认创建 B 树索引；只要索引列参与 <、<=、=、>= 或 > 比较，查询规划器就会考虑使用 B 树索引，包括 `BETWEEN` 和 `IN` 等等价结构，以及 `IS NULL` 和 `IS NOT NULL` 条件。B树索引还支持左锚定模式匹配，例如 `col LIKE 'foo%'`，并能按排序顺序检索行。<sup>[5](https://www.postgresql.org/docs/18/indexes-types.html)</sup> 任何能排序为明确定义线性顺序的数据类型都可以建索引，限制是索引项压缩后不能超过约三分之一页。<sup>[7](https://www.postgresql.org/docs/current/btree.html)</sup>

PostgreSQL 的实现是一棵多级树，每一级构成一个页的双向链表，元页位于第一个段文件起始处的固定位置。叶页是最低层，包含指向表行的元组；内部页指向下一层。通常超过 99% 的页都是叶页。<sup>[7](https://www.postgresql.org/docs/current/btree.html)</sup>

在 MySQL 的 InnoDB 引擎中，除空间索引（使用 R树（R-tree））外，所有索引都是 B 树数据结构，索引记录存储在叶页中。默认索引页为 16 KB，由实例初始化时的 `innodb_page_size` 设置。当记录插入聚簇索引时，InnoDB 会尽量为每页留出 1/16 的空间供未来增长；顺序插入的页最终约 15/16 满，而随机顺序插入会使页处于 1/2 到 15/16 满之间。批量创建索引使用由 `innodb_fill_factor` 设置控制的排序索引构建。<sup>[6](https://dev.mysql.com/doc/refman/26.7/en/innodb-physical-structure.html)</sup> B树也出现在数据库之外：NTFS 将大型目录组织为 B 树，其中记录指向外部簇；Btrfs 文件系统将元数据存储在 B 树中，其上层节点只保存键与块指针对。<sup>[12](https://ntfs.com/ntfs-mft.htm)</sup><sup> • </sup><sup>[13](https://btrfs.readthedocs.io/en/latest/dev/dev-btrfs-design.html)</sup>

## B树与LSM树（LSM-tree）

对于写密集型存储引擎，现代的主要替代方案是日志结构合并树，它被 BigTable、Cassandra 和 RocksDB 等 NoSQL 产品采用，因为它被认为比支撑几乎所有关系数据库系统的 B+树具有更好的存储空间效率和更低的写放大（物理写入数据量超过逻辑写入数据量，损害性能并加剧磨损）。<sup>[14](https://www.usenix.org/publications/loginonline/revisit-b-tree-vs-lsm-tree-upon-arrival-modern-storage-hardware-built)</sup>

**硬件改变比较格局。** 内置透明压缩的存储硬件，如全闪存阵列和[计算存储](https://www.edgepedia.org/zh/computational-storage)驱动器，无需操作系统或应用参与即可无损压缩数据。2021 年和 2022 年发表的研究认为，这种硬件缩小了两种结构之间的物理存储成本差距，并为进一步直接降低 B 树写放大创造了空间。三项设计技术——确定性页影子、局部化页修改日志和稀疏重做日志——在带内置压缩的商用驱动器上把实测 B 树写放大降低了 10 倍以上；由此得到的 B 树在写放大和物理存储使用上达到与 RocksDB 相近或更小的水平。<sup>[15](https://arxiv.org/abs/2107.13987)</sup> 在一项记录大小为 32 B、4 个客户端线程的比较中，WiredTiger 在 8 KB 页大小下的写放大为 268，是 RocksDB 的 38 的 7.1 倍；而改进后的 B 树达到 28，约为 RocksDB 数值的 74%。<sup>[14](https://www.usenix.org/publications/loginonline/revisit-b-tree-vs-lsm-tree-upon-arrival-modern-storage-hardware-built)</sup> 同一项工作指出，B树写放大仅弱依赖于数据集大小，而 LSM 树写放大会随数据集增大而随层数增长。<sup>[15](https://arxiv.org/abs/2107.13987)</sup>

## References

1. [Paul E. Black, "B-tree", Dictionary of Algorithms and Data Structures, NIST, 15 May 2024](https://xlinux.nist.gov/dads/HTML/btree.html)
2. [R. Bayer and E. McCreight, "Organization and Maintenance of Large Ordered Indices" (1972 paper PDF)](https://rtheunissen.github.io/bst/docs/references/1972_bayer_mccreight.pdf)
3. [Organization and Maintenance of Large Ordered Indices, Papers Distilled](https://datasystem.school/data-system-papers-distilled/papers/btree.html)
4. [B-tree operations: insert and delete, UCSD CSE 100 lecture notes](https://cseweb.ucsd.edu/~kube/cls/100/Lectures/lec15.btree/lec15-16.html)
5. [PostgreSQL 18 Documentation, 11.2 Index Types](https://www.postgresql.org/docs/18/indexes-types.html)
6. [MySQL Reference Manual, 17.6.2.2 The Physical Structure of an InnoDB Index](https://dev.mysql.com/doc/refman/26.7/en/innodb-physical-structure.html)
7. [PostgreSQL Documentation, 65.1 B-Tree Indexes](https://www.postgresql.org/docs/current/btree.html)
8. [Database systems course notes, George Washington University](https://www2.seas.gwu.edu/~simhaweb/dbase_notes_complete.pdf)
9. [B-Trees, OpenDSA JMU CS240](https://opendsa-server.cs.vt.edu/ODSA/Books/jmu/cs240/fall-2019/Kirkpatrick/html/BTree.html)
10. [CSE 305 lecture notes, Stony Brook University](https://www3.cs.stonybrook.edu/~sas/courses/cse305/lectures/ch9.pdf)
11. [Elmasri & Navathe, Fundamentals of Database Systems (PDF excerpt)](https://www.auhd.edu.ye/upfiles/elibrary/Azal2020-01-22-11-22-01-90751.pdf)
12. [NTFS Master File Table, NTFS.com](https://ntfs.com/ntfs-mft.htm)
13. [Btrfs design documentation](https://btrfs.readthedocs.io/en/latest/dev/dev-btrfs-design.html)
14. [Revisiting B+-tree vs. LSM-tree, USENIX ;login:, March 2022](https://www.usenix.org/publications/loginonline/revisit-b-tree-vs-lsm-tree-upon-arrival-modern-storage-hardware-built)
15. [Closing the B-tree vs. LSM-tree Write Amplification Gap on Modern Storage Hardware with Built-in Transparent Compression, arXiv:2107.13987](https://arxiv.org/abs/2107.13987)
16. [12.6. B-Trees, CS3 Data Structures & Algorithms](https://opendsax.cs.vt.edu/ODSA/Books/CS3/html/BTree.html)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Algorithms and computational methods › Data structures › Trees

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/b-tree

English original: https://www.edgepedia.org/b-tree
