B-Tree Index Explained Visually

A B-tree keeps keys sorted in pages, so a lookup walks root, branch and leaf. See how it finds one row and why it stays balanced.

In this chapter

A B-tree puts many keys in one page

B-tree indexes: common questions

B-tree or B+tree: which do databases use?

Most use a B+tree: all rows or row pointers live in the leaf pages, which are linked so range scans can walk them in order.

More on Indexes: how they are built

Keep learning

Overview

chapters · indexes A B-tree finds one row in about 3 page reads, even among a million

1. Why not a sorted list or a binary tree?

Each simpler structure is fast at one thing and slow at another. The B-tree is the one that is good at all three jobs on disk. Unsorted list ✓ Insert: just add at the end.✕ Find: check every value, one by one. Sorted list ✓ Find: binary search, very few checks.✕ Insert: everything after the new value must move. Binary search tree ✓ Find and insert are both quick in memory.✕ On disk, every step down is a new page read. ✓ Find: a few page reads.✓ Insert: change one page, split it only when full.✓ Ranges: neighbours sit together. ! Why a sorted list hates inserts [10, 20, 40, 50] + insert 30 → move 40 and 50 one place right → [10, 20, 30, 40, 50]. With a million values, an insert near the start moves almost all of them. A B-tree only changes the one page where 30 belongs.

2. Count page reads, not comparisons

Binary tree: 1 key per page 50ptr · ptr · unused B-tree: hundreds of keys per page One read picks 1 of ~100 branches (the fan-out). 100 → 10,000 → 1,000,000: a million keys need about 3 page reads. If one page read costs 10 ms (a teaching number, not a modern SSD) Pages for 1M keys Binary tree about 20 about 200 ms about 100 about 3 about 30 ms Searching 500 keys inside a page that is already in memory is cheap. Reading the next page is the expensive step, so it pays to do more work per page if it saves a read. i The B+ tree: the version databases actually use

3. Inside the tree: find, insert, delete

Every leaf sits at the same depth, so every lookup costs the same number of page reads, whichever key you ask for. Walk down the tree to see it.

Why the leaves are linked

Each leaf points to the next leaf and the previous one. A range query (BETWEEN, >, ORDER BY) goes down the tree once to its first leaf, then just steps sideways along the chain. ORDER BY … DESC walks the same chain backwards. No trip back up the tree is needed.

What happens when pages fill up or empty out

Insert into a full page: split Leaf [10 20 30 40] is full and 25 arrives. The page splits into [10 20] and [25 30 40], and 25 is copied up to the parent as a new signpost. If the parent is full it splits too. When the root splits, the tree gains a level at the top, so every leaf stays at the same depth. Deletes empty a page: merge

B-tree vs B+ tree

The textbook B-tree and the B+ tree databases actually use B+ tree Where rows live In every page, upper pages included Only in the leaves Upper pages hold Keys and their rows Keys only, as signposts Keys per upper page Fewer, because rows take room More, so each page has more branches and the tree is shorter Range query Go up and down the tree to reach the neighbours Find the first leaf, then follow the leaf links

Every write updates every index

What one change to the table does to each index on it Change to the table Change to each index INSERT a row A new entry goes into the right leaf. If that leaf is full, it splits. DELETE a row Its entry is removed (often only marked, and cleaned up later). UPDATE a row If an indexed column changed, the old entry is removed and a new one added in its new place. Otherwise the index is untouched. Later in this module: why an index lookup can still be slow, even though the tree walk is only 3 pages.

More courses

  • Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
  • Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
  • React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
  • System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
  • NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
  • Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
  • Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
  • AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
  • Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
  • TypeScript in depth — The type system as a design tool. Inference, narrowing, generics, and the errors that mean the model is wrong — not a catalog of utility types.
  • CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
  • Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
  • Docker — What an image is, why layers cache, and how containers find each other on a network.
  • Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
  • Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
  • AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
  • Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.