1 Understanding Indexes
TopAn index is a data structure that speeds up data retrieval. Think of it like the index at the back of a textbook — instead of reading every page to find "INNER JOIN", you look it up in the index and go directly to page 47. Without an index, the database scans every row in the table (full table scan) to find matches.
Without vs With an Index
-- This query on a 10-million-row table:
SELECT * FROM users WHERE email = 'sara@email.com';
-- Without index: scans all 10,000,000 rows → 3.2 seconds
-- With index on email: finds it in ~3 lookups → 0.001 secondsThat's a 3,200x speedup from a single index.
How Indexes Work: B-Tree
Most SQL indexes use a B-Tree (balanced tree) structure. The data is sorted and organized in a tree, so finding a value takes O(log n) operations instead of O(n):
| Table Rows | Full Scan (no index) | B-Tree Lookup (with index) |
|---|---|---|
| 1,000 | 1,000 comparisons | ~10 comparisons |
| 100,000 | 100,000 comparisons | ~17 comparisons |
| 10,000,000 | 10,000,000 comparisons | ~23 comparisons |
| 1,000,000,000 | 1 billion comparisons | ~30 comparisons |
What Gets Indexed Automatically?
| Constraint | Auto-Index? | Why |
|---|---|---|
PRIMARY KEY | Yes (always) | Must be unique and fast to look up |
UNIQUE | Yes | Database needs to check for duplicates on every INSERT |
FOREIGN KEY | Depends on DBMS | MySQL: yes (InnoDB). PostgreSQL: no (add manually!) |
| Regular columns | No | You must create these indexes yourself |
Analogy: A table without indexes is like a phone book sorted randomly. To find "Sara Ahmed", you'd read every entry. An index is like sorting the phone book alphabetically — you can jump to the "S" section and find Sara in seconds.
Key Takeaways
- An index is a sorted data structure that speeds up SELECT queries dramatically
- B-Tree indexes turn O(n) scans into O(log n) lookups
- PRIMARY KEY and UNIQUE columns are indexed automatically
- For other columns you search/filter on frequently, you must create indexes manually