The symptom
A query that was instant on small data takes seconds once the table grew. SQLite has no server, no statistics daemon and no plan cache to tune - performance here is almost entirely about the query plan, and there is exactly one command you need to see it.
Diagnose it
Read the plan: SCAN is the smell, SEARCH is the goal
EXPLAIN QUERY PLAN
SELECT id, total FROM orders
WHERE customer_id = 42 AND status = 'pending';
Output like SCAN orders means SQLite reads every row of the table. SEARCH orders USING INDEX idx_orders_customer_status (customer_id=? AND status=?) means it seeks, and the part in brackets shows which index columns the seek used. Most of this page is about turning the first into the second. EXPLAIN QUERY PLAN does not run the query, so it has no actual row counts. For those, the sqlite3 shell has .timer on for wall-clock time and, in builds with scan-status support (the standard shell from recent releases has it), .scanstats on, which prints loops and rows per plan step after each query. .eqp on shows the plan above every query automatically.
Build the index the query asks for
SQLite uses a multi-column index from the left. The example above wants equality columns in any order (both are equalities), with range columns after them:
CREATE INDEX idx_orders_customer_status
ON orders (customer_id, status);
When the SELECT lists only indexed columns, SQLite skips the table entirely - a covering index shows up in the plan as USING COVERING INDEX. Adding total to the index above would make the query index-only.
Give the planner real statistics
ANALYZE;
SELECT * FROM sqlite_stat1;
ANALYZE records, per index, the row count and the average number of rows per distinct key prefix in sqlite_stat1 (a row like 20000 20 1). The planner uses those numbers to pick between two candidate indexes, or between an index and a scan. Without them it assumes every index is fairly selective. For an app that ships a database file, PRAGMA optimize; before closing a connection runs ANALYZE only where the statistics look out of date, and is the approach the SQLite docs recommend.
Watch for the quiet killers
A LIKE '%abc' filter cannot use any B-tree index. Less obviously, LIKE 'abc%' cannot use a plain index either, because SQLite's LIKE is case-insensitive by default: it needs an index declared COLLATE NOCASE on that column, or use GLOB 'abc*' (case-sensitive), which can seek a normal index. A function around a column (WHERE lower(email) = ...) blocks the index unless you build an index on the same expression (CREATE INDEX ... ON orders(lower(email))), and an ORDER BY that no index satisfies forces a full sort per run. Each shows up as a SCAN or a USE TEMP B-TREE line in the plan. To inspect the SQL that produced the plan - pasted in from a log or an ORM - the SQL-Formatter lays it out clause by clause; the plan tree itself also reads well in the query plan viewer.
Fix it
Add the index whose prefix matches the predicate, equality columns first, range column last, and re-run EXPLAIN QUERY PLAN until the SCAN became a SEARCH. That one loop fixes most slow SQLite queries.
Prefer covering indexes on hot paths. Mobile apps and embedded devices pay real I/O costs for heap pages, so keeping the query entirely inside the index is worth the extra bytes:
CREATE INDEX idx_orders_cover
ON orders (customer_id, status, created_at, total);
Run ANALYZE after big data changes - imports, migrations, bulk deletes - or PRAGMA optimize on a schedule, so the planner is not choosing from stale numbers.
Keep writes from starving reads. SQLite allows one writer at a time. Outside a transaction every INSERT is its own transaction with its own sync to disk, so wrap bulk inserts in a transaction per few thousand rows - SQLite can do tens of thousands of inserts a second but only dozens of separate transactions. In the default rollback-journal mode a writer also blocks readers while it commits; PRAGMA journal_mode=WAL lets readers carry on during writes, and the setting is stored in the database file. Set PRAGMA busy_timeout so a connection waits for the lock instead of failing at once with SQLITE_BUSY. If a query blocks rather than runs slow, the blocking guide's short-transactions advice applies to SQLite too.
The checklist
Work through it in order. Each step gives you the evidence for the next one.
- Run EXPLAIN QUERY PLAN on the slow query; note every SCAN line.
- Check each SCAN's WHERE predicate for a matching index prefix.
- Create the index with equality columns first and the range column last.
- Re-run the plan and confirm SCAN became SEARCH (USING INDEX ...).
- If the SELECT touches few columns, extend the index to cover the query.
- Run ANALYZE after bulk loads (or PRAGMA optimize periodically) so sqlite_stat1 reflects the data.
- Search the plan for USE TEMP B-TREE: an ORDER BY or GROUP BY no index satisfies.
- Enable WAL mode and set busy_timeout if readers and writers queue behind each other.
Questions that come up
Does SQLite really need ANALYZE?
Not always. Without sqlite_stat1 the planner assumes every index narrows the search to a handful of rows, which is fine when the schema has one obvious index per query. It matters when a query could use two different indexes, or when an indexed column has few distinct values (a status flag), because only the statistics tell the planner which choice reads fewer rows. Run ANALYZE after the data reaches a realistic size, or call PRAGMA optimize when connections close.
Why is LIKE '%text%' so slow?
A B-tree index is ordered left to right, so a pattern starting with a wildcard can only be scanned, not sought. A prefix match (LIKE 'abc%') can seek, but only with an index declared COLLATE NOCASE or with GLOB 'abc*' against a normal index. For words anywhere in the text, use an FTS5 full-text table; for suffix search, store the reversed string and index that.
Can I index a function like lower(email)?
Yes. SQLite supports indexes on expressions: CREATE INDEX idx ON users(lower(email)) makes WHERE lower(email) = ? a SEARCH. The expression in the index must match the query exactly.
Is PRAGMA cache_size worth tuning?
Occasionally. The default page cache is small (-2000, meaning about 2 MB, in standard builds; run PRAGMA cache_size to see yours), and it is per connection, so an app whose working set is larger pays repeated reads through the OS. Set it per connection with a negative number of KiB, for example PRAGMA cache_size = -65536 for 64 MB. Raising it to tens of megabytes is cheap on desktop hardware; on memory-tight mobile targets, measure before raising.
How do I inspect the plan outside the app?
EXPLAIN QUERY PLAN is plain SQL and works from any client, including the sqlly CLI and the desktop app's query editor. You can also paste the plan text into SQLly's free query plan viewer at /tools/query-plan-viewer.