Cost model
- SQL course
- Expert
- Lesson 15/45
- 5 min
Learn cost-based planning.
Cost model
The cost model computes the CPU and IO cost of each plan step as a number. The plan with the lowest total cost wins.
-- EXPLAIN çıktısı:
-- Seq Scan on orders (cost=0.00..34.00 rows=1000)Cost is the sum of CPU and IO.
Cost model — details
Cost is an estimated resource use, not real time. EXPLAIN ANALYZE shows both estimate and actual time; it reveals the gap between plan and reality.
Practice Cost model
Run EXPLAIN ANALYZE on all rows of orders to compare estimate and actual time.
Starter code
-- cost and time
EXPLAINSample solution
EXPLAIN ANALYZE SELECT * FROM orders;Expected output
Seq Scan on orders (actual time=0.012..0.018 rows=5)
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Cost model: match the terms
| Term | Meaning |
|---|---|
cost |
cost |
CPU |
CPU |
IO |
IO |
plan |
plan |
2 See actual time
EXPLAIN ____ SELECT * FROM orders;
EXPLAIN ANALYZE SELECT * FROM orders;
Explanation: ANALYZE is the right answer.
3 What does cost measure?
- CPU ve IO Correct answer
- An hour
- Money
- The number of rows
Explanation: Cost measures CPU and IO usage.