ANALYZE and VACUUM
Overview
ANALYZE refreshes optimizer statistics. PostgreSQL routine VACUUM makes obsolete tuple space reusable, updates visibility information and helps prevent transaction ID wraparound; VACUUM FULL rewrites a table and requires stronger locking. Autovacuum should be monitored rather than disabled casually.
First example
SELECT stat FROM sqlite_stat1 WHERE idx='ad_balance';Observed SQLite result (rows in JSON):
[ [ "2 1" ]]How it works
SQLite ANALYZE populates sqlite_stat1; SQLite VACUUM rebuilds the database file and is not PostgreSQL routine vacuum. The SQL fixture records the statistics and page size; the separate host lab measures compaction in a temporary file. Maintenance is engine-specific; plan time, free space, locks and backup verification.
Practical example
SELECT COUNT(*),COUNT(DISTINCT balance) FROM ad_account;Observed SQLite result (rows in JSON):
[ [ 2, 2 ]]Practice and mistakes
Practice: inspect ad_balance statistics, then compare actual row counts. Hint: query sqlite_stat1 by idx. Check: two index entries, average one per key. Mistake: assuming ANALYZE shrinks database files.
PostgreSQL 17 example — NOT RUN
SELECT relname,last_analyze,last_autovacuum,n_dead_tup FROM pg_stat_user_tables ORDER BY relname;Expected result, not executed: "Maintenance statistics; estimates and timestamps vary."