DBAtlas · Database operations atlas

Reclaim free space (shrink / vacuum / compact)

Oracle

ALTER TABLE customer ENABLE ROW MOVEMENT;
ALTER TABLE customer SHRINK SPACE;

SHRINK lowers the high water mark (HWM). To shrink indexes too, use SHRINK SPACE CASCADE.

PostgreSQL

-- Online, light locking (does not return space to the OS):
VACUUM (VERBOSE, ANALYZE) customer;
-- Return space to the OS (locks the table!):
-- VACUUM FULL customer;

VACUUM FULL takes an AccessExclusiveLock and rewrites the table; do not run it at peak hours. Alternative: pg_repack.

MySQL

OPTIMIZE TABLE customer;

On InnoDB it rebuilds the table and returns the data_free space; takes a long time on large tables.

SQL Server

-- Generally NOT RECOMMENDED (causes fragmentation). If really needed:
DBCC SHRINKFILE (N'MyDB_data', 1024);  -- target 1024 MB

Shrinking a data file causes index fragmentation; REBUILD the indexes afterwards. For the log, take a backup first.

MongoDB

db.runCommand({ compact: "customer" })

On WiredTiger it tries to return free space to the operating system (not always possible). In 4.4+ it does not block reads/writes, only certain DDL operations on the same collection; because it generates heavy IO, running it on a secondary and in a maintenance window is safer.

Redis

MEMORY PURGE
# Tries to return jemalloc fragmentation to the OS

The activedefrag yes setting performs online automatic defragmentation; MEMORY DOCTOR gives recommendations.

Open in DBAtlas →