Problemadvanced
Multiple-query optimization
Also called multi-query optimization, MQO.
Optimize a batch of queries together, so that a subexpression common to several queries is computed once and shared. The problem is NP-hard; practical methods are heuristics.
Multiple-query optimization takes a batch of queries that run together and looks for the plans with the least total cost, where a result that several plans need is computed once and shared. A normal query optimizer handles one query at a time, so it cannot see that two queries join the same filtered tables. Timos K. Sellis stated the problem in 1988 as the choice of one plan for each query, where the cost of a set of plans counts each shared task once. The problem is NP-hard: this page proves it by a reduction from set cover, and Sellis and Ghosh (1990) studied its complexity. Prasan Roy, S. Seshadri, S. Sudarshan and Siddhesh Bhobe (2000) put the plans of all queries in one AND-OR DAG, as in the Cascades and Volcano optimizers, and chose the shared nodes greedily. Their methods are heuristics with measured gains.
In plain words
Suppose a report runs ten queries at night, and several of them join this year's orders with the customers. Each query alone would do this join again. It is cheaper to do the join once, keep the result, and let every query read it.
But the best plan for one query alone may not create a result that the others can use. Sometimes each query must take a plan that is a little worse for itself, so that all of them can share one piece of work. Multiple-query optimization looks for the choice of plans for all queries that costs the least in total.
Keeping a shared result also costs something, because it must be written and read again. So sharing pays only when the work saved is larger than this cost.
Definition
Input (the model of Sellis). Queries . A set of tasks, each with a cost ; a task is a subexpression, such as a selection or a join of some relations. For each query , a set of alternative plans, where each plan is a set of tasks .
Question. Choose one plan for each query to minimize the total cost . The decision form asks whether there is a choice with total cost at most .
The DAG model (Roy et al.). The plans of all queries form one AND-OR DAG: an OR node is a subexpression with its alternative ways to compute it, and an AND node is an operator with its inputs. The answer chooses a plan for each query and a set of nodes to materialize. A node in costs its computation plus the cost to write it once, and each use of it costs a read. The first model is the case where sharing is free.
Variants
- Batch MQO. All queries are known at once, as in a nightly report.
- Shared pipelines. Share a scan or an operator between queries that run at the same time, without writing the shared result, which saves the write cost but makes the queries wait for each other.
- Continuous queries. In a stream system, many standing queries run for a long time, and new ones are added to a shared plan as they arrive.
- View maintenance. Several materialized views are updated from the same changes, and the update expressions share subexpressions.
- Queries with nested subqueries. A subquery that is run many times with different values is a batch of similar queries.
Examples
Sharing beats the best plan of each query. Two queries both need the year's orders joined with the customers, , which costs 100 to compute and 10 more to write and read for sharing.
| query | plan | cost alone |
|---|---|---|
| : | ||
| : | 90 | |
| : | ||
| : | 280 |
Optimized one at a time, the queries take and , for . The pairs and share nothing and cost 400 and 390. The pair computes once: . So the best batch plan uses a plan that is not the best alone for each of the two queries.
Sharing that does not pay. If cost 30 to compute and 40 to write and read, the pair would cost less if each query computed again on its own, because . A DAG optimizer must compare the two.
Known algorithms
- One query at a time, then share. Optimize each query alone and then share the subexpressions that the chosen plans happen to have in common, as common subexpression elimination does in a compiler. This is fast, but it misses the plans in the example. Roy et al. call their version of this Volcano-SH.
- Exact search. Sellis (1988) gave exact methods that search the combinations of plans, with A* search guided by a lower bound on the cost of the plans still to be chosen, and faster heuristic methods. Exact search takes time exponential in the number of queries in the worst case.
- Greedy materialization. Roy et al. (2000) start with no shared nodes and repeatedly add to the node of the DAG that lowers the total cost most, until no node helps. This is a greedy algorithm with no proved bound in general; they made it fast with incremental cost updates and report good plans at a small cost in optimization time.
- Volcano-RU. Optimize the queries in sequence, and let each one reuse the nodes that earlier queries in the sequence chose to keep.
Bounds
MQO is NP-hard (proved). The decision form is in NP, because a choice of plans can be checked in polynomial time. For hardness, reduce from set cover. Given a universe and a family of subsets of , make a task of cost 1 for each and a query for each , whose plans are the one-task sets for the sets .
Proof that the optimal costs are equal. A choice of plans picks, for each element , a set that contains , so the chosen sets cover and their number is the total cost. Conversely, a cover gives a choice of plans: for each , pick any set of that contains ; the union of the tasks has at most tasks. So the least total cost equals the size of the smallest cover.
The reduction keeps the cost exactly, and the number of queries is . So the hardness of approximating set cover carries over: unless P = NP, no polynomial algorithm approximates MQO in this model within for queries. When each element is in two sets, the same construction is vertex cover.
Easy cases. With no task shared by two queries, the total cost is a sum of separate terms, and optimizing each query alone is optimal. If every plan has one task, as in the reduction, the problem is set cover, and the greedy set cover algorithm is within a factor .
Reductions
- From set cover and vertex cover. As above.
- To view selection. Choosing nodes to materialize for a batch is close to the view selection problem, in which the views are kept for a whole workload and limited by space; the greedy algorithm of Roy et al. has the same form as the HRU greedy algorithm, but the bound of HRU needs a benefit function with diminishing returns, which MQO costs do not have in general.
- From single-query optimization. With , MQO is normal query optimization.
Applications
- Batch reports and dashboards. Many queries over the same fact tables run at the same time.
- Stream systems. Shared plans for many standing queries.
- View maintenance and data loading. The expressions that update several views share their inputs.
- Query rewriting inside one query. A query with a common table expression or a repeated subquery is a small batch, and the same choice of what to materialize applies.
References
- Multiple-query optimization, the problem of choosing one plan for each query of a batch so that the tasks the plans have in common are done once, and exact and heuristic search methods for it
- On the multiple-query optimization problem, the complexity of the problem
- Efficient and extensible algorithms for multi query optimization, the AND-OR DAG of the plans of all queries, the cost of materializing and reusing a shared node, and the heuristics Volcano-SH, Volcano-RU and a greedy choice of nodes to materialize, with measurements
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 MJSBSF Edit this page History Report an error Markdown