Snowflake, BigQuery & Databricks · Amazon Redshift

Redshift VACUUM and ANALYZE operations

awsMediumredshiftvacuumanalyzedatabase-maintenance

Interview question

Why are VACUUM and ANALYZE necessary in Redshift, and how do they work?

How to answer

The short answer

In Amazon Redshift, data blocks are immutable. When you run an UPDATE or DELETE statement, Redshift does not erase data inside existing blocks on disk. Instead, it marks old rows as deleted (tombstoned) and appends newly updated rows to the end of the table in an unsorted region.

Over time, two things happen: disk space fills up with deleted ghost rows, and newly appended rows remain unsorted, preventing zone maps from skipping blocks.

What VACUUM does

  • VACUUM DELETE: Reclaims disk space occupied by deleted rows and consolidates fragmented blocks.
  • VACUUM SORT: Resorts newly appended rows into the table's sort key order, restoring zone map efficiency.
  • VACUUM FULL: Performs both reclamation and resorting.
SQL
-- Reclaim space and resort a specific table
VACUUM FULL sales_transactions;

Modern Redshift versions perform automated vacuum operations in the background during periods of light cluster load. However, after massive or table rewrites, running an explicit VACUUM is often required before returning the table to analyst queries.

What ANALYZE does

The ANALYZE command updates the statistical metadata of table columns, including row counts, null counts, distinct value counts, and distribution histograms.

The relies entirely on these statistics to choose between hash joins and nested loops, and to decide whether to broadcast or redistribute tables. If statistics are missing or stale after a major load, the optimizer may choose a disastrous query plan that terabytes across slices.

Always run ANALYZE table_name; immediately after a batch COPY pipeline finishes.