A Conversation-Style Primer on Indexes for Beginners
Written like relaxed podcast notes, with no audio or guests. The question for today: what is an index, why do people keep saying "add an index", and should a beginner care?
What is an index?
Think about the index at the back of a textbook. If you want every page that mentions "photosynthesis", you could read the whole book, or you could look it up in the index and jump to the right pages. A database index works the same way. It is an extra structure that helps the database find rows quickly without reading every single one.
What happens without one?
The database does a scan: it reads every row in the table and checks whether it matches. For a table with a hundred rows, that is instant. For a table with millions of rows, it can be slow, especially if the query runs often.
How do I make an index?
One line of SQL:
CREATE INDEX idx_books_author ON books(author_id);
That tells the database: "keep a sorted list of author_id values with pointers to their rows". From then on, a query like this can use it:
SELECT * FROM books WHERE author_id = 7;
How do I know it is being used?
Ask the database how it plans to run the query. In SQLite:
EXPLAIN QUERY PLAN SELECT * FROM books WHERE author_id = 7;
Before the index you will typically see something like SCAN books. After it, you should see SEARCH books USING INDEX idx_books_author. That word change is the whole story.
Wait, do I already have indexes?
Probably, yes. Most databases automatically create an index for the primary key, and for columns marked UNIQUE. That is why looking up a row by its id is fast from day one.
So why not index every column?
Because indexes have costs. Each one takes up space, and every time you insert, update or delete a row, the database has to update the index too. Too many indexes make writing data slower. Indexes help most on columns you search, join or sort by often.
When should a beginner bother?
| Situation | Index worth it? |
|---|---|
| Tiny practice tables | Not really; scans are instant |
| A column you filter by in a frequent query, on a growing table | Yes |
| A foreign key column you join on often | Often yes |
| A column you never search by | No |
A fun experiment
Make a table with a couple of hundred thousand made-up rows, run a query with .timer on in the sqlite3 shell, add an index, and run it again. Seeing the difference with your own eyes is the best way to understand indexes. Our beginner's method for timing a query fairly shows how to do it properly.
One-line takeaway
An index is the database's version of a book index: a shortcut for finding things, which costs a little space and a little extra work every time you write.
A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.
Illustrative outline only: no live session, recording, registration or certificate is offered on this site. Any speakers, panels or schedules mentioned are hypothetical.