Before you set off, you open a maps app and check the route: "take the toll road, exit at the second gate". You haven't driven anywhere yet, but you already know which way you'll go, and if the route looks odd, you can change it before you're stuck in traffic.
You can ask a database the same thing. EXPLAIN asks the database to tell you the plan (query plan) it will use for a query: which table it reads, whether it goes through an index, and whether it still has to sort the result itself. In the Indexes & Performance lesson you met SCAN and SEARCH. Now we read a fuller plan, then translate it into PostgreSQL's words.
The table and the query we use
The orders table in the Lab is the same as in the Indexes & Performance lesson: 20,000 Sunset Coffee orders from 200 customers, with the columns id, customer_id, status, created_at and total. Siti (customer_id 2) has 100 orders. The "Order history" page runs:
SELECT * FROM orders WHERE customer_id = 2 ORDER BY created_at DESC;
Put EXPLAIN QUERY PLAN in front of it. The query isn't run; all that comes out is its plan. With no index at all, sqlite3 in the terminal draws it like this:
QUERY PLAN
|--SCAN orders
`--USE TEMP B-TREE FOR ORDER BY
- Each line is one step. A step indented to the right is part of the step above it.
SCAN orders: read the whole table, 20,000 rows, to find the 100 that match.USE TEMP B-TREE FOR ORDER BY: the result goes into a temporary structure (a temp B-tree) so it can be sorted, because nothing is in order already.
In the Lab and the Playground, the same plan comes out as a table with the columns id, parent, notused and detail. The text in the detail column is exactly the lines above.
The words in a SQLite plan
| Plan line | What it means |
|---|---|
SCAN orders |
Read the whole table from start to end |
SEARCH orders USING INDEX idx (customer_id=?) |
Jump through the index to the matching rows, then fetch each row from the table |
SEARCH orders USING COVERING INDEX idx (customer_id=?) |
Every column needed is already in the index, the table isn't touched at all |
SEARCH orders USING INTEGER PRIMARY KEY (rowid=?) |
Look up by the primary key, for example WHERE id = 42 |
USE TEMP B-TREE FOR ORDER BY |
Sort it yourself in a temporary structure |
SCAN orders USING INDEX idx |
Read the whole index in order, not the whole table |
What's in the brackets, like (customer_id=?), is the column used to search the index. The ? means "any value", because the plan is the same for Siti and for Budi.
USE TEMP B-TREE: sorting it yourself
Add an index on customer_id, then ask for the plan again:
CREATE INDEX idx_orders_customer ON orders(customer_id);
Before you look at the plan, guess: is this index enough, or does the database still have to sort Siti's 100 orders itself?
QUERY PLAN
|--SEARCH orders USING INDEX idx_orders_customer (customer_id=?)
`--USE TEMP B-TREE FOR ORDER BY
SEARCH finds Siti's 100 orders quickly. But that index is only sorted by customer_id and knows nothing about created_at, so the second line is still there: yes, the database still sorts them itself. With 100 rows you won't feel it, but for a customer with 50,000 orders, sorting on every page view is expensive.
The cure is the composite index from the Indexes & Performance lesson, in the same order as the query:
CREATE INDEX idx_orders_customer_date ON orders(customer_id, created_at);
QUERY PLAN
`--SEARCH orders USING INDEX idx_orders_customer_date (customer_id=?)
In this index, Siti's orders already line up by created_at. The database just reads them from the back for DESC, and the temp B-tree line is gone. If the query only needs columns that are in the index, for example SELECT created_at FROM orders WHERE customer_id = 2 ORDER BY created_at DESC, the plan becomes SEARCH orders USING COVERING INDEX idx_orders_customer_date (customer_id=?). Designing indexes for many queries at once is covered in the Index Strategy lesson.
The same in PostgreSQL: EXPLAIN and EXPLAIN ANALYZE
In PostgreSQL plain EXPLAIN is enough. This is PostgreSQL 18's answer for the same query on the same 20,000 rows, with no index:
Sort (cost=381.32..381.57 rows=100 width=24)
Sort Key: created_at DESC
-> Seq Scan on orders (cost=0.00..378.00 rows=100 width=24)
Filter: (customer_id = 2)
And with idx_orders_customer:
Sort (cost=135.63..135.88 rows=100 width=24)
Sort Key: created_at DESC
-> Bitmap Heap Scan on orders (cost=5.06..132.31 rows=100 width=24)
Recheck Cond: (customer_id = 2)
-> Bitmap Index Scan on idx_orders_customer (cost=0.00..5.04 rows=100 width=0)
Index Cond: (customer_id = 2)
- Read from the most indented line: that step runs first, and its result goes up to the step above it (
->). - Seq Scan =
SCAN: read the whole table row by row. Sort =USE TEMP B-TREE. - Index Scan =
SEARCH ... USING INDEX: go through the index, fetch rows one by one. - Index Only Scan =
COVERING INDEX: everything needed is in the index. - Bitmap Index Scan then Bitmap Heap Scan: the index is used to collect the locations of all matching rows first, then the table's pages are read in order. PostgreSQL picks it when quite a few rows match.
cost=5.06..132.31is an estimated cost, a relative number and not milliseconds: the cost up to the first row, then the total cost.rows=100is the estimated number of rows.
For finding a few rows in a big table, the usual speed order is: Index Only Scan fastest, Index Scan fast, Bitmap Index Scan medium, Seq Scan slow. If a big table's plan shows a Seq Scan for a query that needs only a few rows, the main fix is: add an index on the column in the WHERE or JOIN.
EXPLAIN ANALYZE is different: it really runs the query, then adds real numbers like actual time=0.027..0.051 rows=100.00 loops=1 and Execution Time: 0.067 ms. SQLite has no EXPLAIN ANALYZE, which is why the Lab times the query itself.
MySQL also uses EXPLAIN, but its answer is a table with one row per table in the query. MySQL 8.4 also has EXPLAIN ANALYZE, which runs the query, as in PostgreSQL. The columns you read most often:
| In SQLite | In MySQL (EXPLAIN) |
|---|---|
SCAN orders |
type: ALL, the key column empty |
SEARCH ... USING INDEX idx (customer_id=?) |
type: ref (or range for >, <, BETWEEN), key: idx |
SEARCH ... USING INTEGER PRIMARY KEY |
type: const |
COVERING INDEX |
Extra: Using index |
USE TEMP B-TREE FOR ORDER BY |
Extra: Using filesort |
📌 A plan is a choice, not a rule
PostgreSQL's planner picks the cheapest plan by its own estimate. On the same data, even with idx_orders_customer_date, for SELECT * it still picks a Bitmap Heap Scan followed by a Sort, because Siti's 100 orders are spread over 100 different table pages. For SELECT created_at it picks Index Only Scan Backward, and for ... LIMIT 5 it picks Index Scan Backward. So always check the plan on data that looks like production, don't guess.
⚠️ Misreading the plan, miswriting the query
- Thinking SCAN is always bad. Reading the 12 rows of a
settingstable is already instant. And when a query takes most of the rows, PostgreSQL picks a Seq Scan on purpose even if there's an index. - Wrapping the column in a function. With
idx_orders_customer_date,WHERE customer_id = 2 AND strftime('%Y-%m', created_at) = '2026-10'only uses thecustomer_idpart (the plan says(customer_id=?)), because the index holdscreated_at, notstrftime(...). Write it as a range:created_at >= '2026-10-01' AND created_at < '2026-11-01', and the plan becomes(customer_id=? AND created_at>? AND created_at<?). EXPLAIN ANALYZEon aDELETEorUPDATE. The data really changes. Wrap it inBEGIN;...ROLLBACK;.- In the exercise, the answers are lower-case words, exactly:
"fastest","fast","medium","slow".
TL;DR: EXPLAIN QUERY PLAN (SQLite) and EXPLAIN (PostgreSQL, MySQL) show a query's plan without running it; EXPLAIN ANALYZE in PostgreSQL runs it and adds real times. SCAN = Seq Scan (read the whole table), SEARCH ... USING INDEX = Index Scan, COVERING INDEX = Index Only Scan, USE TEMP B-TREE FOR ORDER BY = Sort. For a few rows in a big table: Index Only Scan fastest, Index Scan fast, Bitmap Index Scan medium, Seq Scan slow. A Seq Scan on a big table is usually cured with an index on the WHERE or JOIN column, and an expensive Sort with a composite index in the same order as the ORDER BY.
⚡ Ask for the plan first
Write:
EXPLAIN QUERY PLAN
SELECT * FROM orders WHERE customer_id = 2 ORDER BY created_at DESC;
QUERY PLAN
|--SCAN orders
`--USE TEMP B-TREE FOR ORDER BY
Meaning: EXPLAIN QUERY PLAN shows a query's plan without running it. SCAN orders means reading the whole table, and USE TEMP B-TREE FOR ORDER BY means the database sorts the result itself.
⚡ An index in the query's order
Write:
CREATE INDEX idx_orders_customer_date ON orders(customer_id, created_at);
Guess first: with an index on (customer_id) only, does the database still sort Siti's orders itself?
Yes. That index knows nothing about created_at, so the USE TEMP B-TREE FOR ORDER BY line is still there.
Meaning: In this composite index, Siti's orders already line up by created_at, so the plan is a single SEARCH line, with no sort.
⚡ Seq Scan in PostgreSQL
Guess first: a PostgreSQL plan says "Seq Scan". What is the database doing?
Reading the whole table row by row, the same as SCAN in SQLite.
Meaning: For finding a few rows in a big table: Index Only Scan fastest, Index Scan fast, Bitmap Index Scan medium, Seq Scan slow. A Seq Scan on a big table is usually cured with an index on the WHERE or JOIN column.
In the exercise, the answers are lower-case words, exactly: "fastest", "fast", "medium", "slow".