What is the main cost of adding an index to a table?
Options
- Slower SELECT queries on the column that is indexed
- Extra storage and slower writes, since the index must be updated (Correct option)
- The table is locked permanently after the index is created
- Foreign keys are disabled on the table that is indexed
Answer
Extra storage and slower INSERT/UPDATE/DELETE because the index must be maintained
Why
An index is an extra sorted structure (usually a B-tree) that lets the database find rows quickly, like the index at the back of a book.
But every time you insert, update or delete a row, every index on that table must be updated too, and indexes take disk space.
So index the columns your important queries filter, join or sort on, not every column.
Remember
Indexes speed up reads and slow down writes.