Skip to content

How an index makes a Postgres query fast

Working knowledgeUnder the hoodYou should know: basic SQL

You add an index and a slow query becomes instant. Here is what Postgres does differently, drawn one page read at a time.

01 The query

A sign-in page looks a user up by email. The table is stored on disk in fixed-size pages, and each page holds a handful of rows. Reading a page is the expensive part, so we will count those.

query.sql
SELECT * FROM users
WHERE email = 'ana@x.com';

02 Without an index: read everything

With no index, Postgres does a sequential scan. It reads every page, checks every row, and keeps going even after it finds Ana, because nothing tells it there isn’t a second match further on.

Sequential scan
Step 1 of 13
Rows found 0
users · 12 pages (a toy table)
p1
p2
p3
p4
p5
p6
p7
p8
p9
p10
p11
p12
  • reading now
  • read, no match
  • Ana’s row
  • not read yet

Postgres starts at page 1. Nothing is read yet.

0 of 12 pages read

03 With an index: walk the tree

A normal Postgres index is a B-tree: a separate, sorted structure of emails. Each email in the bottom layer points to the exact page and row where that user lives. Finding Ana means walking down from the top, one page per level.

Index scan
Step 1 of 5
Root · index page
below f… ↓
f… to m…
m… and up
Branch · index page
a… to al…
al… to b… ↓
b… to f…
Leaf · index page
alex@q.com → p3
ana@x.com → p7 r3
ana.w@x.com → p2
Table page 7 · heap
r2 · bo@y.com
r3 · ana@x.com · Ana
r4 · cy@z.com
  • pointer followed
  • compared
  • path taken
  • Ana
  • not read yet

Postgres has the query and the index. Nothing is read yet.

0 pages read
Sequential scanEvery pageDoubles when the table doubles.
Index scanOne per level, plus oneEach page holds many keys, so the tree gains a level only when the table grows many times over.

04 Ask Postgres what it did

Put EXPLAIN in front of the query to see the plan. The first line tells you which walk it chose.

Before
Seq Scan on users
Filter: (email = 'ana@x.com')
After
Index Scan using users_email_idx on users
Index Cond: (email = 'ana@x.com')

05 Add the index

SQL
CREATE INDEX users_email_idx ON users (email);

On a live table, a plain CREATE INDEX blocks writes until it finishes. Use CREATE INDEX CONCURRENTLY: it is slower to build, but inserts and updates keep working.

06 When the index won’t help

email LIKE '%@x.com'A wildcard at the startThe tree is sorted from the first character. With no known start, there is nowhere to begin the walk.
lower(email) = 'ana@x.com'A function on the columnThe index holds email, not lower(email). Index the expression itself: CREATE INDEX ON users (lower(email)).
status = 'active'Most rows matchIf the query returns a big share of the table, jumping around costs more than reading straight through. The planner picks the scan.
INSERT / UPDATEWrites pay for itEvery insert, and every update that changes email, also updates the tree. Index what you search, not every column.

07 Check yourself

Question

You have an index on email. Will WHERE lower(email) = 'ana@x.com' use it?

Question

Almost every row has status = 'active'. There’s an index on status. For WHERE status = 'active', what will Postgres most likely do?

08 Practise this

Was this tutorial useful?Your vote tells us what to improve next.
Spotted a mistake, or something unclear?