Index maintenance
- SQL course
- Expert
- Lesson 11/45
- 5 min
Learn rebuild and analyze.
Index maintenance
Indexes bloat and fragment over time. REINDEX rebuilds from scratch; VACUUM clears dead rows; ANALYZE refreshes statistics.
REINDEX INDEX idx_products_name;
VACUUM ANALYZE products;Maintenance uses three commands.
Index maintenance — details
On write-heavy tables, a weekly REINDEX makes sense. PostgreSQL REINDEX CONCURRENTLY avoids locks and keeps the application running.
Practice Index maintenance
Run REINDEX INDEX to rebuild the idx_products_name index.
Starter code
-- index maintenance
REINDEXSample solution
REINDEX INDEX idx_products_name;Expected output
REINDEX
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Rebuild the index
REINDEX INDEX ____
REINDEX INDEX idx_products_name
Explanation: idx_products_name is the right answer.
2 Index maintenance: match the terms
| Term | Meaning |
|---|---|
REINDEX |
rebuild |
VACUUM |
clean |
ANALYZE |
stats |
bloat |
bloat |
3 What does REINDEX do?
- Rebuilds the index Correct answer
- Drops the table
- Collects statistics
- Takes a backup
Explanation: REINDEX rebuilds the index.