EXPLAIN & Query Plans
Read real EXPLAIN output: scan types, cost, rows, Filter and Index Cond, and what EXPLAIN ANALYZE adds, so you can tell why a query is slow.
EXPLAIN shows the plan before the work
SQL says what you want, not how to get it. The planner works out a plan, a tree of steps, and EXPLAIN prints it. EXPLAIN SELECT ... does not run the query. It only shows the plan the planner chose.
Each line is a node. Scan nodes at the bottom read tables: Seq Scan reads every row, Index Scan walks an index, and Bitmap Heap Scan collects row locations from an index first and then reads those pages. Nodes above them, such as Sort, work on the rows passed up.
Guessing is slow, reading the plan is quick
A slow query is slow for a reason you can see in the plan: a Seq Scan over a huge table, a Filter that throws most rows away, a Sort of everything. Reading the plan tells you whether an index would help, before you build one.
Read cost, rows and width
Seq Scan on tenk1 (cost=0.00..445.00 rows=10000 width=244)cost=0.00..445.00 is startup..total. Startup is the work before the first row can come out, total is the work to finish. The units are arbitrary: by default fetching one disk page in sequence costs 1.0, and the planner chooses the plan with the lowest total. rows is the planner's estimate of the rows this node passes up, and width is the average row size in bytes.
Where does 445 come from? The manual's own sum: 345 pages x 1.0, plus 10000 rows x 0.01 (the default cost of handling a row) = 445.
Now compare the manual's plans for the same table. WHERE unique1 < 7000 stays a Seq Scan with a Filter line, cost 470, because most rows match. WHERE unique1 < 100 switches to a bitmap scan using the index, cost about 225. WHERE unique1 = 42 becomes a plain Index Scan, cost about 8. The more selective the query, the more the index pays off.
Read six real plans
Go through the queries from the first to the last, and use the Decode chips to highlight each part of the plan. Finish with EXPLAIN ANALYZE.
What PostgreSQL printed
cost. cost=startup..total. Startup is the work before the first row can come out, and total is the work to finish. The units are arbitrary: by default fetching one disk page in sequence costs 1.0, and the planner picks the plan with the lowest total.
345 pages x 1.0 + 10000 rows x 0.01 = 445
Every row is wanted, so reading the whole table is the right plan. The cost 445 is the manual's own sum: 345 pages x 1.0 plus 10000 rows x 0.01.
Check yourself
Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.
Remember this
- EXPLAIN shows the plan without running the query. EXPLAIN ANALYZE runs it and adds actual times and row counts.
- cost=startup..total is in arbitrary units (one sequential page fetch is 1.0). rows is an estimate, and width is bytes per row.
- A Filter means rows were read and then discarded. An Index Cond means the index did the narrowing.
- Wrap EXPLAIN ANALYZE of an UPDATE or DELETE in BEGIN and ROLLBACK, and run ANALYZE when estimates look wrong.
Done with this concept?
Mark it complete to track your progress. No login needed.