Engineering 5 min read Updated
When a database index helps and when it only adds cost
In short
An index helps when a frequent query can use it to find, filter or sort rows, and every write pays for it, so add one only for a query you actually run.
A query that runs quickly on test data can slow down in production. An index lets the database find rows without reading the whole table, but every insert, update and delete then does more work. Find the slow query first, and only then add the index that serves it. Indexes are one part of speed work; the wider picture is in speed as part of design.
Without an index the database reads the whole table
Without a suitable index, the database has to read the table to find matching rows. The MySQL manual describes this as reading “the entire table”, and it notes that “the larger the table, the more this costs”. An index keeps the values of one or more columns in sorted order, so the database can jump to the rows it needs. SQLite explains a lookup through an index as a short series of binary searches rather than a walk through every row.
An index can also cover a query. If it holds every column that the query reads, the database answers from the index alone. SQLite says that in this case it “will never look up the original table row”.
Column order decides whether an index is used
A composite index sorts its rows by the first column and uses the second column to break ties. Both databases can use any leftmost part of it. An index on fruit and state can therefore serve a filter on fruit alone, or a filter on both columns together. MySQL states that any leftmost prefix of the index can be used to look up rows.
A filter on the second column alone usually cannot use the index. In SQLite, the lookup columns must start from the first column, using =, IN or IS, and only the rightmost column used may take an inequality. SQLite can skip the first column when it knows that column holds many duplicate values, but that knowledge comes only from running ANALYZE first.
| Filter on an index over fruit and state | Index used for lookup? | Reason |
|---|---|---|
| fruit = ? | Yes | The leading column is used with equality |
| fruit = ? AND state = ? | Yes | Both columns form the leftmost part |
| state = ? | Usually not | The leading column is missing |
SQLite also warns against keeping two indexes where one is a prefix of the other. Drop the one with fewer columns, because the longer index still serves the shorter lookups.
Working on something similar in your own product? A few honest sentences are enough – we read every enquiry and reply by email. Apply with your idea
Every index slows down writes
Every index has to be updated whenever the table changes. The MySQL manual on insert optimisation counts one unit of work per index for each inserted row, and it says that the cost of maintaining B-tree indexes grows with the size of the table. An index that no query uses therefore costs time on every write and returns nothing.
For a one-off bulk load, the same manual suggests deferring index updates until the end, where the storage engine allows it. That is a technique for imports. It is not a reason to leave indexes out of a production table that queries depend on.
When an index does not help
- Small tables, where a full scan costs little. The MySQL manual says indexes are less important for queries on small tables.
- Reports that read most or all rows. The manual notes that reading sequentially can be faster than working through an index.
- Comparisons across types. Comparing a string column with a number, for example, may prevent the use of any index on that column.
- Filters that match much of the table. SQLite estimates the work of each option and picks the cheapest, so a full scan can win.
Check before you keep an index
Ask the database for its query plan before and after the change. SQLite compares the estimated work of each option, and its documentation recommends running PRAGMA optimize so that the planner has up-to-date statistics. Statistics go stale as data changes, so run the analysis again after large imports or deletions.
Then use this short checklist:
- List the queries that run most often and the ones that users notice as slow.
- For each query, note its equality filters, its sort order and whether it reads only a few columns.
- Add the smallest index that serves the query, with equality columns first.
- Remove any index that is a prefix of another index.
- Compare the query plan and the timing of the write path before and after the change.
If you are still choosing the engine, read SQLite or MySQL for a small product first. Whatever the index, keep user input out of the query text; preventing SQL injection explains how.
The rules about index use come from the SQLite and MySQL manuals. The checklist is general practice, and the effect of any index depends on your data and your engine.
Common questions
- Does every query get faster with an index?
- No. An index helps only when a query can use it to find or sort rows. An index that no query uses still makes every insert and update do more work.
- Why does the order of columns matter?
- A composite index is sorted by its first column, so queries benefit fully only when they use its leftmost columns. Put the columns you filter with equality first.
- How do I know that the database really uses an index?
- Ask the database for its query plan before and after the change. The plan shows how the query runs, and it is the evidence for keeping or removing the index.
Sources
- The SQLite Query Planner, SQLite.org. Accessed 24 November 2026
- The SQLite Query Optimizer Overview, SQLite.org. Accessed 24 November 2026
- How MySQL Uses Indexes, MySQL 8.4 Reference Manual (Oracle). Accessed 14 November 2026
- Optimizing INSERT Statements, MySQL 8.4 Reference Manual (Oracle). Accessed 14 November 2026
Further reading
-
SQLite or MySQL for a small product
Choose SQLite when one application on one host reads and writes the data and writers can take turns; choose MySQL when many machines or simultaneous writers share it.
-
Plain PHP or a framework? An honest comparison
Neither option is the safe default; the right choice depends on the size of the project, the team, the expected lifetime and the work you will own.
-
In-app search: SQL, full-text or search engine?
Product and admin search does not start with a separate search cluster. Match the search backend to catalogue size, typo tolerance, faceting and sync complexity.
Have an idea that should exist? Or a working product that needs more customers, a new website or a complete rework? Bring us anything. Apply with your idea