Help the planner make better choices
SQLite's query planner uses statistics stored in sqlite_stat1 to decide which index to use. Without stats, it makes reasonable defaults; with stats, it can pick dramatically better plans for skewed data.
Two commands you should know:
- ANALYZE — gather statistics for the entire database (or a single table). Run after a major data shape change, or as part of a deploy step.
- PRAGMA optimize — modern, lightweight version. SQLite tracks which tables changed enough to need re-analyzing and only re-runs those. Cheap to call on every connection close.
Tip: The recommended modern pattern: call
PRAGMA optimize on each connection close. SQLite figures out what (if anything) needs work; usually the call is near-free. This keeps stats fresh in long-running apps without a maintenance window.