Database indexing explained: B-tree, composite, covering and partial indexes

Vinnie sniffs out the first rung of the scaling ladder: how a B-tree index finds rows without reading the whole table, why column order matters in a composite index, covering and partial indexes, how EXPLAIN shows whether an index is used, and the price every index charges on writes.

GazeekDATABASE SCALING PATTERNS WITH VINNIE·EPISODE 2
EPISODE 2

Database indexing explained: B-tree, composite, covering and partial indexes

Vinnie sniffs out the first rung of the scaling ladder: how a B-tree index finds rows without reading the whole table, why column order matters in a composite index, covering and partial indexes, how EXPLAIN shows whether an index is used, and the price every index charges on writes.

0:00 / 4:45

Diagrams in this video · 1Free in archhy

Copy and edit in archhy: How an index finds rows (new tab)

The diagrams, full size

Copy any of them into archhy, free, and change it for your own system.

After this lesson you can

  • Explain how a B-tree index finds rows without a full table scan
  • Choose the column order of a composite index
  • Use covering and partial indexes, and EXPLAIN to check an index is used
  • Weigh the cost an index adds to every write
Transcript

Database indexing explained: B-tree, composite, covering and partial indexes

Vinnie here. Rung one of the ladder. Indexing. It's the cheapest fix there is, and the one people forget first. By the end, you'll know how an index finds a row, how to build a good one, and what it costs you.

The problem

You ask for one customer's orders, in a table of ten million rows. With no index, the database has only one way to answer. Read every row, and keep the few that match. That's a sequential scan. It's fine on a small table. But it gets slower every time the table grows.

How an index finds rows

Let's sniff along the path. One. No index. The database reads the whole table, start to finish. Two. With an index, it starts at the root of a B-tree, a balanced, sorted tree. Each level says which branch to take. Three. A few steps down, it reaches a leaf. That holds the key, forty-two, and where its rows live. Four. So it fetches only the matching rows. A few pages read, instead of the whole table. Five. But there's a price. Every insert and delete, and many updates, have to change the indexes as well.

Composite, covering and partial

Now the patterns. A composite index covers more than one column, and the order matters. A good rule of thumb: put the column you test for equality first, then the range, like a date. This one helps a search by customer, or by customer and date. A search by date alone usually can't use it well, because date isn't the leading column. PostgreSQL 18 added skip scan, which helps sometimes, when the leading column has few distinct values. A covering index holds every column the query needs. In PostgreSQL, include adds them as payload, so the answer can often come from the index alone. That's an index-only scan. A partial index covers only some rows. Here, just the open orders. It's smaller and cheaper to keep up, but only queries that ask for open orders can use it. To check, put explain in front of the query. Look for an index scan, bitmap or index-only. If you expected a few rows from a big table and see a seq scan, the index isn't helping. For a big share of the table, though, a seq scan can be the right plan.

The trade-off

So when should you index? Columns you filter, join and sort on, in the queries you run all the time. And when not? Every index slows down writes, and takes space. On a table that's mostly writes, each extra index hurts. A column where most rows share one value rarely helps on its own. And an index nothing uses is pure cost, so remove it. Unless it enforces a primary key or a unique rule: those stay.

How the big databases do it

PostgreSQL and MySQL both use B-trees for ordinary indexes. SQL Server has the same ideas with its own names. Included columns for covering, and filtered indexes for partial. MongoDB has compound indexes, where field order matters just the same, and partial indexes too. And a managed PostgreSQL or MySQL, on any cloud, is the same engine underneath. The same rules apply.

Remember

Let's recap. An index lets the database go straight to the rows, instead of reading the whole table. In a composite index, the leading column matters. Covering and partial indexes save even more. Check with explain. And remember, every index costs you on writes.

The full article, and what's next

Next time, rung two. Query optimization. We read the query plan, and hunt down the N plus one problem. See you there.

Read the article

PostgreSQL: Index Types: the whole idea in writing, free.

Read it ↗