Problemadvanced
Parametric query optimization
Also called PQO.
Given a query whose cost depends on parameters unknown at compile time, find a set of plans and a rule that picks, for each parameter value, a plan that is optimal for it.
Parametric query optimization asks for the best plan of a query not at one point but over a whole range of values of some parameters that are unknown when the query is compiled. Typical parameters are the selectivity of a predicate with a host variable, the size of a relation, or the memory the query will get. The answer is a set of plans and a rule that, given the actual values at run time, picks a plan that is optimal for them. Yannis E. Ioannidis, Raymond T. Ng, Kyuseok Shim and Timos K. Sellis named the problem in 1992. Goetz Graefe and Karen Ward had given dynamic plans in 1989, where a choose-plan operator keeps the alternatives in the plan. When each plan cost is a linear function of the parameters, the region where a plan is optimal is convex, which this page proves; with piecewise linear costs, this is not true in general.
In plain words
A query can be compiled once and run many times with different values, for example "the orders of customer number ?". For one customer the query returns three rows, and for another it returns a million. The best plan for three rows is not the best for a million.
Normal optimization picks one plan for one guess of the values. Parametric optimization finds, before the query runs, a small set of plans and a map that says which plan is best for which values. At run time, the database looks at the real values, finds them on the map and runs that plan, with no new optimization.
The work is to find all plans that are best somewhere, and the borders between them, without optimizing again for every possible value.
Definition
Input. A query , a parameter space (for example for selectivities), the set of plans for , and a cost function for each plan and parameter value .
Output. A set and a map such that for every . A set with this property is a parametric optimal set of plans. The region of is , and the regions of the plans in cover .
Goal. Make small and the map fast to evaluate, and find them with far fewer calls to the optimizer than there are points in .
Variants
- Linear costs. Each cost has the form . This holds, for example, when is the selectivity of one predicate and the rest of the plan is fixed.
- Piecewise linear or nonlinear costs. Costs change slope where an input stops fitting in memory, and products of selectivities make costs nonlinear in several parameters.
- Approximate PQO. Accept a plan whose cost is at most times the optimum at each . This can make much smaller.
- Dynamic plans. Keep the choice inside the plan as choose-plan operators, decided when the query starts (Graefe and Ward, 1989).
- Discrete parameters. The memory size or the number of available processors can take only a few values; then PQO is a separate optimization for each value, with shared work.
Examples
One parameter, three plans. Let be the selectivity of a predicate with a host variable, and let the optimizer have three plans with linear costs.
| plan | cost |
|---|---|
| : index scan and index nested loop join | |
| : full scan and hash join | |
| : full scan and sort-merge join |
and cost the same at , and and at . and cost the same at , which is inside the region of , so it is not a border. The answer is with the regions , and . A system that compiled the query for and kept would pay 36,100 at , where costs 13,180.
Piecewise linear costs give a region that is not convex. Plan is an index nested loop join with cost . Plan is a hash join whose build input fits in memory up to and spills to disk after that, with cost for and for . Both costs grow with . is cheaper when , that is , and again when , that is . At , costs 3000 and costs 7000. So the region of is , which is not convex. This corrects the claim that piecewise linear costs give convex regions; the claim holds for linear costs, below.
Known algorithms
- Optimize at sample points. Run the normal optimizer at the points of a grid over and keep the plans found. This is simple, but it can miss a plan whose region falls between the grid points, so it gives no guarantee.
- Randomized search for many values at once. Ioannidis et al. (1992) used randomized search methods, such as iterative improvement and simulated annealing, and shared the work between the searches for different parameter values.
- Choose-plan operators. Graefe and Ward (1989) keep two subplans in the plan when their costs cannot be compared at compile time, under a choose-plan operator that evaluates the costs when the query starts. Cole and Graefe (1994) optimized such plans with costs given as intervals.
- Linear costs, one parameter. If the optimizer can return all plans that are optimal at some , the regions are the pieces of the lower envelope of the lines . Sort the lines by slope and keep the lines of the envelope with a stack, as in half-plane intersection; for lines this costs time.
- Linear costs, vertex checks. By the convexity theorem below, if the same plan is optimal at all vertices of a convex polytope , it is optimal on all of . So an algorithm can optimize at the vertices of , stop where they agree, and split where they do not. Later work by Ganguly (1998) and by Hulgeri and Sudarshan (2002) built PQO algorithms on this idea for linear and piecewise linear costs.
Bounds
Linear costs give convex regions (proved). If is convex and every cost is linear, , then every region is a convex set, and a polyhedron if is one.
Proof. For each other plan , the set is , a closed half-space (or all of , or the empty set). So is the intersection of with finitely many half-spaces, and an intersection of convex sets is convex.
Two consequences follow. Each plan of has one connected region, so in one dimension the range splits into at most intervals. And the optimal cost is the minimum of linear functions, so it is concave and piecewise linear.
Hardness. For a single parameter value, PQO is normal query optimization, which contains join ordering, NP-hard in general. So PQO is at least as hard. The size of depends on the query and the cost model; for nonlinear costs, a plan can be optimal on many separate pieces of , as the example shows.
Reductions
- From query optimization. Normal optimization is PQO with .
- To a lower envelope. With linear costs over a known set of candidate plans, PQO is the computation of the lower envelope of hyperplanes in , which is the upper boundary of the intersection of the half-spaces below them.
- To parametric linear programming. When the costs come from a linear program whose objective depends on , the regions of the optimal bases are the regions of parametric linear programming, and they are convex for the same reason.
Applications
- Prepared statements and stored procedures. The query is compiled once with host variables, and the plan is picked per call, so each call gets a good plan without a full optimization.
- Unknown memory or load. The memory a query gets and the number of processors are known only when the query starts.
- Plan caching. A database can cache the set and the map for a frequent query, in place of one plan that is bad for some values.
- A step toward run-time adaptivity. When the true values are still not known at start-up, mid-query re-optimization checks them during the run, and eddies change the plan for each tuple.
References
- Parametric query optimization, the statement of the problem, with parameters such as the size of the buffer memory, and randomized search methods that find plans for many parameter values at once
- Yannis E. Ioannidis, Raymond T. Ng, Kyuseok Shim and Timos K. Sellis, Parametric query optimization, The VLDB Journal 6(2) (1997), 132 to 151, the journal version of the 1992 paper
- Goetz Graefe and Karen Ward, Dynamic query evaluation plans, Proceedings of ACM SIGMOD 1989, 358 to 366, the choose-plan operator, which keeps several subplans whose costs cannot be compared at compile time and picks one when the query starts
- Richard L. Cole and Goetz Graefe, Optimization of dynamic query evaluation plans, Proceedings of ACM SIGMOD 1994, the optimization of plans with choose-plan operators when costs are intervals, in the Volcano optimizer generator
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 3338AZ Edit this page History Report an error Markdown