Advertisement
How Database Indexes Work — Why the Same Query Can Take 2ms or 8 Seconds
The phone book analogy that actually explains it, and how to find the problem with EXPLAIN
I once spent half a day chasing a "slow query" bug that turned out to be exactly this. 2 milliseconds versus 8 seconds. Same query, same table, same data. What separates those two numbers is almost never the query itself; it's whether the database had an index to jump to, or had to check every single row one by one to find what you asked for. Understanding that difference explains most of what "database performance" means in practice.
The Phone Book Analogy That Holds Up
Imagine looking up "Sharma" in a phone book that's sorted alphabetically — you flip to the S section directly, in seconds. Now imagine the same phone book with every entry in random order — you'd have to check every single page until you found it. An index is the alphabetical sorting. Without it, the database is doing the random-order search, checking every row, every time, no matter how simple the question.
What's Actually Happening Without an Index
Ask a database SELECT * FROM users WHERE email = 'x@example.com' on a table with no index on email, and it performs what's called a full table scan — it reads every single row in the table, checks if that row's email matches, and moves to the next one. On a table with 500 rows, you won't notice. On a table with 5 million rows, that's 5 million comparisons for one lookup, every single time that query runs.
What an Index Does
An index on a column builds a separate, pre-sorted structure (commonly a data structure called a B-tree) that maps values in that column directly to the rows that contain them. Instead of checking every row, the database jumps almost directly to the matching row, the same way you'd jump straight to the S section of the phone book. This is why adding one index can turn an 8-second query into an 8-millisecond one — you haven't changed the query or the data, you've changed how expensively the database has to search for it.
A Concrete Before-and-After
| Without Index | With Index | |
|---|---|---|
| Rows checked to find one match | All 5,000,000 | ~23 (tree depth, roughly) |
| Time on a large table | Seconds | Milliseconds |
| Cost to maintain | None | Slightly slower writes, more disk space |
Why You Can't Just Index Every Column
This is the part beginners consistently get wrong: indexes aren't free. Every index has to be updated every time a row is inserted, updated, or deleted — so an over-indexed table gets meaningfully slower to write to, even though it gets faster to read from. The real skill isn't "add indexes," it's identifying which specific columns get searched or filtered on often enough to justify that ongoing write cost. A column you filter on constantly (like email, user_id, or status) is usually worth indexing. A column you rarely query directly usually isn't.
Where Indexes Matter
- Columns used in
WHEREclauses — the single most common reason to add an index - Columns used in
JOINconditions — joining two large tables without an index on the join column is one of the most common causes of a slow application - Columns used in
ORDER BY— sorting a huge unindexed result set is expensive; an index in the right order can let the database skip the sort entirely - Foreign key columns — frequently joined against, and frequently forgotten when a table is first created
Where Indexes Don't Help (And Can Even Hurt)
- Small tables — a full scan of 200 rows is already fast; an index adds write overhead for no meaningful read benefit
- Columns with very few distinct values — indexing a boolean-like column (say, a status with only 3 possible values across 10 million rows) often doesn't help much, because the index still has to point to a huge chunk of matching rows
- Columns rarely used in a
WHERE,JOIN, orORDER BY— an unused index is pure cost, no benefit
How to Actually Find the Problem in a Real Database
Most relational databases (Postgres, MySQL) let you ask directly what a query is doing with EXPLAIN in front of it:
EXPLAIN ANALYZE SELECT * FROM users WHERE email = 'x@example.com';If the output shows "Seq Scan" (sequential/full table scan) on a large table, that's the database telling you exactly what's slow and why — it's checking every row because nothing tells it where to jump directly. Adding CREATE INDEX idx_users_email ON users(email); and re-running the same EXPLAIN should show "Index Scan" instead, with a dramatically lower estimated cost.
Read the Query Plan Before You Guess
That half-day I mentioned earlier ended with one CREATE INDEX statement. A slow query is almost never mysteriously "heavy." It's the database doing more manual searching than it needs to, because nothing tells it where to look directly. Learn to read EXPLAIN output before reaching for any other fix, index the columns that get searched and joined on often, and resist the urge to index everything just in case, since that trade-off runs the other way on every single write, forever.
Frequently Asked Questions
Advertisement
Was this article helpful?
Advertisement
Comments
No comments yet. Be the first to share your thoughts!
Advertisement
Related Posts

SQL Basics: The Queries Every Beginner Should Know
Ten queries that cover most of what you'll use day to day, with runnable examples
Ten SQL queries that cover most day-to-day database work, with runnable examples — SELECT, filtering, joins, aggregation, and the mistakes that trip up beginners.

Git and GitHub for Complete Beginners: A Step-by-Step Guide
From your first commit to your first pull request, without the jargon
A practical walkthrough of Git and GitHub — what problem version control solves, the five commands you'll use daily, and how to set up your first repository.
Docker for Complete Beginners: What It Solves and How to Get Started
From "works on my machine" to your first container, without the orchestration jargon
Docker packages an application with everything it needs so it runs identically anywhere. Here's what a container is, your first real commands, and where beginners get confused.
How HTTPS Works — What's Really Happening Behind the Padlock
The genuinely clever trick that lets two strangers agree on a secret while being watched
Every time you see the padlock icon, your device and a server just agreed on a shared secret in public, safely. Here's the actual mechanism — the key exchange and the certificate trust system — and what the padlock does and doesn't guarantee.
Advertisement