Home›Learn›How a query travels
Course · FoundationsLesson 3 of 330 read

How a query travels

What actually happens between sending an UPDATE and getting UPDATE 1 back?

Ten steps, animated, with an index and without.

Press Play, or step with the arrows. Switch to Without the index, or Side by side, to see what one missing index changes.

Most system design courses start with how a web request travels. This lesson does the same for the database: one UPDATE, from the driver to the disk and back, with the numbers we measured on real servers.

Try it · real PostgreSQL in your browser

Run it: one missing index

A real PostgreSQL runs in this page, with a users table of 50,000 rows. Run the steps in order and watch the plan change.

  1. Find one user, no index on email
    EXPLAIN (ANALYZE, BUFFERS)
    SELECT * FROM users WHERE email = 'user42000@example.com';
  2. Add the index
    CREATE INDEX users_email ON users (email);
  3. Same query again
    EXPLAIN (ANALYZE, BUFFERS)
    SELECT * FROM users WHERE email = 'user42000@example.com';
  4. Watch an UPDATE write a new version
    SELECT ctid, xmin, name FROM users WHERE id = 42;
    UPDATE users SET name = 'Asha' WHERE id = 42;
    SELECT ctid, xmin, name FROM users WHERE id = 42;

The same trip in MySQL and MongoDB

Go deeper

The steps are the same in every engine. The names and the files change.

StepPostgreSQLMySQL, InnoDBMongoDB, WiredTiger
ConnectionA process per connectionA thread per connectionA thread per connection
PlanPlanner, from table statisticsOptimizer, from index statisticsTries candidate plans, caches the winner
Find the rowIndex, then the table pageThe primary key B-tree holds the row; a secondary index gives the key, then a second lookupIndex, then the document in the collection file
Old versionStays in the table page until VACUUMGoes to the undo log until purgeKept in the cache while a reader needs it
Log flushed at commitWALRedo log and binlogJournal, with j: true or w: "majority"
Commit wait, Notebook 20.236 ms0.556 ms0.954 ms
To replicasWAL streamBinlogOplog
Data file writtenLater, at a checkpointLater, by the page cleanerLater, at the 60 second checkpoint

Commit times are from one client on the Notebook 2 runner. Your disk will give different numbers; the order between the settings is what carries over.

Where the time goes

Go deeper

For a lookup by an indexed key, most of the trip is two network hops and one log flush. Reading a few pages is cheap, especially when they are already in memory.

Two things turn this into a slow query. A missing index turns 4 pages into 19,231. A lock held by another transaction turns step 4 into a wait with no upper limit except your lock_timeout. When a query is slow, ask which of the ten steps it is stuck in.

See it on your own server

Go deeper
BEGIN;
EXPLAIN (ANALYZE, BUFFERS)
UPDATE users SET plan = 'pro' WHERE email = 'asha@example.com';
ROLLBACK;
-- Index Scan or Seq Scan, and Buffers: shared hit=… read=…
-- Who is waiting on a lock right now (step 4)
SELECT pid, wait_event_type, wait_event, state, now() - query_start AS running, query
FROM pg_stat_activity
WHERE wait_event_type = 'Lock';
-- MySQL: the plan (estimates only)
EXPLAIN UPDATE users SET plan = 'pro' WHERE email = 'asha@example.com';

Paste a plan into the EXPLAIN reader to see which step costs the most. MySQL only runs EXPLAIN ANALYZE on SELECT and multi-table UPDATE and DELETE, so a single-table UPDATE gets plain EXPLAIN here. Run the same WHERE clause as a SELECT to see the actual cost.

Connected

Where this shows up

The same mechanism, indexes and plans, what a commit promises, elsewhere on the site: in real incidents, in measurements, and in designs.

Check yourself · 3 questions

Check yourself

Pick an answer. You will see whether it is right, and why.

1. With default settings, when UPDATE returns, what is on disk?

2. In Notebook 3, how many pages did PostgreSQL read to find one user by email without an index, on 1,000,000 rows?

3. With default asynchronous replication, when does the replica receive the change?

Part 1 of 4 · Learn · 23 pagesAll pages

How do databases actually work? From one row on disk to transactions: the words you need, PostgreSQL, MySQL and MongoDB from the files up, then Redis, Cassandra and vector search, side by side, and the query and concurrency traps.