Module lessons (2/4)
EXPLAIN and ANALYZE
Have you ever wondered how the database chooses whether to use that nice index you just created? As soon as you hit Enter on a query (say, a long join with a chained group by), the Query Planner computes dozens of strategies on the fly to carry the query out and picks the one believed, in theory, to be fastest.
But as a developer, you need to be able to look inside the dark mind of the Query Planner.
EXPLAIN
To discover how the Planner intends to proceed, just put the magic word EXPLAIN at the start of your query.
EXPLAIN SELECT * FROM orders WHERE status = 'delivered';The result won't be the orders! Instead, you'll see the plan in output (with estimated statistics):
Seq Scan on orders (cost=0.00..25.88 rows=15 width=40)
Filter: (status = 'delivered'::text)This tells us:
- The Planner will use a Seq Scan (brute, sequential read of the entire disk). Bad if there are billions of orders.
- It estimates ~15 rows will be returned, and computes the "cost" in pre-loaded CPU cycles.
EXPLAIN ANALYZE
While EXPLAIN relies on algorithmic guesses (based on global statistics computed in the last few hours over the data), EXPLAIN ANALYZE actually runs the query for real (and waits for the full logical results), comparing the real elapsed time in mathematically absolute terms.
EXPLAIN ANALYZE SELECT * FROM orders WHERE customer_id = 90;Index Scan using idx_orders_cust on orders (cost=0.15..8.13 rows=4 width=40) (actual time=0.012..0.015 rows=4 loops=1)
Index Cond: (customer_id = 90)
Planning Time: 0.081 ms
Execution Time: 0.033 msHooray! Here you learn that the database has decided to apply an Index Scan on our secondary idx_orders_cust index, taking a microscopic execution time of a thousandth of a second, skipping millions of rows on the fly.
Your turn
Write a basic EXPLAIN command to discover how PostgreSQL intends to find products with a current stock below 10 units. (Do not use ANALYZE in this step.)
Show hint
Just prepend the word EXPLAIN at the start of the existing statement.
Solution available after 3 attempts
Getting truthful metrics
We want a real performance diagnosis (timings and cycles), not a mathematically guessed sketch.\nUse the dedicated statement to study, by actually running it, the lookup of ALL 'customers' signed up before the 1st of January 2025 ('signed_up_on' < '2025-01-01').
Show hint
Add the word ANALYZE right after EXPLAIN.
Solution available after 3 attempts