The missing index kit (find the full scans before your users do)
The short version
A database with no index does not know where a row lives. When someone logs in with their email, it reads row one, then row two, then row three, until it finds a match. At a thousand users that is a few milliseconds and you will never feel it. At a million users it is the same code, the same query, and a screen that will not load. Nothing in your app changed. You just grew.
An index is a lookup table the database keeps for one column, so instead of reading every row it jumps straight to the right one. The work below is finding which columns need one, adding them without locking your live table, and proving the scan is gone afterwards. It takes about twenty minutes and it is the cheapest performance fix there is.
Copy these prompts
Paste them into Claude Code, or any AI that can see your project, in this order. Run them against a real database with real row counts, because on an empty dev database every query looks fast.
1. List the columns you filter on
Read my codebase and list every database query that filters, joins, or sorts on a column: every WHERE, JOIN, ORDER BY and LIMIT, including the ones my ORM generates. For each one give me the table, the columns in the condition, roughly how often it runs (per request, per login, per background job), and whether an index on those columns already exists. Do not write any SQL changes yet. Just give me the list.
2. Prove which ones are actually scanning
For each query on that list, write me the EXPLAIN ANALYZE statement to run against my real database, and tell me exactly what to look for in the output to distinguish an index scan from a sequential scan. Then rank the queries by rows scanned times how often they run, so I fix the expensive one first rather than the slowest looking one.
3. Write the migration, safely
For the top offenders, write the CREATE INDEX statements. Rules: use the concurrent or online form so my live table is never locked; put the most selective column first in a composite index; tell me which existing indexes the new one makes redundant so I can drop them; and estimate the write cost, because every index slows down inserts and updates. Give me the rollback statement for each one too.
Here is the whole loop, with the parts that bite left in.
Why the app was fast for months
The cost of a scan grows with your table, not with your code. That is the trap: the query that runs in 3 milliseconds on your laptop is the same query, unchanged, that runs in 4 seconds in production.
no index on email, database reads every row until it matches
1,000 rows about 500 reads on average invisible
100,000 rows about 50,000 reads you notice
5,000,000 rows about 2,500,000 reads the page stops loading
That is a linear scan. When the database gives up on finding a shortcut it does a sequential scan of the table, and if the email does not exist at all, say a typo on the login screen, it reads every single row before it can tell the user their password was wrong.
An index turns that line into a handful of reads no matter how big the table gets.
Finding them: EXPLAIN is the whole skill
You do not guess at this. You ask the database what it plans to do.
EXPLAIN ANALYZE SELECT * FROM users WHERE email = 'someone@example.com';
Two words in the output decide it:
Seq Scan on users (rows=2481903) <- bad. it is reading the whole table
Index Scan using users_email_idx <- good. it jumped straight there
Run it on production sized data. A sequential scan on 200 rows is often the correct plan, because for a tiny table reading everything is genuinely faster than a lookup, so a dev database will happily hide the problem you are hunting.
The four places a missing index hides
The login column is the obvious one. These are the ones people miss:
- Foreign keys. Most databases index the primary key for you and do not index the column pointing at it. Every join on that column is a scan, and deleting a parent row scans the child table to check.
- ORDER BY with LIMIT. A feed sorted by created date pulls the whole table into memory and sorts it, just to hand you twenty rows.
- The second half of a composite index. An index on (account, created) helps a query filtered by account. It does nothing for a query filtered only by created. Column order is not cosmetic.
- Anything a background job touches. Nobody watches a cron job's latency until it starts overlapping its own next run.
Adding it without taking the site down
The naive form takes a write lock and holds it while it builds. On a large table that is your app being down for the length of the build.
-- locks the table for the whole build. fine on 500 rows, an outage on 5 million
CREATE INDEX users_email_idx ON users (email);
-- builds alongside live traffic instead
CREATE INDEX CONCURRENTLY users_email_idx ON users (email);
The concurrent form is slower and cannot run inside a transaction, which matters because many migration tools wrap everything in one by default. It can also leave an invalid index behind if it fails, so check for that and drop it before retrying. Every database has its own version of this idea, so ask for yours by name.
Indexes are not free
This is the part the AI will skip unless you ask. Every index you add is another structure the database has to update on every insert, update and delete on that table, and another chunk of disk and memory. Six indexes on a hot write table is its own performance bug.
The rule: index the columns you actually filter on, in the order you filter on them, then delete the ones nothing uses. Most databases can tell you how many times each index has been read since the counters were reset. Zero reads on a busy table means it is pure cost.
Prove it afterwards
An index you did not verify is a guess with a migration attached.
1. EXPLAIN ANALYZE before, saved. note the scan type and the row count.
2. add the index.
3. run the same statement. it must now say Index Scan.
4. compare actual time, not estimated cost.
If the plan still says sequential scan, the database decided it was the better route and it is usually right: your filter matches most of the table, or the column is wrapped in a function, or the types do not match on either side of the comparison. Any of those three will silently disqualify a perfectly good index.
Steal this for your app
- Every column you log in with, look up by, or join on needs an index. Start with the login column, then walk the foreign keys.
- Test on production sized data. The bug is defined by row count, so an empty dev database cannot show it to you.
- Ask the database, do not guess. EXPLAIN ANALYZE is one line and it ends every argument about which query is slow.
- Build concurrently. A migration that locks the table is a worse outage than the slow page you were fixing.
- Fix the expensive query, not the slow one. Rows scanned times calls per minute. A 200ms query running constantly costs you more than a 2 second report someone opens twice a day.
- Do this before you need it. The fix is the same at 1,000 users and at a million. Only the size of the incident changes.
Run this on your codebase
Paste this into a Claude session in your repo:
Audit this codebase for missing database indexes. Report before changing anything:
1. Every query that filters, joins or sorts on a column, including ORM generated ones, with the table and columns involved.
2. Which of those columns have no index today. Call out unindexed foreign keys and ORDER BY plus LIMIT separately.
3. The EXPLAIN ANALYZE statement to run for each one, and what output proves it is a sequential scan.
4. A ranked list by rows scanned times call frequency, not by query duration.
5. CREATE INDEX statements for the top offenders, using the concurrent form, with column order justified, the write cost estimated, and a rollback for each.