CREATE INDEX idx_posts_user_id ON posts(user_id);A simple index can dramatically reduce scan work on filters and joins.
CREATE INDEX patterns, covering and partial indexes, EXPLAIN QUERY PLAN, ANALYZE, and practical tuning tips for SQLite queries.
Speed up filters, joins, and sorts with the right index shape.
CREATE INDEX idx_posts_user_id ON posts(user_id);A simple index can dramatically reduce scan work on filters and joins.
CREATE INDEX idx_posts_user_created_at ON posts(user_id, created_at DESC);Composite indexes work best when they match common WHERE and ORDER BY patterns.
CREATE UNIQUE INDEX idx_users_email ON users(email);Useful both for integrity and for fast point lookups.
CREATE INDEX idx_sessions_active ON sessions(user_id)
WHERE revoked_at IS NULL;Partial indexes can be smaller and more selective than indexing the entire table.
Use planner output and statistics to tune real queries.
EXPLAIN QUERY PLAN
SELECT * FROM posts WHERE user_id = 42 ORDER BY created_at DESC LIMIT 10;High-signal first step when a query feels slower than expected.
Show plans before each entered statement.
.eqp onHandy during interactive tuning sessions in the sqlite3 shell.
Help the planner choose better indexes.
ANALYZE;Planner statistics can improve decisions for complex queries and multiple indexes.
REINDEX;Useful after collation changes or when rebuilding index structures.
PRAGMA optimize;A low-cost maintenance step you can run periodically in long-lived applications.