Your app feels fast in development. A few hundred rows, instant responses, no complaints. Then real traffic arrives, the tables grow past a million rows, and a page that used to load in 200 milliseconds now takes eight seconds. The code did not change. The data did, and the indexes did not keep up. Most of the time, the cause is not a bad database or a weak server. It is a handful of database indexing mistakes that turn ordinary queries into full table scans.
Indexes are the cheapest performance win in most applications. They are also easy to get wrong, because a wrong index costs you twice: the query still runs slowly, and every write pays to maintain an index nobody uses. Here are the seven mistakes I see most often, how to spot each one, and what to do instead.
1. Indexing Every Column Just in Case
When a query is slow, the tempting fix is to add an index on every column the query touches. It feels safe. It is not. Each index is a separate data structure the database has to update on every INSERT, UPDATE, and DELETE. A table with twelve indexes writes noticeably slower than the same table with three, and the extra indexes rarely help reads at all.
A better rule: index the columns your WHERE clauses, JOINs, and ORDER BYs actually filter on, and only after you have seen the query run slowly. If you are not sure which columns those are, read the slow query log first. Debugging a slow database query without guessing starts with the same habit: measure the query, then decide what to index.
2. Forgetting Indexes on Foreign Keys and Join Columns
This is the most common of all database indexing mistakes, and the easiest to miss. You add a foreign key like user_id to an orders table, and the application joins orders to users on every dashboard load. Many databases do not create an index on a foreign key automatically. MySQL does, through the constraint, but PostgreSQL does not. Without that index, every join scans the whole orders table.
The fix takes one line: an index on the foreign key column. Check every column that appears in a JOIN condition or a WHERE clause that filters by a related record. If your app lists a user’s orders, comments, or invoices, those foreign key columns need indexes.
3. Getting the Column Order Wrong in Composite Indexes
A composite index on (status, created_at) is not the same as one on (created_at, status). Databases use a composite index from the left. A query that filters on status can use the first index; a query that filters only on created_at cannot, because created_at is not the leftmost column.
Put the most selective column first, the one that narrows the result set the most. If your queries usually filter by status = 'paid' and then sort by created_at, the index (status, created_at) serves both the filter and the sort. If some queries filter only by date, they need their own index. Writing faster SQL queries covers the same leftmost-prefix rule with examples you can run against your own schema.
4. Wrapping Indexed Columns in Functions
You have an index on created_at. Then someone writes WHERE YEAR(created_at) = 2026, or WHERE LOWER(email) = '[email protected]'. The database cannot use the plain index anymore, because the WHERE clause does not compare the raw column value. It has to compute the function for every row first, which means a full scan.
Rewrite the query so the column stands alone. Instead of YEAR(created_at) = 2026, use a range: created_at >= '2026-01-01' AND created_at < '2027-01-01'. For case-insensitive email lookups, store a normalized lowercase copy, or use a functional index such as LOWER(email) if your database supports it. The point is the same either way: if a function sits between the column and the comparison, the index cannot do its job.
5. Adding Indexes and Never Checking Whether They Are Used
Indexes pile up over the life of a project. A developer adds one to fix a slow report, the report gets rewritten six months later, and the index stays. Meanwhile every write still pays for it. On a busy table, unused indexes are pure overhead.
Check usage every few months. In PostgreSQL, pg_stat_user_indexes shows how many times each index has been scanned; an index with zero scans over a long period is a candidate for removal. MySQL offers the same view through sys.schema_unused_indexes. Drop the unused ones on staging first, watch for a day, then drop them in production. Good indexing best practices include pruning, not just adding.
6. Ignoring the Cost Indexes Add to Writes
Every index is a trade. Reads get faster, writes get slower. On a read-heavy table such as product listings, that trade is usually worth it. On a write-heavy table such as event logs, five indexes can hurt more than they help, because almost nobody queries the table by those columns anyway.
Before adding an index, ask how the table is actually used. If the table takes ten thousand inserts an hour and gets queried by one nightly report, one or two indexes are plenty. If a table is read a hundred times for each write, index generously. The ratio of reads to writes on that specific table is what decides it.
7. Trusting the Index Instead of Reading EXPLAIN
An index existing does not mean the planner uses it. Small tables often get scanned in full because reading the whole table is genuinely faster than seeking through an index. A query with a low-selectivity filter, such as status where 90 percent of rows match, may also skip the index. And stale table statistics can push the planner toward a bad plan even when a good index exists.
Run EXPLAIN on the query and read the output. Look for sequential scans on large tables, and check whether the plan mentions the index you expected. If it does not, find out why before adding another index. Sometimes the answer is to run ANALYZE so the planner sees fresh statistics. Sometimes the query itself needs rewriting.
How to Find Database Indexing Mistakes Before Users Do
You do not need expensive tooling to catch these problems. A short routine, run monthly, covers most of it:
- Turn on the slow query log and review the top ten queries by total time, not just by single-query time. A query that takes 50 milliseconds but runs a million times a day matters more than one slow report.
- Run EXPLAIN on each of those ten queries and note every sequential scan on a table with more than a few thousand rows.
- List unused indexes and check whether any of them can be dropped.
- Check that every foreign key column and every JOIN column has an index.
- Re-run the same queries after each change and record the timings, so you know whether the fix helped or just moved the cost somewhere else.
That routine is the core of database query optimization for small teams. It takes an hour, it needs no new tools, and it catches the mistakes that make slow database queries a permanent background problem instead of a one-off incident. If your database runs on a small VPS, the same checks matter even more, because you cannot buy your way out with a bigger instance. Tuning MySQL vs PostgreSQL on a VPS with limited RAM explains how memory limits change which index choices pay off.
Fixing Indexes Without Breaking Production
Index changes touch live tables, so treat them like any other production change. Test on a copy of production data, not on an empty development database, because an index that looks perfect on a hundred rows can behave differently on ten million. Measure the slow query before and after, and keep the numbers.
On large tables, build the index in a way that does not lock writes. PostgreSQL offers CREATE INDEX CONCURRENTLY, which takes longer but lets the table keep serving traffic. MySQL’s online DDL options do the same job in most cases. Run the build outside peak hours, and have a rollback plan, which for an index is simply dropping it again. How SQL joins work, visually is worth a read before you touch join indexes, because a misunderstood join is often the real reason the query is slow, and no index fixes a join that returns the wrong rows.