Pcornel
Browse topics
Options
Recent
Favorites
Databases · Technical reference

Table Statistics and ANALYZE

Direct answer

Table statistics describe data distribution (row counts, distinct values, histograms) that the planner uses to estimate rows; ANALYZE (or UPDATE STATISTICS) refreshes them.

When to use this

Use it when plans are poor after data changes.

First 30 seconds

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.

Operational verification

data changes → statistics refreshed → better estimates → better plans

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
MariaDB / MySQL / PostgreSQL / Microsoft SQL Server concepts
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformMariaDB / MySQL / PostgreSQL / Microsoft SQL Server concepts
Scope / prerequisiteUse it when plans are poor after data changes.
What next?2 suggested next steps
Evidence0
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

2 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

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.

Key points

Autovacuum/autoanalyze or auto-update usually handle it, but bulk loads need a manual ANALYZE. Skewed columns may need higher statistics targets.

Key checks
  • SELECT relname,last_autoanalyze FROM pg_stat_user_tables; — first check for this topic
  • psql -c "SELECT now(), version();" — PostgreSQL reachable and version
  • mysql -e "SELECT NOW(), VERSION();" — MariaDB/MySQL reachable and version
  • sqlcmd -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

DatabaseCommandAutomatic
PostgreSQLANALYZE orders;autovacuum analyze
MySQL/MariaDBANALYZE TABLE orders;innodb_stats_auto_recalc
SQL ServerUPDATE 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

CauseExampleFix
Stale statisticsBulk load, then immediate queriesANALYZE orders; after the load
Too coarseSkewed column with thousands of distinct valuesALTER TABLE orders ALTER COLUMN customer_id SET STATISTICS 1000; ANALYZE orders;
Correlated columnscity = 'Aarhus' AND postcode = '8000' treated as independentCREATE STATISTICS orders_geo (dependencies) ON city, postcode FROM orders;
ExpressionsWHERE 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.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.

Common mistakes

  • Stale statistics after bulk loads.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Terminology layer

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.

Aliases that resolve here
table statistics and analyzedatabase statistics analyzeplanner row estimate
Confirmation

Confirm action

Continue?