A student sent me a query that took four seconds on 900,000 rows. She had added an index, she said, and it made no difference. I asked to see the query. She was filtering on LOWER(email) and had indexed email. The database could not use her index because she had wrapped the column in a function.
That is the whole subject in one story. An index is only useful if the database can match what you wrote to what it stored.
What the database does without an index
Imagine a table of one million customers. You ask for the row where email is ada@example.com. With no index, the database reads row one, checks the email, moves to row two, checks, and continues. One million reads to find one row. This is called a full table scan, and it is exactly as slow as it sounds.
At 10,000 rows you will not notice. The scan finishes in milliseconds and you conclude the database is fast. At one million rows, the same query takes seconds and you conclude the database is bad. Neither conclusion is right. Nothing changed except the amount of work.
What an index is
An index is a second structure, kept in sorted order, mapping the column value to the row. Like the index at the back of a textbook. You do not read the whole book to find "photosynthesis". You go to the index, find the page number, and turn to it.
In SQL it looks like this:
CREATE INDEX idx_customers_email ON customers (email);
Now the same lookup does a few jumps instead of a million reads. On a properly indexed column, finding one row in a million takes single digit milliseconds. That is not tuning. That is using the tool as designed.
Where indexes stop working
An index helps when the query can use the sorted order. That breaks in a few common ways.
- Wrapping the column in a function, like
LOWER(email)orDATE(created_at). Index the expression instead, or store the value already normalised. - Leading wildcard searches, like
LIKE '%ada%'. The database cannot jump because it does not know where to start. - Comparing different types. If
phoneis text and you search with a number, some engines quietly convert the column and skip the index. - Low variety columns. An index on a true or false column usually loses to a scan, because half the table matches.
None of these are exotic. The function one catches more people than any other, because the query looks perfectly reasonable.
The cost you pay on writes
Indexes are not free. Every insert, update and delete has to update every index on that table. A table with eight indexes writes nine times. On a read heavy app nobody notices. On an app taking 200 orders a minute, extra indexes can turn a fast insert into a queue.
So the rule is not "index everything". It is "index what your queries filter, sort or join on, and delete the ones nothing uses". A table with a phone number column that only appears in reports does not need an index for the checkout page.
If a table is mostly written and rarely read, like an audit log, fewer indexes is usually right.
For a long time I added indexes on every column that ended with _id, assuming foreign keys needed them everywhere. Most did. But on a small lookup table with 40 rows, the index was pure overhead and the planner ignored it anyway. I had added 12 indexes that did nothing except slow down imports.
The habit I built after that: after a month in production, list the indexes on the busiest tables and check which ones the statistics say are never used. Postgres shows this in pg_stat_user_indexes. Drop the dead ones.
How to check before and after
Put EXPLAIN in front of the query. In MySQL use EXPLAIN ANALYZE to see real timings. In Postgres, EXPLAIN ANALYZE gives you the plan and the actual rows examined.
Before the index you will see a sequential scan with a row count near the table size. After, you want an index scan with a small number. If the plan still shows a sequential scan after you create the index, the query is not shaped in a way the index can serve. Do not add more indexes. Fix the query.
Compare the time too, not just the plan. Write the numbers down. "Was 3.8s, now 40ms" is evidence. "Feels faster" is not.
Test it on your own table
Pick your largest table. Run one query you use often and time it. Run EXPLAIN and note the rows examined. Add an index on the column in the filter. Run both again and write down the difference.
That single exercise will teach you more than reading about B-trees for an hour. If your query is still slow after indexing, the cause may be elsewhere, so read why your database gets slow. And if the slowness only appears on the live server, see why code works locally but not on the server. The software development course has a section on reading query plans without a computer science degree.