Your app gets slower as your tables grow. One documented cause is a filtered query with no usable index: reading every row is the only plan the database has left. It is not the only cause of a slow query, and a later section shows how to check yours. But when a WHERE filter has no index it can use, what follows is documented behavior, not a guess.
MySQL's documentation puts it in one sentence: without an index, MySQL must begin with the first row and read through the entire table to find the relevant rows, and the larger the table, the more this costs. PostgreSQL's EXPLAIN guide shows the same behavior in a query plan: a WHERE clause attached to a sequential scan is a filter checked against every row the scan visits, so the scan still visits all of them.
Here is what an index changes, what it costs, and the three everyday mistakes that make a database ignore an index you already have.
What an index actually is
An index is a separate data structure the database maintains next to your table. For the common case it is a B-tree: MySQL stores most of its index types in B-trees, and PostgreSQL's default index type is a B-tree. The tree holds the indexed column's values in sorted order, plus pointers to the rows they belong to.
Sorted is the whole trick. MySQL's docs describe the payoff: with an index on the columns in question, MySQL can determine the position to seek to in the middle of the data file without looking at all the data. Instead of checking row after row, the database walks the tree straight to the value, then fetches only the matching rows. PostgreSQL's EXPLAIN guide describes the same two-step plan: one node visits the index to find the locations of matching rows, and the node above it fetches those rows from the table.
The lookup follows a shallow tree instead of the full row count.
What an index costs
Indexes are not free speed. PostgreSQL's indexes chapter warns that indexes add overhead to the database system as a whole and should be used sensibly. The overhead lands on writes: inserts and deletes touch every index on the table, an update that changes an indexed column rewrites that index's entries, and every index takes storage.
There is also a case where the scan wins. MySQL's docs note that indexes matter less for small tables, and for queries that read most of the rows anyway: when a query needs most of the table, reading it sequentially is faster than walking an index, because sequential reads minimize disk seeks. An index is a tool for finding a needle, not for reading the haystack.
An index also has to be chosen, not just present. PostgreSQL's planner selects the plan it estimates as cheapest, so an index that exists can still go unused. Checking the plan beats assuming it.
Three ways your query ignores the index you have
Creating the index is half the job. These three patterns quietly bypass it.
A function on the column. PostgreSQL's documentation on expression indexes gives the example directly: a case-insensitive lookup like WHERE lower(col1) = 'value' can use an index only if the index was built on the lower(col1) expression itself. A plain index on col1 does not match a filter wrapped in a function. Index the expression you actually filter on.
A leading wildcard. PostgreSQL's index-types page is precise: a B-tree index can serve LIKE only when the pattern is a constant anchored at the beginning of the string. col LIKE 'foo%' can use the index; col LIKE '%bar' cannot, because a sorted structure cannot help you find a value by its ending. There is a locale caveat in the same section worth reading before you rely on this.
The wrong end of a composite index. MySQL's leftmost-prefix rule: for a multiple-column index, the optimizer can use any leftmost prefix. An index on (col1, col2, col3) supports lookups on col1, on (col1, col2), and on all three. A lookup that filters only on col3 skips it. Column order in the index is a design decision, not a detail.
How to see it yourself
You do not have to guess which path a query takes. PostgreSQL's EXPLAIN output is a tree of plan nodes with cost estimates, and the scan nodes at the bottom tell you the access method: sequential scan, index scan, or bitmap index scan. A Seq Scan with a Filter line on a big table is the reading-every-row pattern, visible in plain text.
Run EXPLAIN on your slowest query before you add anything. If the plan already uses an index, the problem is somewhere else.
The takeaway
A filtered query without a usable index reads the whole table, and it gets slower exactly as your data grows. An index is a sorted side structure that trades write speed and storage for reads that jump straight to the matches. Add indexes for the filters your app actually runs, write the filters so the index can serve them, and confirm with EXPLAIN instead of guessing.
Source pages: PostgreSQL: Chapter 11, Indexes, PostgreSQL: Using EXPLAIN, PostgreSQL: Index Types, PostgreSQL: Indexes on Expressions, MySQL 8.4: How MySQL Uses Indexes.
I write about startups and AI at startupwiki.substack.com.
Cover photo: Erol Ahmed on Unsplash.

Top comments (0)