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.
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 → findP→Po→ exact page number in 3 hops!
2. Clustered vs Non-Clustered Index (Major Backend Interview Question!)
| Feature | Clustered Index (MySQL InnoDB Primary Key) | Non-Clustered / Secondary Index |
|---|---|---|
| How Rows Are Stored | The table rows themselves are physically sorted on disk by the PK | Separate B+ Tree storing (indexed_col → pointer/PK) |
| Max Per Table | 1 (a table can only be physically sorted one way!) | Many per table |
| Lookup Speed | 1 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!
-- 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 Clause | Uses idx_orders_city_status_date? | Why? |
|---|---|---|
WHERE city = 'Pune' | ✅ YES | Uses leftmost prefix (city) |
WHERE city = 'Pune' AND status = 'paid' | ✅ YES | Uses prefix (city, status) |
WHERE city = 'Pune' AND status = 'paid' AND created_at >= '2026-11-01' | ✅ YES | Uses 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>/</BETWEENrange 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:
-- 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, orDELETEmust 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 frequentWHERE/ORDER BYfilter patterns.
5. Quick Check Your Understanding
Module 8.1 Quick Quiz
Keep Exploring: Related Lessons & Tools
All Courses →Paste LeetCode-style level-order tree arrays, graph edge lists, or 2D matrices to render clean vector SVG diagrams and DFS traversals.
Paste any Python, JS, Java, or C++ snippet to estimate Time & Space complexity, or enter input size N to see which algorithms pass in 1s.