Skip to main content

ANALYZE Command

ANALYZE collects statistics that help SQLite choose better query plans.

It is especially useful after major data distribution changes.

Core Concepts

flowchart LR
A[Data distribution changes] --> B[Run ANALYZE]
B --> C[sqlite_stat tables updated]
C --> D[Planner makes better index choices]
CommandScope
ANALYZE;Analyze all relevant tables/indexes
ANALYZE table_name;Analyze one table
ANALYZE index_name;Analyze one index
ANALYZE;
Expected output
Statistics refreshed for planner use.

Code Examples

-- Refresh planner stats.
ANALYZE;
Expected output
Command completes with no result rows.
-- Inspect whether sqlite_stat1 has entries.
SELECT tbl, idx, stat
FROM sqlite_stat1
ORDER BY tbl, idx;
tblidxstat
table/index namesindex name or nullplanner stats text

SQLite-Specific Nuances

SQLite Nuance

SQLite stores analyze results in internal sqlite_stat* tables used by the query planner.

These stats are not business data, but they influence performance decisions.

Common Pitfalls / Best Practices

Pitfall

Adding indexes but never running ANALYZE after major data changes, then assuming planner choices will always stay optimal.

Best Practice

Run ANALYZE after bulk imports or large churn, and pair it with query-plan checks for critical queries.

Quick Challenge

Run a targeted analyze for table orders_idx and show one row from sqlite_stat1 for that table.

View Solution
ANALYZE orders_idx;

SELECT tbl, idx, stat
FROM sqlite_stat1
WHERE tbl = 'orders_idx'
LIMIT 1;
tblidxstat
orders_idxindex namestats text