回收空闲空间(shrink / vacuum / compact)
Oracle
ALTER TABLE customer ENABLE ROW MOVEMENT;
ALTER TABLE customer SHRINK SPACE;SHRINK 会降低高水位线(HWM)。若要同时收缩索引,使用 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 会获取 AccessExclusiveLock 并重写整张表;不要在高峰时段执行。替代方案:pg_repack。
MySQL
OPTIMIZE TABLE customer;在 InnoDB 上会重建表并释放 data_free 空间;大表上耗时很长。
SQL Server
-- Generally NOT RECOMMENDED (causes fragmentation). If really needed:
DBCC SHRINKFILE (N'MyDB_data', 1024); -- target 1024 MB收缩数据文件会导致索引碎片;之后需对索引执行 REBUILD。对于日志,先执行备份。
MongoDB
db.runCommand({ compact: "customer" })在 WiredTiger 上会尝试将空闲空间归还给操作系统(并非总能成功)。在 4.4+ 中不会阻塞读写,只阻塞同一集合上的某些 DDL 操作;由于会产生大量 IO,在从节点上并在维护窗口内执行更安全。
Redis
MEMORY PURGE
# Tries to return jemalloc fragmentation to the OSactivedefrag yes 设置可在线自动整理碎片;MEMORY DOCTOR 会给出建议。