Use it when plans are poor after data changes.
Table Statistics and ANALYZE
Table statistics describe data distribution (row counts, distinct values, histograms) that the planner uses to estimate rows; ANALYZE (or UPDATE STATISTICS) refreshes them.
1. Last analyze: SELECT relname,last_autoanalyze FROM pg_stat_user_tables; / ANALYZE TABLE t; / sys.dm_db_stats_properties.
2. Run ANALYZE after bulk changes.
3. Compare plan before/after.
4. Raise statistics target for skewed columns.
5. Monitor.
data changes → statistics refreshed → better estimates → better plans
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 How to verify Verify boundary
Confirm last analyze time is after the last big data change. Check estimates on the problem query improve after ANALYZE.
Step 2 Common failures Narrow the fault
Bulk loads followed by immediate queries run with old statistics. Correlated columns cause large under-estimates. Disabling autovacuum also stops automatic statistics.
Autovacuum/autoanalyze or auto-update usually handle it, but bulk loads need a manual ANALYZE. Skewed columns may need higher statistics targets.
SELECT relname,last_autoanalyze FROM pg_stat_user_tables;— first check for this topicpsql -c "SELECT now(), version();"— PostgreSQL reachable and versionmysql -e "SELECT NOW(), VERSION();"— MariaDB/MySQL reachable and versionsqlcmd -S <server> -Q "SELECT @@VERSION"— SQL Server reachable and version
ReferenceWhat it is1 section · depth on demand
How it works
Statistics include row counts, null fraction, distinct values and common values with their frequencies. The planner multiplies selectivities of predicates, which goes wrong when columns are correlated; extended statistics can describe such dependencies.
ReferenceImportant terms & boundaries3 sections · depth on demand
Updating statistics
| Database | Command | Automatic |
|---|---|---|
| PostgreSQL | ANALYZE orders; | autovacuum analyze |
| MySQL/MariaDB | ANALYZE TABLE orders; | innodb_stats_auto_recalc |
| SQL Server | UPDATE STATISTICS dbo.orders; | auto update stats |
Run manually after bulk loads or large deletes, when plans suddenly change, or when estimated and actual rows differ greatly.
In depth
What the planner knows about a column
SELECT attname, null_frac, n_distinct, most_common_vals, most_common_freqs, correlation
FROM pg_stats WHERE tablename = 'orders' AND attname = 'status';
From these it estimates selectivity: if 'open' has frequency 0.02 in most_common_freqs, WHERE status = 'open' on 5 million rows is estimated at 100,000 rows.
When estimates go wrong
| Cause | Example | Fix |
|---|---|---|
| Stale statistics | Bulk load, then immediate queries | ANALYZE orders; after the load |
| Too coarse | Skewed column with thousands of distinct values | ALTER TABLE orders ALTER COLUMN customer_id SET STATISTICS 1000; ANALYZE orders; |
| Correlated columns | city = 'Aarhus' AND postcode = '8000' treated as independent | CREATE STATISTICS orders_geo (dependencies) ON city, postcode FROM orders; |
| Expressions | WHERE lower(email) = … | Expression index (statistics are gathered for it) |
When autoanalyze runs
Autovacuum analyses a table after roughly autovacuum_analyze_threshold + autovacuum_analyze_scale_factor × rows changed rows (defaults 50 + 10 %). On a 100-million-row table that is 10 million changes, so the newest dates are often missing from the histogram — a common cause of bad plans for "today's data". Lower the scale factor per table:
ALTER TABLE events SET (autovacuum_analyze_scale_factor = 0.01);
SELECT relname, last_analyze, last_autoanalyze, n_mod_since_analyze FROM pg_stat_user_tables;
MySQL InnoDB keeps persistent statistics (innodb_stats_persistent) and recalculates after about 10 % of rows change; ANALYZE TABLE forces it, and histograms (ANALYZE TABLE … UPDATE HISTOGRAM ON col) help on non-indexed columns.
Common mistakes
- Stale statistics after bulk loads.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Terms & aliases
Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.