Techniqueadvanced
Mid-query re-optimization
Also called Kabra-DeWitt re-optimization, progressive optimization.
Count the rows at checkpoints while a plan runs, and optimize the rest of the query again when a count is far from its estimate, reusing the results already computed.
Mid-query re-optimization checks the estimates of a plan while the plan runs, and if they are badly wrong, optimizes the rest of the query again. A classic optimizer picks a plan once, from cardinality estimates whose errors can grow fast with the number of joins (see estimation error propagation). Navin Kabra and David J. DeWitt (1998) added statistics collectors to the plan: at a point where an intermediate result is fully materialized, the system compares the observed size with the estimate, and if a new plan for the rest is expected to be much faster, it re-optimizes, using the materialized result as a new input with exact statistics. Markl et al. (2004) called their form progressive optimization (POP): the optimizer computes, for each checkpoint, a validity range of sizes for which the plan stays the best, and a CHECK operator triggers re-optimization when the observed size leaves the range. The decisions of both are heuristics. Eddies go further and change the order for each tuple.
In plain words
Before a query runs, the database guesses how many rows each step will make, and picks a plan for those numbers. If a guess is very wrong, the plan can be very slow.
Mid-query re-optimization puts check points into the plan. At a check point, the database counts the rows that were really produced. If the count is close to the guess, the plan goes on. If it is far off, the database stops, plans the rest of the query again with the true count, and goes on with the new plan. The work done before the check point is kept and used by the new plan.
The best place for a check point is where a step has finished all its rows anyway, such as after a sort. There, stopping and changing the plan costs little.
When it applies
- Long queries with uncertain estimates. Many joins, correlated predicates, parameter values unknown at compile time, or missing statistics. A query that runs for minutes can afford a second optimization.
- Plans with materialization points. A sort, the build side of a hash join, or a temporary table finishes its input before the next step uses it. There, the exact size is known and no output has been sent yet. See pipelining and materialization.
- An optimizer that can take a partial result as input. The new plan must use the materialized result as a table, with its exact size and maybe a histogram.
- It does not apply to short queries, where re-optimization costs more than it saves, or to fully pipelined plans that have already sent rows to the user, unless the system can avoid sending duplicates.
The template
- Optimize and annotate. Choose a plan as usual. For each candidate checkpoint, record the estimated size and, for POP, a validity range of sizes for which the plan is still the best among the alternatives the optimizer compared there.
- Place checkpoints. Put a statistics collector or a CHECK operator above the subplans whose estimates are uncertain, preferably at materialization points.
- Observe. At run time, count the rows that reach the checkpoint, and collect other statistics if they are cheap, such as the number of distinct values or a histogram.
- Decide. POP re-optimizes when . Kabra and DeWitt re-optimize when the estimated gain of a new plan for the rest is large compared with the remaining cost and with the time to optimize again. Both tests use thresholds, which are heuristics.
- Re-optimize the rest. Treat each materialized intermediate result as a base table with known statistics, like a materialized view, and optimize the remaining query. The optimizer can still choose plans that do not use these results.
- Bound the loop. Limit the number of re-optimizations per query, so that a query does not keep changing its plan.
Validity ranges are intervals (proved, for linear costs). At a checkpoint with size , let be the chosen subplan above it and the alternatives the optimizer compared with it. If each cost is an affine function of , , then the set of where is at least as cheap as every alternative is an interval.
Proof. For each , the set is . A linear inequality in one variable defines a half-line, the whole line or the empty set, and each of these meets in an interval. The set where is best is the intersection of these intervals, which is an interval.
With costs that are not affine in , for example for a sort, the set can still be an interval when the differences of the costs are monotone in , but this is not true in general. Also, the range says only that beats the alternatives that were compared at that point; it does not say that the whole plan is optimal, so it is a heuristic test.
Classic examples
A validity range. A plan joins the result of a filter on Orders with Customers. The optimizer estimates rows and picks an index nested loop join with a cost of page reads (three reads per index probe). The alternative is a hash join with a cost of (a scan of Customers plus a small cost per row). The nested loop join is cheaper when , that is when , so the validity range is . At , the costs are 3000 and 20,010. If the filter really returns 50,000 rows, the CHECK operator above it sees that is out of range. The nested loop join would cost 150,000 reads, and the hash join 20,500, so re-optimization saves about 129,500 reads, minus the cost of optimizing again and of writing the filter result to a temporary table.
Kabra and DeWitt. In their system, statistics collectors were placed where the optimizer expected the largest errors and where collecting was cheap, and a re-optimization was done only if it was expected to improve the rest of the query by a large factor. They also used the new statistics to give the remaining operators better memory budgets. Their measurements showed large gains for queries with bad initial estimates and small overhead otherwise; these are measurements of their prototype.
POP in DB2. Markl et al. placed CHECK operators of several kinds: lazy checks at existing materialization points, and eager checks that materialize or buffer rows only to be able to check. They measured POP on TPC-H queries in a DB2 prototype, with large gains for queries whose estimates were wrong and a small overhead for the others.
Variations
- Lazy and eager checks. A lazy check waits for a natural materialization point. An eager check is placed earlier, for example below a pipelined join, and buffers rows until it can decide; if the plan has already sent rows to the user, the new plan must remove the rows already sent.
- Choice among precomputed plans. Instead of optimizing again, prepare several plans at compile time and pick one at run time, as in the choose-plan operator of dynamic plans and in parametric query optimization.
- Robust plans. Choose at compile time a plan whose cost grows slowly when the estimates are wrong, so that fewer checks fail. Later work computed bounding boxes around estimates for this.
- Per-tuple adaptivity. Eddies remove the fixed plan altogether and route each tuple, which needs no checkpoints but costs a decision per tuple.
- Learning across queries. The observed sizes can be fed back to the statistics, so the next query starts with better estimates, and learned optimizers use them as training data.
Pitfalls
- Thrashing. If the new estimates are also wrong, the query can re-optimize many times. Limit the number of rounds, and widen the thresholds after each one.
- Duplicate rows. A pipelined plan may have sent some result rows before a check fails. The new plan must not send them again.
- Losing the work done. A re-optimization that ignores the materialized results wastes the work before the checkpoint. Make the intermediate results available to the new plan.
- Checking only one number. The row count can be right while the distribution is wrong, for example when a join value is skewed. Collect distinct counts or histograms where they matter.
- Too many checkpoints. Each check costs time and some prevent pipelining. Place them where the estimates are most uncertain.
Exercises
- In the validity range example, find the range if the hash join cost is . (Answer: gives .)
- Give three cost functions of , for one chosen plan and two alternatives, for which the set of where the chosen plan is best is not an interval. (Hint: costs that are not affine.)
- A query has re-optimized once at a hash join build and the new plan has a sort. Where can the next check go, and what does it cost?
- Explain why an eager check below a pipelined join must either buffer its input or track which result rows were sent, and compare the costs of the two.
References
- Navin Kabra and David J. DeWitt, Efficient mid-query re-optimization of sub-optimal query execution plans, Proceedings of ACM SIGMOD 1998, 106 to 117, statistics collectors in the plan, the decision to re-optimize at a materialization point, and the re-optimization of the rest of the plan with the intermediate result as a new input
- Robust query processing through progressive optimization, CHECK operators, validity ranges computed by sensitivity analysis during optimization, and re-optimization that reuses intermediate results, in a DB2 prototype measured on TPC-H
- Viktor Leis, Andrey Gubichev, Atanas Mirchev, Peter Boncz, Alfons Kemper and Thomas Neumann, How good are query optimizers, really?, Proceedings of the VLDB Endowment 9(3) (2015), 204 to 215, Section 3 (the size of cardinality errors in real systems, the reason to check estimates at run time)
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag ZYAS1Y Edit this page History Report an error Markdown