← All articles

Database Indexing Explained: How It Speeds Up Your Queries

Burncode Team 3 min read

A huge share of "why is our database slow" problems come down to one missing thing: an index. It's one of the highest-leverage things you can understand about databases, and it's simpler than most explanations make it sound.

The library analogy

Imagine a library with no card catalog and no organization system — every book just placed wherever there happened to be shelf space. Finding a specific book means walking the aisles checking every single one. That's a database table without an index: to find a row, the database checks every row, one at a time, until it finds what it's looking for. This is called a full table scan, and on a small table it's fine — on a table with millions of rows, it's brutally slow.

An index is the card catalog. It's a separate, ordered structure that says "here's exactly where to find rows where this column equals this value," so the database can jump straight there instead of checking everything.

How it actually works under the hood

Most databases implement indexes using a data structure called a B-tree — a balanced tree that keeps values in order and lets the database narrow down a search by roughly halving the remaining possibilities at each step, similar to how you'd find a word in a printed dictionary. Instead of checking a million rows one by one, the database might check a dozen tree nodes and land directly on the answer.

This is why adding the right index can turn a query that takes several seconds into one that takes a few milliseconds — you're not making the database work harder, you're giving it a much shorter path to the same answer.

What to actually index

  • Columns used in WHERE clauses — anything you filter by frequently is a strong index candidate.
  • Foreign keys — columns that reference another table, since joins rely on them heavily.
  • Columns used in ORDER BY — an index that already stores data in the right order can skip the sorting step entirely.
  • Columns in JOIN conditions — joining large tables without indexes on the join columns is one of the most common causes of a slow query.

The trade-off nobody mentions until it bites

Indexes aren't free. Every index has to be updated whenever you insert, update, or delete a row, which means write operations get slower as you add more indexes. They also take up real disk space, which matters at scale. The mistake to avoid isn't "not enough indexes" or "too many indexes" in the abstract — it's indexing columns that are barely ever queried, which slows down every write for a benefit you never actually collect.

A good rule of thumb: index the columns your actual queries filter, join, and sort by — check your slow query log if you have one — and resist the urge to index everything "just in case."

Diagnosing exactly which indexes a real production database needs — versus which ones are silently costing you on every write — is a routine part of the infrastructure work we do, and it's often the single highest-impact change available for a system that's started feeling sluggish under real traffic.