Two queries return the same answer, one in a blink and the other after a coffee. SQL does not say how to get the answer, so the difference is hidden in the plan DuckDB chose: what it read, what it filtered early, how it joined. Reading a plan is how you find out, and it is a skill you use whenever a query is slower than it should be.
The idea in plain words
Where we are. The SQL skill's indexes and plans topic used a cost simulator. Here it is DuckDB's own plan, read as data. The next topic asks what to do when the answer to a query does not fit in memory.
The tree of operators the database chose to answer a query, data flowing from the scans at the leaves up to the result. EXPLAIN shows it without running; EXPLAIN ANALYZE runs it and records how many rows each operator produced.
A plan is a tree you can read
# EXPLAIN prints the plan as boxes; the JSON form is the same tree as dataquery = "SELECT symbol, avg(close) AS mean_close FROM bars WHERE date >= '2026-08-01' GROUP BY symbol"print(con.sql("EXPLAIN " + query).fetchall()[0][1])print(operator_names(plan_tree(con, query)))
╭─ Projection ────────────────────────────╮ │ Projections: │ │ __internal_decompress_string(#0), #1 │ │ ~5 rows │ ╰────────────────────┬────────────────────╯ ╭─ Hash Group By ────┴────────────────────╮ │ Groups: #0 │ │ Aggregates: avg(#1) │ │ ~5 rows │ ╰────────────────────┬────────────────────╯ ╭─ Projection ───────┴────────────────────╮ │ Projections: symbol, close │ │ ~6,698 rows │ ╰────────────────────┬────────────────────╯ ╭─ Projection ───────┴────────────────────╮ │ Projections: │ │ __internal_compress_string_ubigint(#0), │ │ #1 │ │ ~6,698 rows │ ╰────────────────────┬────────────────────╯ ╭─ Seq Scan ─────────┴────────────────────╮ │ Table: memory.main.bars │ │ Type: Sequential Scan │ │ Projections: symbol, close │ │ Filters: date >= '2026-08-01'::DATE │ │ ~6,698 rows │ ╰─────────────────────────────────────────╯ ['PROJECTION', 'HASH_GROUP_BY', 'PROJECTION', 'PROJECTION', 'SEQ_SCAN']
Read it from the bottom: the scan reads bars, then the aggregate groups the rows, then projections shape the output. The scan box lists Filters: the date test was handed to the reader, which can skip data before producing any row. A condition that DuckDB cannot push into the scan, such as one on an aggregate result (HAVING), shows up as a separate FILTER node higher up, after the work is done.
# A WHERE condition lives in the scan; a HAVING condition becomes a FILTER node after the aggregatewhere = plan_tree(con, "SELECT symbol, avg(close) FROM bars WHERE date >= '2026-08-01' GROUP BY symbol")having = plan_tree(con, "SELECT symbol, avg(close) AS m FROM bars GROUP BY symbol HAVING avg(close) > 50")print("WHERE ->", scan_filters(where), "| FILTER nodes:", operator_names(where).count("FILTER"))print("HAVING ->", scan_filters(having), "| FILTER nodes:", operator_names(having).count("FILTER"))
WHERE -> ["(date >= '2026-08-01'::DATE)"] | FILTER nodes: 0 HAVING -> [] | FILTER nodes: 1
EXPLAIN ANALYZE: what it actually read
The plain plan carries estimates. EXPLAIN ANALYZE runs the query and records real row counts per operator, so a scan that produced 6,699 rows against another that produced 15 settles which query is cheaper. Row counts do not depend on the machine, unlike timings, which is why this lab compares rows.
# Real row counts from the scans of two queries that return the same kind of rowswide = "SELECT * FROM bars WHERE symbol = 'KO'"narrow = "SELECT * FROM bars WHERE symbol = 'KO' AND date >= '2026-08-01'"print("wide scan rows:", scan_rows(plan_tree(con, wide, analyze=True)))print("narrow scan rows:", scan_rows(plan_tree(con, narrow, analyze=True)))print("cheaper:", cheaper_query(con, wide, narrow))
wide scan rows: 6699 narrow scan rows: 15 cheaper: b
The optimizer can change a plan between DuckDB versions and as tables grow, and small tables may show a plan you would not see on the full data. Measure on data of the real size, compare row counts first because they are stable, and re-check plans after an upgrade.
What the tasks ask
- 1`operator_names`Walk the plan tree recursively, parents before children, with
name_ofandchildren_of. - 2`scan_filters` and `scan_rows`Look only at scan nodes (
is_scan): collect theirFilters, or add up theirrows_of. - 3`cheaper_query`Analyse both queries with
plan_tree(..., analyze=True), compare withscan_rows, answer "a", "b" or "same".
FILTER node sits above a HASH_GROUP_BY and the scan below it shows no filters. What does that suggest?Lab · what's graded
4 tasks · 100 pts- Walk the plan:
operator_names25 pts - Filters in the scan:
scan_filters25 pts - Rows read:
scan_rows25 pts - Which query reads less:
cheaper_query25 pts