Without an index, finding matching rows means scanning the whole table. An index is a sorted lookup structure — like a book's index — that lets the database jump straight to the rows it needs, turning slow scans into fast lookups.
Creating indexes
CREATE INDEX idx_students_email ON students(email);CREATE INDEX idx_students_programme ON students(programme_id);-- A composite index helps queries filtering both, in orderCREATE INDEX idx_prog_pkg ON students(programme_id, package);
The trade-off
Key takeaways
- 01An index is a sorted lookup structure that turns full-table scans into fast lookups.
- 02Index the columns you filter, join and sort on (primary keys are indexed automatically).
- 03Indexes cost space and slow writes — index deliberately, not everything.
- 04EXPLAIN shows whether a query uses an index.
