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
Create an index
CREATE INDEX idx_notes_user_id_created_at ON notes (user_id, created_at DESC);

# Add a basic lookup index.

Expression index
CREATE INDEX idx_users_lower_email ON users (lower(email));

# Index a computed expression used in the query.

Partial index
CREATE INDEX idx_tasks_open_due_at ON tasks (due_at) WHERE completed_at IS NULL;

# Index only matching rows.

## Planner tools
Explain query plan
EXPLAIN QUERY PLAN
SELECT id, created_at
FROM notes
WHERE user_id = 42
ORDER BY created_at DESC
LIMIT 20;

# Inspect scans, searches, and covering-index use.

Analyze statistics
ANALYZE;

# Populate or refresh planner statistics.

Use the .expert shell helper
sqlite3 app.db ".expert"

# Ask the SQLite shell for index advice.

Recommended next

No recommendations yet.