Comparing plans

functional

Capture before and after a change and read what got better, what got worse and what changed.

Tuning is a loop: capture a plan, change something (an index, a rewrite, fresh statistics), capture again, and check that it actually helped. Comparing two plans of the same query turns that last step into a list of sentences instead of two diagrams to squint at.

SQLly
SQLly plan comparison listing better and changed itemsSQLly plan comparison listing better and changed items
Comparing two captures of a lookup query, before and after two indexes: the new plan ran 190 times faster, the cost estimate dropped, both tables are now read through indexes, and the join changed from a hash join to a nested loop.

Comparing in the app

  1. Capture or paste the first plan. Every capture in a query tab is kept, up to the last eight.
  2. Make your change and capture again (the Re-capture button repeats the same kind of capture), or paste the second plan.
  3. Press Compare on the plan pane. It appears once the tab holds two captures.
  4. Pick the Baseline and the Comparison capture from their buttons if you want something other than the last two. Show Plan goes back to the plan.

Plans over 2,000 operators are not kept for comparison; the pane says so when it drops one. Captures of different query text are not aligned, since matching their operators would be guesswork.

What the comparison says

The headline is the overall result (“The new plan ran in 0.12 ms, 190 times faster than the baseline's 22.8 ms”), followed by one sentence per change, each marked Better, Worse or Changed:

Kind of changeExample
Time and cost“The optimizer's cost estimate went from 7865 to 44.04, 179 times lower.”
Bottleneck“The slowest step was Seq Scan on customers c with 41% of the time; now it is Bitmap Heap Scan on orders o with 51%.”
Join order and join algorithm“The join of customers c and orders o changed from Hash Join to Nested Loop.”
Access path and index choice“customers c is now searched through the index customers_email_lower_idx. The baseline scanned it.”
Lookups, tables, sortsA key lookup that disappeared, a table the plan no longer reads, a sort that is gone.
Parallelism and spills“The new plan does not run in parallel. The baseline did.” A sort that no longer spills.
Estimates and rows“The row estimate for customers c was off by a factor of 250 in the baseline and is close now.”
  • A table the new plan reaches through an index (a PostgreSQL Bitmap Heap Scan, an Oracle TABLE ACCESS BY INDEX ROWID) is reported as a change of access path, not as a table the plan stopped reading.
  • Comparing an estimated plan with an executed one says which is which and that only costs, row estimates and plan shape can be compared.
  • When nothing material moved: “No material operator changes were detected.”

Comparing elsewhere

  • Online viewer: paste the first plan, then the second into the Plan after a change tab. A What changed between the two plans section appears above the findings, with a switch between the first plan and the one after the change. See The free online viewer.
  • Notebooks: a Plan cell can compare its query with another query cell (a rewrite next to the original) or with the plan from the cell's previous run, using the same sentences in a compact form. See Profiles, tests, diffs and plans.