Mid SQL

How do you rebuild or reorganize an index?

Reorganizing an Index: A lighter operation that compacts the index and defragments it. It is

used when fragmentation is low (less than 30%).

SQL Server:

ALTER INDEX idx_name ON table_name REORGANIZE;

PostgreSQL:

PostgreSQL does not have an explicit REORGANIZE command, but you can run VACUUM to

clean up the database and reduce fragmentation:

VACUUM INDEX idx_name;

MySQL:

OPTIMIZE TABLE table_name;

Rebuilding an Index: A more intensive operation where the index is completely dropped

and recreated. This can reduce fragmentation to nearly zero.

SQL Server:

ALTER INDEX idx_name ON table_name REBUILD;

PostgreSQL:

REINDEX INDEX idx_name;

MySQL:

ALTER TABLE table_name DROP INDEX idx_name, ADD INDEX idx_name

(column_name);

More from SQL Server Tutorial

All questions for this course