Algorithm Search⌘K

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 Q1,…,QnQ_1, \ldots, Q_n. A set TT of tasks, each with a cost c(t)≥0c(t) \ge 0; a task is a subexpression, such as a selection or a join of some relations. For each query QiQ_i, a set Ai\mathcal{A}_i of alternative plans, where each plan is a set of tasks p⊆Tp \subseteq T.

Question. Choose one plan pi∈Aip_i \in \mathcal{A}_i for each query to minimize the total cost c(p1∪⋯∪pn)=∑t∈p1∪⋯∪pnc(t)c(p_1 \cup \cdots \cup p_n) = \sum_{t \in p_1 \cup \cdots \cup p_n} c(t). The decision form asks whether there is a choice with total cost at most KK.

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 MM of nodes to materialize. A node in MM 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, E=σyear=2024(Orders)⋈CustomersE = \sigma_{\text{year}=2024}(\text{Orders}) \bowtie \text{Customers}, which costs 100 to compute and 10 more to write and read for sharing.

query plan cost alone
Q1Q_1 AA: E⋈NationE \bowtie \text{Nation} 100+20=120100 + 20 = 120
Q1Q_1 BB: σ(Orders)⋈(Customers⋈Nation)\sigma(\text{Orders}) \bowtie (\text{Customers} \bowtie \text{Nation}) 90
Q2Q_2 CC: E⋈LineitemE \bowtie \text{Lineitem} 100+200=300100 + 200 = 300
Q2Q_2 DD: (σ(Orders)⋈Lineitem)⋈Customers(\sigma(\text{Orders}) \bowtie \text{Lineitem}) \bowtie \text{Customers} 280

Optimized one at a time, the queries take BB and DD, for 90+280=37090 + 280 = 370. The pairs A,DA, D and B,CB, C share nothing and cost 400 and 390. The pair A,CA, C computes EE once: 100+10+20+200=330100 + 10 + 20 + 200 = 330. 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 EE cost 30 to compute and 40 to write and read, the pair A,CA, C would cost less if each query computed EE again on its own, because 2⋅30<30+402 \cdot 30 < 30 + 40. 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 MM 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 UU and a family F\mathcal{F} of subsets of UU, make a task tSt_S of cost 1 for each S∈FS \in \mathcal{F} and a query QeQ_e for each e∈Ue \in U, whose plans are the one-task sets {tS}\{t_S\} for the sets S∋eS \ni e.

Proof that the optimal costs are equal. A choice of plans picks, for each element ee, a set that contains ee, so the chosen sets cover UU and their number is the total cost. Conversely, a cover C\mathcal{C} gives a choice of plans: for each ee, pick any set of C\mathcal{C} that contains ee; the union of the tasks has at most ∣C∣|\mathcal{C}| tasks. So the least total cost equals the size of the smallest cover. □\square

The reduction keeps the cost exactly, and the number of queries is ∣U∣|U|. So the hardness of approximating set cover carries over: unless P = NP, no polynomial algorithm approximates MQO in this model within (1−ϵ)ln⁡n(1 - \epsilon) \ln n for nn 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 Hn≤ln⁡n+1H_n \le \ln n + 1.

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 1−1/e1 - 1/e of HRU needs a benefit function with diminishing returns, which MQO costs do not have in general.
  • From single-query optimization. With n=1n = 1, 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

  1. 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
  2. On the multiple-query optimization problem, the complexity of the problem
  3. 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.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a known algorithm
  • a contract.yaml