How do you rebuild or reorganize an index?
Short answer: Reorganizing an Index: A lighter operation that compacts the index and defragments it.
Explain a bit more
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);
Real-world example (ShopNest)
ShopNest adds an index on Orders(CustomerId, CreatedAt) because “my recent orders” is queried constantly.
Say this in the interview
- Define — one clear sentence (the short answer above).
- Example — relate it to a project like ShopNest or your real work.
- Trade-off — when you would not use it.
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png