SQLite Indexes and Query Planner Cheatsheet

CREATE INDEX patterns, covering and partial indexes, EXPLAIN QUERY PLAN, ANALYZE, and practical tuning tips for SQLite queries.

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

Create and use indexes

Speed up filters, joins, and sorts with the right index shape.

Create a basic index

Index a frequently filtered column.

sqlANYsqliteindexperformance
sql
CREATE INDEX idx_posts_user_id ON posts(user_id);
Notes

A simple index can dramatically reduce scan work on filters and joins.

Create a composite index

Index multiple columns in query order.

sqlANYsqlitecomposite indexorder by
sql
CREATE INDEX idx_posts_user_created_at ON posts(user_id, created_at DESC);
Notes

Composite indexes work best when they match common WHERE and ORDER BY patterns.

Create a unique index

Enforce uniqueness and speed up lookups.

sqlANYsqliteunique index
sql
CREATE UNIQUE INDEX idx_users_email ON users(email);
Notes

Useful both for integrity and for fast point lookups.

Create a partial index

Index only rows that match a predicate.

sqlANYsqlitepartial indexwhere
sql
CREATE INDEX idx_sessions_active ON sessions(user_id)
WHERE revoked_at IS NULL;
Notes

Partial indexes can be smaller and more selective than indexing the entire table.

Inspect query plans

Use planner output and statistics to tune real queries.

Explain a query plan

See whether SQLite scans or uses an index.

sqlANYsqliteeqpplanner
sql
EXPLAIN QUERY PLAN
SELECT * FROM posts WHERE user_id = 42 ORDER BY created_at DESC LIMIT 10;
Notes

High-signal first step when a query feels slower than expected.

Enable automatic EXPLAIN QUERY PLAN in shell

Show plans before each entered statement.

textANYsqlite3eqpshell
text
.eqp on
Notes

Handy during interactive tuning sessions in the sqlite3 shell.

Collect statistics with ANALYZE

Help the planner choose better indexes.

sqlANYsqliteanalyzestatistics
sql
ANALYZE;
Notes

Planner statistics can improve decisions for complex queries and multiple indexes.

Rebuild indexes

Recreate indexes when needed.

sqlANYsqlitereindex
sql
REINDEX;
Notes

Useful after collation changes or when rebuilding index structures.

Run PRAGMA optimize

Let SQLite perform lightweight maintenance.

sqlANYsqlitepragmaoptimize
sql
PRAGMA optimize;
Notes

A low-cost maintenance step you can run periodically in long-lived applications.

Recommended next

No recommendations yet.