Indexes - Speed Up Queries
An Index helps the database find data faster.
Like a book index - instead of reading every page, jump to the right spot.
Create an index:
CREATE INDEX idx_salary ON employees(salary);Now queries filtering by salary are much faster!
Types:
- B-tree - default, good for ranges
- Hash - exact matches only
- Composite - multiple columns
Trade-offs:
- Faster reads
- Slower writes (index must update)
- Uses storage space
Index columns you frequently filter or join on.
Check your understanding
What is a trade-off of adding indexes?