Summary - Optimizer
- SQL course
- Expert
- Lesson 18/45
- 5 min
Refresh statistics with ANALYZE.
Summary - Optimizer
The optimizer: EXPLAIN builds plans, statistics and histogram set the cost, a join strategy is picked, the plan cache speeds repeats. The right plan gives the right speed.
-- EXPLAIN -> ANALYZE -> plan cache
-- nested loop / hash join / merge joinThe optimizer weaves four parts.
Practice Summary - Optimizer
Summary task: compare the join plan of orders and customers with EXPLAIN ANALYZE.
Starter code
-- summary optimizer
EXPLAINSample solution
EXPLAIN ANALYZE SELECT * FROM orders o JOIN customers c ON o.customer_id = c.id;Expected output
Hash Join on orders x customers (actual time=0.020..0.035 rows=5)
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 See plan and time
EXPLAIN ____ SELECT * FROM orders;
EXPLAIN ANALYZE SELECT * FROM orders;
Explanation: ANALYZE is the right answer.
2 Which join suits a large equality join?
- Cross join
- Nested loop
- Seq scan
- A hash join Correct answer
Explanation: A hash join suits a large equality join.
3 Summary - Optimizer: match the terms
| Term | Meaning |
|---|---|
EXPLAIN |
plan |
ANALYZE |
stats |
hash join |
hash |
plan cache |
cache |