intermediatedbms

Indexes

The thing that makes queries fast without changing any data.

Key Points

What it is

An index is a separate data structure the database maintains alongside a table that makes lookups on specific columns much faster. Instead of scanning every row, the database uses the index to jump directly to the relevant rows. The tradeoff is that indexes slow down writes and take up storage.

Why it exists

A table with 100 million rows and no index means every query reads all 100 million rows to find the ones you want. That's fine for small tables. For large ones it takes minutes. An index turns that into milliseconds.

Intuition

Think of a book's index at the back. If you want to find every page that mentions 'normalization', you don't read every page — you go to the index, find 'normalization', and it tells you exactly which pages to go to. A database index works the same way. The database builds and maintains this lookup structure so it doesn't have to read the whole table every time you query.

When to use

When not to use

Common mistakes

Notes

In practice

When pulling large datasets from a production database, slow queries are almost always a missing or misused index. Understanding indexes helps you write queries that the database can execute efficiently and helps you have informed conversations with database admins about performance.

Was this concept helpful?

Spot an error or gap? Suggest an edit →

Related concepts

sql-basicstransactions-and-acidrelational-model
Chirp at me!