How Do B+ Tree Indexes Reduce Query Cost?
The Problem Indexes Solve
Without an index, finding a single row in 1 million rows requires a full table scan. At 100 rows/page and 0.1ms per disk read, that is 10,000 page reads taking 1 second. With a B+ tree index: 3-4 page reads taking 0.4ms. That is 2500x faster.
B+ Tree Structure
The tree has two node types. Internal nodes contain only keys and pointers to children. They act as routing guides. Leaf nodes contain the actual data (or pointers to rows) and are linked together in a doubly-linked list. This linking is crucial: once you find the starting point for a range query like WHERE id BETWEEN 60 AND 90, you simply follow the links without climbing back up the tree.
How Lookup Works
To find key 75: start at root, compare 50 < 75 < 100, follow middle pointer. At internal node [60|75|90], find exact match, follow pointer to leaf containing that row. Total: 3 page reads regardless of table size. Each level multiplies capacity: with 500 keys per node, a 3-level tree indexes 125 million rows (500³).
The Cost of Indexes
Indexes are not free. Every insert, update, or delete must also update all indexes on that table. 5 indexes means 6 writes per row change. Indexes also consume storage, typically 10-30% of table size per index. Rule of thumb: index columns in WHERE, JOIN, and ORDER BY clauses that filter large tables. Skip indexes on small tables or rarely-queried columns.