A 100,000-card archive at the village office: SCAN or SEARCH through an index, from a real SQLite’s EXPLAIN QUERY PLAN, times and cards opened. An interactive…
Why it matters
"My query is slow" is almost always about an index, and the difference is orders of magnitude, not percent. Reading EXPLAIN and knowing when an index goes unused (a function on the column, a leading %, an index’s left column skipped) turns query tuning from guessing into deciding.
The key idea
An index is a sorted copy of a column, and it is used only when the query can search that order
Common mistakes
Indexed every column to be safe, and inserts got slower Every INSERT, UPDATE and DELETE must also update every index, and each index takes space. Index only the columns often in WHERE, JOIN or ORDER BY.
A query with WHERE lower(x) = ... does not use its index The index keeps x as it is, not lower(x), so SQLite goes back to a SCAN. Index the expression (CREATE INDEX ... ON t (lower(x)), MySQL 8.0.13+ too), or use a case-insensitive collation and write x = ....
LIKE '%foo' assumed to use the index, but it is slow A leading % means the start could be anything, so the index’s order does not help: a SCAN. 'foo%' has a fixed start and can be searched as a range. In SQLite LIKE ignores case, so that range is used only when the column is COLLATE NOCASE.
Try it in this playground
Turn a SCAN into a SEARCH with an index Make an index on the column the WHERE names: the cards opened drop from 100,000.
Wrap an indexed column in a function The plan goes back to SCAN though the index exists.
Make an index on an expression An index on lower(nama) lets WHERE lower(nama) = ... SEARCH again.
Get a COVERING INDEX Take only columns the index holds (SELECT nama): SQLite needs no archive card at all.