Conceptcore
Left-deep and bushy join trees
Also called left-deep join tree, bushy join tree, zig-zag join tree.
The shapes of join plans: a left-deep tree has a base relation as each inner input, a bushy tree may join two intermediate results.
A join tree is a binary tree whose leaves are the relations of a query and whose inner nodes are joins. Its shape decides which intermediate results exist and how they flow between the operators. In a left-deep tree the right input of every join is a base relation, so the tree is a sequence: join the first two relations, then join the result with the third, and so on. In a bushy tree both inputs of a join can be results of other joins. This page defines the shapes, counts them and says when each one is used. The join ordering problem is the choice of the cheapest tree in a given space of shapes, and the System R join enumeration is the classic search of the left-deep space.
In plain words
You must join four tables. One way is a line: join two tables, then join the result with a third table, then with the fourth. At every step one input is a stored table. That is a left-deep plan.
Another way is to work on two parts at the same time: join the first two tables, join the other two tables, and then join the two results. That is a bushy plan. Bushy plans can keep every partial result small, and the two parts can run in parallel. But there are many more of them, so the search for the best one takes longer. Left-deep plans are fewer, and each step can use an index on the stored table.
The idea
Inputs of a join. This page calls the left input of a join its outer input and the right input its inner input. For a nested loop join the outer input is read once and the inner input once for each outer tuple, so a base relation with an index on the join column is a good inner input, as in the index nested-loop join. For a hash join, systems and papers differ in which input they build the hash table on, so the words left-deep and right-deep can change places from one source to the next. Read each source with its own convention.
Shapes. For relations :
- A left-deep tree has the form . It is fixed by the order of its leaves.
- A right-deep tree is the mirror image: the left input of every join is a base relation.
- A bushy tree is any binary tree with the relations as leaves, each leaf once.
- Between them is a third shape, in which every join has at least one base relation as an input, on either side. The query plan page counts it too.
Every left-deep and every right-deep tree is bushy. All of them compute the same result, because the natural join is commutative and associative under both set and bag semantics; the equivalence rules prove this. Only the intermediate results, and so the costs, differ.
Counts with cross products. A left-deep tree is an order of the leaves, so there are of them. A bushy tree is a binary tree shape with leaves and ordered children, labeled with the relations. There are shapes, the Catalan number, because a shape is a way to put brackets into a product of factors. Each shape has different labelings, so there are
bushy trees. For these are 3 628 800 left-deep trees and 17 643 225 600 bushy trees.
Counts without cross products. A join of two inputs that no join predicate connects is a cross product. Most optimizers skip trees with cross products, and then the count depends on the query graph, with one node for each relation and an edge for each join predicate. A left-deep tree has no cross product exactly when each relation after the first has an edge to an earlier one.
- Chain : each prefix of the order is an interval of the chain, and after the first relation each step extends the interval to the left or to the right, so there are left-deep trees. For bushy trees, every subtree covers an interval, and the root splits the interval in places, with two orders of the inputs. With and , the Catalan recurrence gives .
- Star with center and other relations: the center must be first or second, which gives left-deep trees.
- Clique: every order is allowed, so all left-deep trees and all bushy trees have no cross product.
Examples
Four relations. For , the tree is left-deep, is right-deep, and is bushy and neither of the two. With cross products there are left-deep trees and bushy trees. For the chain without cross products there are left-deep trees and bushy trees.
A bushy tree that is cheaper. Take the chain with , , and selectivities on the edges and and on . Then , , , and the result has 100 tuples. Take as the cost the sum of the sizes of the results of all joins.
| tree | results of the joins | cost |
|---|---|---|
| 100, 1000, 100 | 1200 | |
| 10 000, 1000, 100 | 11 100 | |
| 100, 10 000, 100 | 10 200 | |
| 100, 100, 100 | 300 |
Every left-deep tree must at some point hold three of the four relations, and every set of three costs at least 1000, or 10 000 with a cross product. The bushy tree joins the two small pairs first and never builds a set of three, so it costs a quarter of the best left-deep tree.
A pipeline. In the left-deep tree with index nested loop joins, each tuple of goes through all three joins before the next tuple is read, and no intermediate result is stored. The inner inputs , and are read through their indexes. System R searched only left-deep plans, and this is one of them.
Where it is used
- System R and its successors search only left-deep trees, by dynamic programming over subsets of relations (System R join enumeration). The space is smaller, and every inner input is a stored relation that can use an index.
- Polynomial cases. The IKKBZ algorithm finds the best left-deep tree without cross products for a tree query graph and a cost with the ASI property.
- Bushy search. DPccp and DPhyp find the best bushy tree without cross products by dynamic programming over connected subgraphs. Greedy operator ordering builds a bushy tree by joining the pair with the smallest result first. Rule-based optimizers such as Cascades reach bushy trees by commutativity and associativity.
- Execution. A left-deep tree of nested loop joins runs as one pipeline. A tree of hash joins with the build inputs all on one side can build all hash tables first and then stream one input through all of them, which needs memory for all tables at the same time. Bushy trees allow two subtrees to run in parallel.
References
- P. Griffiths Selinger, M. M. Astrahan, D. D. Chamberlin, R. A. Lorie and T. G. Price, Access path selection in a relational database management system, Proceedings of ACM SIGMOD 1979, 23 to 34, Section 5, Access path selection for joins (join orders in which each new relation is joined to the result of the relations before it)
- Analysis of two existing and one new dynamic programming algorithm for the generation of optimal bushy join trees without cross products, the search space of bushy trees without cross products and its size for chain, star and clique queries
Where this leads
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 F52X6N Edit this page History Report an error Markdown