Back to blog

One table got slow: do you need a bigger database yet?

Thắng Đoàn
Thắng Đoàn

FotoFlow has 10,000 users and logging in looks a user up by email across a growing table. The query takes two seconds and gets slower every day, so the first instinct is a bigger database. The real problem: with nothing to guide it, the database does a full table scan, reading every row. Scans grow with the table: fine at a thousand rows, painful at fifty million.

How a phone book does it

Nobody reads a phone book page by page. It is sorted, so you open the middle, see whether your name comes before or after, and throw away half the book. Repeat. Halving 50 million entries takes only about 26 steps. An index is a sorted copy of one column, with a pointer back to each full row.

CREATE INDEX ON users (email) builds a B-tree: a small tree of sorted keys where each level says which branch to follow. Three hops instead of fifty million rows, about 2 milliseconds. Each tree node is one disk page holding hundreds of keys, so every level multiplies the reach by hundreds, and three or four levels cover billions of rows. That is why an indexed lookup costs about the same at a million rows as at a billion.

Every index taxes every write

When a user signs up, the row goes into the table and into every index on it, each kept sorted. Indexes also take disk and memory. A table with 6 indexes does 7 writes per insert; unused indexes are pure cost. Index what you query, not everything: columns you search, join, or sort on.

Order matters in composite indexes

An index on (country, city) is sorted by country first, then city within each country. It speeds up queries on country, or country plus city, but a query on city alone cannot use it and falls back to a scan. Put the column you always filter on first; indexes help from the left.

When an index cannot help

A leading wildcard defeats it: WHERE email LIKE '%@gmail.com' says nothing about the start of the email, so sorted order is useless. Functions break it too: WHERE LOWER(email) = ... cannot use a plain index on email because the index stores the original values. Fixes: an expression index, a stored derived column, or a search engine.

Low selectivity defeats it as well. Index country and then query WHERE country = 'US' when 40 percent of rows match, and the database ignores the index, because hopping to a different page for each of 20 million rows is slower than reading the table front to back. An index pays off when it is selective: when it narrows the search to a small fraction of the rows. For a small slice you always query, like WHERE status = 'pending', a partial index holding just those rows is tiny and fast.

Do not guess, ask

Prefix a query with EXPLAIN and the database shows its plan: which index it will use and how many rows it expects to read. "Index Scan" on a few rows is good news; "Seq Scan" on a big table is the red flag, and EXPLAIN ANALYZE reports real timings. Most databases keep a slow-query log: start there.

If an index stores the columns a hot query needs, like CREATE INDEX ON users (email) INCLUDE (id, name), the database answers from the index alone and never visits the table: a covering index. It makes the index bigger, so cover only hot queries.

What this does not solve

An index makes reads fast, but it says nothing about what the database does with a write, why "saved" is true, or why a write-heavy FotoFlow table might still crawl. That is the storage engine, the next post.

Which query in your project would flip from scan to index scan with one line of DDL?
How many indexes does your most-written table have, and could you name the query each one serves?

Two seconds or two milliseconds: usually one sorted column apart.

Next in the series: Where does a database actually put your rows?

Sources

PostgreSQL Docs: Indexes

Designing Data-Intensive Applications

System Design Primer

Share:

Recommended for you

Enjoyed this article?

Subscribe for new articles. No spam. Unsubscribe anytime.

No pasting. This is the challenge.0/200

Typed live, never pasted.