SQL Server
ALTER INDEX idx_orders_customer ON orders REBUILD;
Online with ONLINE = ON, else the table is locked. REORGANIZE is the lightweight online alternative for light fragmentation.
Indexes bloat after churn. Each engine has its own rebuild verb, and one of them (MySQL) rebuilds the whole table instead.
Filed under Index
Five engines, four commands and one workaround: ALTER INDEX REBUILD (SQL Server, Oracle), REINDEX (PostgreSQL, SQLite), and MySQL only rebuilds via table-level DDL. Concurrency needs the ONLINE/CONCURRENTLY keyword on each.
Rebuilding rewrites the index structure compactly. The portability story is mixed: same idea, five different commands, and concurrency guarantees differ per engine.
ALTER INDEX idx_orders_customer ON orders REBUILD;
Online with ONLINE = ON, else the table is locked. REORGANIZE is the lightweight online alternative for light fragmentation.
REINDEX INDEX idx_orders_customer;
Takes an exclusive lock while building; REINDEX INDEX CONCURRENTLY avoids blocking writes (PostgreSQL 12+).
ALTER TABLE orders ENGINE=InnoDB;
InnoDB has no per-index rebuild command. A null ALTER TABLE ... ENGINE=InnoDB, or OPTIMIZE TABLE (which InnoDB maps to the same recreate), rebuilds the table and all its indexes as online DDL.
REINDEX idx_orders_customer;
REINDEX works on an index, table or the whole database (REINDEX; with no argument).
ALTER INDEX idx_orders_customer REBUILD;
Add ONLINE to keep DML running (Enterprise Edition). For light cleanup, ALTER INDEX ... COALESCE merges sparse leaf blocks without a rebuild, and SHRINK SPACE also releases space in ASSM tablespaces.
SQL Server: sys.dm_db_index_physical_stats avg_fragmentation_in_percent. PostgreSQL: pgstattuple bloat. InnoDB: innodb_index_stats. In practice, rebuild after bulk loads and large deletes, not on a schedule.
For SQL Server light fragmentation: yes, it is always online and cheaper. Rebuild for heavy fragmentation, changing fillfactor, or switching compression.
SQL Server rebuild does (reorganize does not). PostgreSQL REINDEX does not; run ANALYZE. Oracle REBUILD recomputes stats by default.
The bread-and-butter index statement: nearly identical everywhere, with a few differences in naming and locking.
Dropping an index is nearly universal, with one twist: SQL Server and MySQL require the table name in the statement.
The periodic-housekeeping commands: reclaim dead space and refresh the statistics the optimizer plans with.
SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.