Theoremadvanced
NP-hardness of join ordering
Also called Ibaraki-Kameda theorem.
Finding an optimal join order is NP-hard in general, while for tree queries under an ASI cost function it is polynomial.
The NP-hardness of join ordering says that no polynomial algorithm finds the cheapest order of the joins of a query in general, unless P = NP, while some special cases are easy. Ibaraki and Kameda proved in 1984 that the choice of the best left-deep order without cross products is NP-complete for general query graphs under the cost of nested loop joins. For tree query graphs and costs with the adjacent sequence interchange (ASI) property, the same problem is polynomial, by the IKKBZ algorithm. Cluet and Moerkotte (1995) studied the case in which cross products are allowed. This page states the three results together and proves the hardness of left-deep ordering with cross products under the cost in full, by a reduction from CLIQUE. The join ordering page defines the problem and surveys its algorithms; this page holds the theorem and the proof.
Statement
Instances. An instance of join ordering has relations with estimated cardinalities , and a query graph with an edge for each join predicate, labeled with a selectivity . For a pair with no edge, . All numbers are rationals written in binary, and the size of the instance is the number of bits. The database itself is not part of the input. Under the independence assumption, the join of a set of relations has the estimated size
A left-deep tree is an order . It has a cross product at step if has no edge to . Its cost is the sum of the sizes of the results of its joins:
The sizes are counts of tuples of bag joins, so the problem is the same under set and bag semantics. The complexity measure is time in the size of the instance.
Theorem.
- Hardness with cross products. The following problem is NP-complete: given an instance and a rational , is there a left-deep tree, cross products allowed, with ? So finding a left-deep tree of least is NP-hard.
- Hardness without cross products (Ibaraki and Kameda 1984). For left-deep trees without cross products and the cost of nested loop joins of their model, the decision problem is NP-complete for general query graphs.
- The polynomial case (Ibaraki and Kameda 1984; Krishnamurthy, Boral and Zaniolo 1986). If the query graph is a tree and the cost has the ASI property, as and the nested loop cost have, then a left-deep tree without cross products of least cost can be found in polynomial time: in the first paper and in the second.
In plain words
An optimizer must choose the order in which a query joins its tables. Each order gives the same answer, but the sizes of the partial results differ, and so does the work. The theorem says that no fast method always finds the best order, unless many hard problems all have fast methods.
The proof turns a known hard puzzle into a join order question. The puzzle asks if a network has a group of a given size in which every member knows every other member. Each person becomes a table, and each pair who know each other becomes a join condition that makes the result much smaller. A cheap order must start with a group where everyone knows everyone. So a fast join planner would also solve the puzzle fast.
There is one easy case: when the joins form a tree, a known method finds the best order quickly.
Proof
Part 1, membership in NP. A certificate is an order . Each prefix size is a product of at most cardinalities and at most selectivities, so it has a number of bits polynomial in the size of the instance, and the sum and the comparison with take polynomial time.
Part 1, hardness. The reduction is from CLIQUE, which is NP-complete: given a graph with vertices and an integer with , does have vertices that are pairwise adjacent? This reduction was written for this page; it is not taken from the paper of Cluet and Moerkotte. Write for the number of pairs among vertices.
Construction. For each vertex take a relation with . For each edge of put a join predicate with selectivity between and , and put no predicate between two vertices that are not adjacent. Let
All numbers are powers of 2 with exponents of absolute value , so the instance and have bits and are built in polynomial time.
Cost of an order. For an order of the vertices, let be the number of edges of among . The prefix of length has the size , and . Since , every . The function decreases, because .
If has a clique of vertices, put the vertices of first. For the prefix is a clique, so and . For the prefix holds all the edges of , so , and with this gives . There are such terms, so .
If has no clique of vertices, take any order . Its first vertices are not a clique, so some has . Let be the least such . For , and . At , , so . So
The same first sum is part of . The rest of is . Each term of the sum is at most , because decreases. And , so each of the other terms is at most . The rest of has terms, so it is at most , because . So for every order.
So has a clique of vertices if and only if some left-deep tree has . With membership, this proves part 1.
An example. Take the path and , so , and . Then . The order has and , so its cost is more than . An order that starts with has , which is worse. With the edge added, the graph is a triangle, the order has , and its cost is exactly .
Part 2. This is the theorem of Ibaraki and Kameda; the proof, a reduction for their cost of nested loop joins on a general query graph, is in their paper and is not repeated here.
Part 3. Fix the first relation . In a tree query, an order without cross products is an order in which each relation comes after its parent in the tree hung from , so the problem becomes the ordering of jobs under a tree of precedence constraints. For an ASI cost the cost of an order is for consecutive parts and , and exchanging two adjacent blocks and changes the cost by a positive factor times , with . So an optimal order puts blocks in order of increasing rank, and a child block with a smaller rank than its parent block is glued to the parent. This gives the optimal order for each in polynomial time, and trying all choices of gives the optimum. The IKKBZ page gives the algorithm, an example and the full proof.
Consequences
- Exponential exact search. Unless P = NP, an exact join enumerator needs more than polynomial time on some queries. The dynamic programs used in practice, such as the System R enumeration for left-deep trees and DPccp for bushy trees, are exponential in , and systems switch to heuristics such as greedy operator ordering above a limit on the number of relations.
- The shape of the query graph matters. Part 3 makes tree queries easy for left-deep trees without cross products, and parts 1 and 2 need general graphs. Chain queries are easier still: for bushy trees without cross products, an interval dynamic program like that of matrix chain multiplication runs in time.
- Cross products do not make the problem easier. Allowing cross products makes the space larger and can make the best plan cheaper, as the star query example on the join ordering page shows, but by part 1 the problem stays NP-hard.
- Hardness is about estimates. The theorem is about the estimated sizes, not about a database. An exact algorithm for the estimates still depends on the cost model and on the quality of the estimates.
Tightness
- Number sizes. The reduction of part 1 uses selectivities of bits and estimated sizes far below one tuple. It shows NP-hardness for numbers written in binary. It does not show that the problem stays hard when all numbers are small integers written in unary.
- The conditions of part 3 are needed. Without the tree condition, part 2 gives hardness for the nested loop cost. Without the ASI property, the rank argument fails, because the cost change of an exchange no longer has the sign of a rank difference.
- Other tree shapes. Parts 1 and 2 are about left-deep trees. This page makes no claim about bushy trees, which have their own hardness results in the literature.
- Fixed . For a fixed number of relations, all orders can be tried, so the hardness is a statement about the growth in .
History
Ibaraki and Kameda (1984) studied the nesting order of nested loop joins. They proved the NP-completeness for general query graphs and gave an algorithm for tree queries, based on results on sequencing jobs with precedence constraints. Krishnamurthy, Boral and Zaniolo (1986) stated the algorithm for every cost with the ASI property and made it ; the pair of results gives the name IKKBZ. Cluet and Moerkotte (1995) then studied left-deep trees with cross products, the space that System R searched when it allowed cross products.
References
- Toshihide Ibaraki and Tiko Kameda, On the optimal nesting order for computing N-relational joins, ACM Transactions on Database Systems 9(3), 482 to 502 (1984), the NP-completeness of the optimal nesting order for general query graphs under the cost of nested loop joins, and the O(n^2 log n) algorithm for tree queries
- Optimization of nonrecursive queries, the O(n^2) algorithm for tree queries with cost functions that have the adjacent sequence interchange (ASI) property
- On the complexity of generating optimal left-deep processing trees with cross products, the complexity of optimal left-deep trees when cross products are allowed
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 | W4 complete |
|
Tag BXD0MK Edit this page History Report an error Markdown