optimization
optimization
Goal:
Declarative SQL query Imperative query execution pla
sname
ELECT S.sname
rating > 5
ROM Reserves R, Sailors S bid=100
Reserves Sailors
Plan: Tree of R.A. ops, with choice of alg for each op.
sname
bid=100 rating > 5
sid=sid
D D
• As the number of joins increases, the number of alternative plans grows rapidly; we need to
restrict the search space. C C
• System-R: consider only left-deep join trees.
A– Left-deep trees B D A
allow usCto generate all fully pipelined B
plans:Intermediate B
A not written
results
to temporary files.
• Not all left-deep trees are fully pipelined (e.g., SM join).
Enumeration of Left-Deep Plans
• Enumerated using N passes (if N relations joined):
– Pass 1: Find best 1-relation plan for each relation.
– Pass 2: Find best way to join result of each 1-relation plan
(as outer) to another relation. (All 2-relation plans.)
– Pass N: Find best way to join result of a (N-1)-relation
plan (as outer) to the N’th relation. (All N-relation plans.)
• For each subset of relations, retain only:
– Cheapest plan overall, plus
– Cheapest plan for each interesting order of the tuples.
Enumeration of Plans (Contd.)
• ORDER BY, GROUP BY, aggregates etc. handled as a
final step, using either an `interestingly ordered’ plan or
an additional sorting operator.
• An N-1 way plan is not combined with an additional
relation unless there is a join condition between them,
unless all predicates in WHERE have been used up.
– i.e., avoid Cartesian products if possible.
• In spite of pruning plan space, this approach is still
exponential in the # of tables.
• If we want to consider all (bushy) trees, we need only a
slight modification to the algorithm.
Sailors:
B+ tree on rating
Hash on sid Example sname
Reserves:
B+ tree on bid
• Pass 1: sid=sid
– Sailors: B+ tree matches rating>5, and is probably
cheapest. However, if this selection is expected to
retrieve a lot of tuples, and index is unclustered, file bid=100 rating > 5
scan may be cheaper.
• Still, B+ tree plan kept (tuples are in rating order).
Reserves Sailors
– Reserves: B+ tree on bid matches bid=100; cheapest.
• Pass 2: We consider each plan retained from Pass 1 as the
outer, and consider how to join it with the (only) other
relation.
e.g., Reserves as outer: Hash index can be used to get Sailors
tuples that satisfy sid = outer tuple’s sid value.
SELECT S.sname