SQLite Indexes and Query Planner

SQLite CREATE INDEX patterns, expression and partial indexes, EXPLAIN QUERY PLAN, and lightweight performance tuning.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

SQLite index creation

Practical CREATE INDEX examples for apps and embedded databases.

Create an index

Add a basic lookup index.

sqlANYsqlitecreate-index
sql
CREATE INDEX idx_notes_user_id_created_at ON notes (user_id, created_at DESC);
Notes

A compact way to speed up per-user timelines or history screens.

Expression index

Index a computed expression used in the query.

sqlANYsqliteexpression-index
sql
CREATE INDEX idx_users_lower_email ON users (lower(email));
Notes

Helpful when the query uses the same expression in predicates.

Partial index

Index only matching rows.

sqlANYsqlitepartial-index
sql
CREATE INDEX idx_tasks_open_due_at ON tasks (due_at) WHERE completed_at IS NULL;
Notes

A smaller index can be more efficient when only open tasks matter to the hot path.

Planner tools

Validate the actual plan chosen by SQLite.

Explain query plan

Inspect scans, searches, and covering-index use.

sqlANYsqliteexplain-query-plan
sql
EXPLAIN QUERY PLAN
SELECT id, created_at
FROM notes
WHERE user_id = 42
ORDER BY created_at DESC
LIMIT 20;
Notes

SQLite reports whether it scans a table, searches with an index, or can use a covering index optimization.

Analyze statistics

Populate or refresh planner statistics.

sqlANYsqliteanalyzestatistics
sql
ANALYZE;
Notes

Statistics can improve plan selection in nontrivial schemas.

Use the .expert shell helper

Ask the SQLite shell for index advice.

bashANYsqlitecliexpert
bash
sqlite3 app.db ".expert"
Notes

The SQLite CLI includes tools that can help surface candidate indexes for sample queries.

Recommended next

No recommendations yet.