// dialect comparison

Maintenance commands: VACUUM, ANALYZE, OPTIMIZE in every dialect

The periodic-housekeeping commands: reclaim dead space and refresh the statistics the optimizer plans with.

Filed under Sessions & admin

The verdict

Four different worlds. PostgreSQL needs vacuuming enough that autovacuum runs by default; SQL Server and Oracle reuse space internally; MySQL and SQLite each get one rebuild command. Statistics refresh is the common denominator, with five different spellings.

Where they differ

No shared syntax or even shared concepts: PostgreSQL's VACUUM reclaims dead tuples, SQL Server never needs vacuuming, and everyone wants fresh statistics.

How each engine writes it

SQL Server

UPDATE STATISTICS orders;
-- space reclamation is automatic; index bloat is handled by ALTER INDEX ... REORGANIZE

No VACUUM: deleted space is reused. The maintenance is statistics (UPDATE STATISTICS or Ola Hallengren's scripts) and index reorganize/rebuild.

PostgreSQL

VACUUM ANALYZE orders;
-- autovacuum normally covers this; VACUUM FULL reclaims all space but locks

VACUUM removes dead tuples without blocking reads/writes; ANALYZE refreshes planner stats. VACUUM FULL rewrites the table with an exclusive lock; pg_repack avoids the lock.

MySQL

ANALYZE TABLE orders;
OPTIMIZE TABLE orders;

ANALYZE refreshes InnoDB statistics under a read lock; OPTIMIZE TABLE maps to a table rebuild on InnoDB, done as online DDL that blocks writes only briefly at the start and end.

SQLite

VACUUM;
ANALYZE;

VACUUM rebuilds the whole database file, reclaiming free pages; it needs enough free space for a copy and an exclusive lock. ANALYZE populates sqlite_stat1 for the query planner.

Oracle

EXEC DBMS_STATS.GATHER_TABLE_STATS(USER, 'ORDERS');

No vacuum: undo + segment reuse. DBMS_STATS is the statistics API (never ANALYZE ... COMPUTE for real systems); segment shrink via ALTER TABLE ... SHRINK SPACE.

Questions that come up

Why is PostgreSQL's autovacuum falling behind?

Long-running transactions hold back dead-tuple cleanup: one open idle transaction blocks vacuum progress everywhere. Monitor pg_stat_user_tables dead tuples and long transactions.

Does ANALYZE block anything?

Engine-dependent. PostgreSQL's ANALYZE, SQL Server's UPDATE STATISTICS and Oracle's DBMS_STATS do not block reads or writes. MySQL's ANALYZE TABLE holds a read lock, so writes wait, and SQLite's ANALYZE is a write transaction that blocks other writers.

How often should I run these?

Lean on automatics first: autovacuum/autovacuum-analyze in PostgreSQL, auto-updated stats in SQL Server and Oracle, InnoDB auto-recalc. Manually after bulk loads and large deletes.

Keep reading

// write it once, run it anywhere

Switch engines without relearning SQL

SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.