Skip to content
Ameen Abozaid
Planned lessonMID-013-L08

Demo: a bad estimate and how statistics fix it

  • Lesson 8 of 8
  • Mid-level
On This Lesson 3 topics

This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.

In this lesson

diagnose a bad plan caused by a wrong row estimate.

Draft outline details

Step by step

  1. Create correlated columns (country, currency) and a query filtering on both; the planner estimates 5 rows, actual is 300 000.
  2. Show the nested loop chosen because of the underestimate and its runtime.
  3. Run CREATE STATISTICS s (dependencies) ON country, currency FROM orders; ANALYZE orders;.
  4. Planned mistake: forget to run ANALYZE after creating the statistics; the estimate does not change. Fix: analyze.
  5. Re-run: estimate close to actual, plan switches to a hash join, time drops.

Check your understanding

Why was the estimate wrong?

Show answer

The planner assumed the two columns were independent.

Practice

Apply this lesson with the module's lab, exercises and quiz.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Type to search levels, phases, modules and lessons.