Skip to main content
Albero Academy

Indexes

Speed up queries by letting the database jump to matching rows.

Data & Schema9 min

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

index what you filter/join on
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 order
CREATE INDEX idx_prog_pkg ON students(programme_id, package);

The trade-off

Key takeaways

  1. 01An index is a sorted lookup structure that turns full-table scans into fast lookups.
  2. 02Index the columns you filter, join and sort on (primary keys are indexed automatically).
  3. 03Indexes cost space and slow writes — index deliberately, not everything.
  4. 04EXPLAIN shows whether a query uses an index.

Finished this chapter?

Mark it complete to track your progress through SQL.