Pcornel
Databases · Indexes & Query Plans

Table Statistics and ANALYZE

Cheat sheet · https://pcornel.dk/topic.php?topic=6172

Back to the Topic

Answer

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

Remember

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

First 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

Verify in this order

data changes → statistics refreshed → better estimates → better plans

Where it usually breaks

Bulk loads followed by immediate queries run with old statistics.

Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=6172

Confirmation

Confirm action

Continue?