Chapter 7 of 10All chapters
Chapter 7 of 10
Indexes
Speed for lookups, cost for writes.
What to index
Index the columns you filter, join and sort on. Every index has to be updated on insert and update, so indexing everything makes writes slow and helps nothing.
- A unique index enforces uniqueness as well as speeding lookups.
- A composite index is used left to right, so column order matters.
Measuring
EXPLAIN shows whether an index was used. Adding indexes without reading the plan is guessing, and the guesses are usually wrong.