AlgoViz

B-tree

medium 26 min

A B-tree is a sorted, always balanced tree with hundreds of keys in each page-sized node.

Where it shows up

The same pattern appears across everyday software.

  • PostgreSQL

    Its default index type is a B-tree in which each level can be used as a doubly linked list of pages. Leaf pages hold pointers to table rows.

  • MySQL InnoDB

    Indexes are B-trees with records in the leaf pages and a default page of 16 KB. The table itself is the primary key index.

  • SQL Server and Oracle

    SQL Server calls its rowstore index a B+ tree and links the pages of each level both ways. Oracle's leaf blocks are doubly linked as well.

  • SQLite

    Uses two kinds: table b-trees keep all data in the leaves under a 64-bit integer key, and index b-trees hold arbitrary keys with no data.

  • MongoDB

    Its documentation says that indexes use a B-tree data structure, so document databases rely on it too.

  • File systems

    ext4 maps a file to disk blocks with a tree of extents, and Btrfs keeps its metadata in B-trees. Finding one block of a huge file is the same problem as finding one row.