重建 / 重组索引
Oracle
ALTER INDEX customer_city_idx REBUILD ONLINE;ONLINE 可在不锁表的情况下重建(Enterprise Edition)。移动到其他表空间:... REBUILD TABLESPACE new_ts。
PostgreSQL
REINDEX INDEX CONCURRENTLY customer_city_idx;
-- Whole table: REINDEX TABLE CONCURRENTLY customer;CONCURRENTLY(PG 12+)在不阻塞表写入的情况下重建;速度较慢但安全。
MySQL
-- Rebuild InnoDB indexes + table:
ALTER TABLE customer ENGINE=InnoDB, ALGORITHM=INPLACE, LOCK=NONE;
-- Alternative: OPTIMIZE TABLE customer;LOCK=NONE 允许并发写入;并非所有 ALTER 都能使用 INPLACE。
SQL Server
-- Rebuild for high fragmentation:
ALTER INDEX ALL ON dbo.customer REBUILD WITH (ONLINE = ON);
-- Lighter option for low fragmentation:
-- ALTER INDEX ALL ON dbo.customer REORGANIZE;常用阈值:碎片率 5-30% 执行 REORGANIZE,>30% 执行 REBUILD。ONLINE 是 Enterprise 功能。
MongoDB
db.customer.reIndex()在前台重建所有索引并锁定集合。在 6.0+ 中仅能在独立实例上运行(副本集成员上不允许);在副本集上,应逐节点(滚动)通过 dropIndex + createIndex 重建单个索引。
Redis
# Not applicable (core). If RediSearch is used:
# FT.ALTER / rebuilding is done per the module documentation.键值模型没有传统意义上的索引维护。