Lesson 4 of 9 · 28m
Indexing Like a Practitioner
Read query plans, choose composite key order, use partial and covering indexes, and recognize when an index is write amplification rather than help.
SQLite's query planner is good, but it cannot rescue a schema that refuses to state access patterns. Most slow SQLite applications are not suffering because SQLite is slow. They are scanning too much data because nobody wrote down the shape of the queries. Indexing is where that shape becomes explicit.
Start with the plan, not the index
Use EXPLAIN QUERY PLAN before and after:
EXPLAIN QUERY PLAN
SELECT id, created_at
FROM events
WHERE tenant_id = ? AND kind = ? AND created_at >= ?
ORDER BY created_at DESC
LIMIT 50;Phrases such as SCAN events mean SQLite expects to walk many rows. SEARCH events USING INDEX means it found a constrained path. USING COVERING INDEX means the index itself contains the needed columns and SQLite does not need to visit the table row for this query.
A temporary b-tree in a plan is not automatically bad, but on a hot path it often means the index does not support your ORDER BY shape.
Composite indexes have column order semantics
If queries filter tenant and then sort by time, a useful index might be:
CREATE INDEX events_tenant_kind_created
ON events(tenant_id, kind, created_at DESC);Equality columns generally come first, then range or ordering columns. An index on (created_at, tenant_id) serves a different access pattern. SQLite can sometimes use prefixes, but it cannot magically rearrange the index for every query.
Think of a composite index as a sorted phone book. If the book is sorted by last name then first name, it is excellent for last-name lookup and poor for finding everyone named Ada regardless of last name.
Partial indexes encode real selectivity
Partial indexes are one of SQLite's best practical features:
CREATE INDEX jobs_pending_created
ON jobs(created_at)
WHERE status = 'pending';If only a small fraction of jobs are pending, this index is small and highly relevant. The query must use a compatible predicate for SQLite to prove the partial index applies. WHERE status = 'pending' works; a semantically similar expression the planner cannot prove equivalent may not.
Expression indexes solve computed lookup problems
If queries search lower(email), index the expression rather than hoping the planner will transform a column index:
CREATE INDEX users_lower_email ON users(lower(email));The query must match the indexed expression closely enough for the planner to use it. This is a schema design decision, not a cosmetic hint.
Field notes
- Read
EXPLAIN QUERY PLANon realistic data, not an empty fixture table. - Composite column order is part of the design, not an implementation detail.
- Indexes speed reads and tax writes; measure both sides.
- Partial indexes are excellent for hot subsets such as pending work or soft-deleted rows.
ANALYZEcan improve plans, but statistics can become stale after major distribution changes.- An index that fixes one query may still hurt the system if the table is write-heavy.
Was this lesson helpful?