Sub topic 1
What EXPLAIN Shows and Why It Matters
Prerequisites
You should be able to write SELECT queries with WHERE, JOIN, GROUP BY, and ORDER BY. You should also know that an index is a database structure that can help find rows without reading a whole table. The examples use PostgreSQL and assume you can run SQL in psql or another SQL client.
Why Query Plans Matter
A SQL statement describes the result you want. It does not describe the exact steps the database must take. PostgreSQL's planner chooses those steps. It considers available indexes, table statistics, join methods, sorting work, and expected result size.
The chosen steps are called a query plan. A plan is a tree of operations, called nodes. One node might read rows from a table. Another might join two inputs. A parent node receives rows from its child nodes and performs the next operation.
EXPLAIN displays this plan without executing the query:
EXPLAIN
SELECT *
FROM orders
WHERE customer_id = 42;A typical result might look like this:
Index Scan using orders_customer_id_idx on orders
(cost=0.42..8.44 rows=3 width=64)
Index Cond: (customer_id = 42)This output says that PostgreSQL expects to use the orders_customer_id_idx index. The scan is a child operation that produces rows for the query's result.
The Main Information in a Plan
The text in parentheses contains estimates:
cost=0.42..8.44is the estimated startup cost and total cost. These are planner units, not milliseconds.rows=3is the estimated number of rows this node will produce.width=64is the estimated average size of each output row in bytes.
The cost helps PostgreSQL compare possible plans. A lower cost usually wins, but cost is not a promise about elapsed time. Hardware, cache state, configuration, and data distribution all affect actual performance.
The plan also shows the operation type. For example, Index Scan means PostgreSQL expects to find matching table rows through an index. A Seq Scan means it expects to read the table from beginning to end. A sequential scan can be the right choice when a query needs a large fraction of a table.
Read the plan from the bottom upward. The lowest nodes obtain or create input rows. Higher nodes consume those rows and produce the final result.
Why EXPLAIN Is Useful
A query can be logically correct but operationally slow. EXPLAIN helps you see what PostgreSQL expects to do before spending time changing the query or schema.
Use it to ask focused questions:
- Is PostgreSQL reading a whole table when a selective index could help?
- Does the planner expect a small or large result?
- Which operation feeds the next operation?
- Does the plan match the way you think the query works?
EXPLAIN provides estimates only. To compare those estimates with real execution, use EXPLAIN ANALYZE. Because EXPLAIN ANALYZE runs the query, use extra care with statements that modify data. A later section shows how to run those statements inside a transaction and roll the changes back.
Exercise
What does EXPLAIN do, and what do rows=3 and width=64 describe?
Show answer
EXPLAIN displays the planner's expected operations without running the query. The values estimate the number of output rows and the average size of each row in bytes.