SQL · Module 08: Indexes, ACID Transactions & OptimizationTutorial 22 of 24
All Tutorials·~1.5 hrs

8.1 B+ Tree Indexes, Composite Indexes & The Leftmost Prefix Rule

Understand how B+ Tree indexes turn 10-second full table scans into 2ms lookups, master the Leftmost Prefix Rule for multi-column composite indexes, and learn why over-indexing slows down writes.

01 Intuition: Plain-English mental model
02 Visuals: Step-by-step diagrams
03 Practice: Clean examples & tradeoffs

1. How a B+ Tree Index Turns O(N) Into O(log N)

Imagine looking up the word "PostgreSQL" in a 1,000-page textbook:

  • Without an index (Full Table Scan — O(N)): Read Page 1, Page 2, Page 3 ... up to Page 1,000!
  • With a B+ Tree Index (O(log N)): Jump straight to the sorted index at the back → find P → Po → exact page number in 3 hops!
Root: [ 50 | 100 ]Leaf: 10, 25, 42Leaf: 55, 72, 89Leaf: 105, 130, 199O(log N) disk hops: Find id = 72 in 2–3 hops instead of scanning 10,000,000 rows!
A B+ Tree keeps keys sorted across balanced nodes — finding 1 row out of 10,000,000 takes only 3–4 disk hops!

2. Clustered vs Non-Clustered Index (Major Backend Interview Question!)

FeatureClustered Index (MySQL InnoDB Primary Key)Non-Clustered / Secondary Index
How Rows Are StoredThe table rows themselves are physically sorted on disk by the PKSeparate B+ Tree storing (indexed_col → pointer/PK)
Max Per Table1 (a table can only be physically sorted one way!)Many per table
Lookup Speed1 lookup (leaf node is the row!)2 lookups (secondary index → PK/heap fetch), unless it's a Covering Index

(Note: PostgreSQL stores all tables in an unordered Heap and every index — including PK — points to the heap tuple ctid.)


3. Composite Indexes & The Leftmost Prefix Rule

You can index multiple columns together — order matters enormously!

SQL
-- Composite Index on (city, status, created_at)
CREATE INDEX idx_orders_city_status_date
ON orders (city, status, created_at);

Think of this like a phonebook sorted first by Last Name (city), then First Name (status), then Middle Name (created_at):

Query WHERE ClauseUses idx_orders_city_status_date?Why?
WHERE city = 'Pune'✅ YESUses leftmost prefix (city)
WHERE city = 'Pune' AND status = 'paid'✅ YESUses prefix (city, status)
WHERE city = 'Pune' AND status = 'paid' AND created_at >= '2026-11-01'✅ YESUses all 3 columns!
WHERE status = 'paid'❌ NO!Skipped leftmost column city!
WHERE created_at >= '2026-11-01'❌ NO!Skipped city and status!

Golden Composite Index Rule (Equality First, Range Last!):
Put = equality columns first (city, status) and > / < / BETWEEN range columns last (created_at).


4. Covering Indexes (INCLUDE) & The Trade-Off of Over-Indexing

If an index already contains every column your SELECT needs, the database performs a blazing-fast Index-Only Scan without ever touching the main table heap:

SQL
-- PostgreSQL Covering Index: answers SELECT amount FROM orders WHERE user_id = 42 from index alone!
CREATE INDEX idx_orders_user_covering
ON orders (user_id)
INCLUDE (amount, status);

Why Not Index Every Single Column?

  • Every INSERT, UPDATE, or DELETE must also update every index on that table — slowing down writes and consuming extra RAM/disk.
  • Rule of thumb: Index Primary Keys, Foreign Keys (used in JOINs), and your most frequent WHERE / ORDER BY filter patterns.

5. Quick Check Your Understanding

Module 8.1 Quick Quiz

Q1.You create a composite index on (department, role, salary). Which WHERE clause CANNOT efficiently use this index?
Q2.What is the main downside of adding 15 indexes to a write-heavy table?

Keep Exploring: Related Lessons & Tools

All Courses →