CodeBlanks Logo Icon CodeBlanks

Summary - Optimizer

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 join

The optimizer weaves four parts.

Practice Summary - Optimizer

Summary task: compare the join plan of orders and customers with EXPLAIN ANALYZE.

Starter code

query.sql
-- summary optimizer
EXPLAIN

Sample solution

query.sql
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

Question
EXPLAIN ____ SELECT * FROM orders;
Answer
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

All lessons in Expert