Indexes

A lookup structure that lets the database jump to matching rows instead of scanning the whole table.

What is it?

Say users has 10 million rows and you run SELECT * FROM users WHERE email = 'amara@example.com'. With no shortcut, the database checks every row's email until it finds a match or reaches the end. That's slow, and it gets slower as the table grows.

An index fixes this. It's a separate, ordered structure, like the index at the back of a textbook, that maps a column's values to the rows that hold them. With an index on email, the database jumps almost straight to the matching row.

Explain like I'm 10

Without an index, finding a topic in a book means flipping through every page. With the index at the back, you look the topic up alphabetically and it tells you which page to turn to.

Examples

Before: scanning every row

-- No index on email.
-- The database checks every row's email, one at a time,
-- until it finds (or rules out) a match.
SELECT * FROM users WHERE email = 'amara@example.com';

With millions of rows and no index, this query's cost grows with the table's size. This is a 'full table scan.'

After: creating an index

CREATE INDEX idx_users_email ON users (email);

-- Same query as before, now much faster:
SELECT * FROM users WHERE email = 'amara@example.com';

With the index in place, the same query jumps almost directly to the matching row instead of checking every row.

How it works

Most indexes are built as a B-tree — a sorted, tree-shaped structure that finds a value in a few steps. It's like finding a word in a dictionary by repeatedly splitting the search space in half, instead of reading from the start.

The tradeoff: indexes aren't free. Every insert, update, or delete must also update every index on the table, which slows writes. Indexes also use extra disk space. So databases don't index every column automatically; you choose, usually for columns often searched or joined on.

Why does it exist?

Without indexes, any query that filters or joins on a real condition would scan the whole table, and query time would grow linearly (or worse) with data size. Indexes trade a little storage and slightly slower writes for much faster reads on the columns that matter.

When to use it

Index columns often used in WHERE, JOIN, or ORDER BY, especially on large tables. Foreign key columns are strong candidates, since they're joined on often.

When not to use it

Skip indexes on columns that are rarely searched or that change on almost every write (each index update adds write cost). Skip them on very small tables too: scanning a few hundred rows is already fast.

Common mistakes

  • Indexing every column 'just in case,' which slows every write without speeding up reads that don't use those columns.

  • Expecting an index to help a query that doesn't filter, join, or sort on that column.

  • Forgetting that foreign key columns often need an index too, since joins on them are common.

Practice exercises

  1. Easy:

    In your own words, explain why a full table scan slows down as a table grows, but an indexed lookup barely does.

  2. Medium:

    Write a CREATE INDEX statement for the status column of an orders table that's often filtered by status.

  3. Hard:

    Describe a scenario where adding an index would hurt overall performance instead of helping.

Interview questions

What is a database index?

A separate, ordered lookup structure for a column that lets the database jump to matching rows instead of scanning the whole table.

What's the tradeoff of adding an index?

Faster reads on that column, but extra disk space and slower writes, since every insert, update, or delete must update the index too.

What data structure do most database indexes use internally?

A B-tree. It keeps data sorted in a tree shape, so finding a value takes relatively few steps.