SQL Indexes Explained With a Library Analogy
Why your query is slow, and what an index actually does under the hood.
Imagine a library with a million books and no catalogue. Finding a specific title means walking every shelf. That is a full table scan.
An index is the catalogue
It is a separate, sorted structure that maps a value to the location of the row. Looking up a name in a sorted catalogue takes a handful of steps instead of a million.
Why not index everything?
Every index must be updated on every insert, update and delete. Over-indexing turns fast writes into slow ones and wastes disk.
Composite indexes and column order
An index on (status, created_at) helps queries filtering by status, or by status and date. It does not help a query filtering only by date. The leftmost prefix rule matters.
How to check
Put EXPLAIN in front of your query. If you see "type: ALL", you are doing a full scan.