HeadlinesBriefing favicon HeadlinesBriefing.com

Why B+trees Power Database Indexes: A Practical Guide

Hacker News •
×

Ben Dicken published a comprehensive explainer on B-trees and database indexes at PlanetScale. The piece covers how B-trees work, why major database systems like MySQL, Postgres, MongoDB, and Dynamo rely on them, and what makes B+trees the preferred choice for modern indexes.

B-trees store key-value pairs in a tree structure optimized for disk-based storage. Each node can be sized to match disk block sizes (typically 16k), enabling efficient reads and writes. A three-level B+tree with 682 keys per node can store over 300 million key-value pairs while remaining just three levels deep.

B+trees improve on standard B-trees by storing values only at leaf nodes, allowing inner nodes to fit more keys and keep trees shallower. MySQL's InnoDB uses 16k B+tree nodes by default, storing all table data this way with the primary key as the tree key. The piece also explains why auto-incrementing integers often outperform UUIDs as primary keys—smaller keys mean more fit per node, resulting in faster lookups.