CREATE INDEX idx_notes_user_id_created_at ON notes (user_id, created_at DESC);A compact way to speed up per-user timelines or history screens.
SQLite CREATE INDEX patterns, expression and partial indexes, EXPLAIN QUERY PLAN, and lightweight performance tuning.
Practical CREATE INDEX examples for apps and embedded databases.
CREATE INDEX idx_notes_user_id_created_at ON notes (user_id, created_at DESC);A compact way to speed up per-user timelines or history screens.
CREATE INDEX idx_users_lower_email ON users (lower(email));Helpful when the query uses the same expression in predicates.
CREATE INDEX idx_tasks_open_due_at ON tasks (due_at) WHERE completed_at IS NULL;A smaller index can be more efficient when only open tasks matter to the hot path.
Validate the actual plan chosen by SQLite.
EXPLAIN QUERY PLAN
SELECT id, created_at
FROM notes
WHERE user_id = 42
ORDER BY created_at DESC
LIMIT 20;SQLite reports whether it scans a table, searches with an index, or can use a covering index optimization.
ANALYZE;Statistics can improve plan selection in nontrivial schemas.
sqlite3 app.db ".expert"The SQLite CLI includes tools that can help surface candidate indexes for sample queries.