From the above we can easily derive that sigma_{c_1} sigma_{c_2} R = sigma_{c_2} sigma_{c_1} R
The meaning of the arrows: rewritings versus axioms. What we write is axioms. The left to right direction is 99% the beneficial. However views complicate matters. Generalization to theta join Example 6.14
Watch out rules like that: you cannot apply them blindly. May lead to an explosion of alternative expressions. Use in the exam the rules of duplicates.
Two relational algebra expressions are said to be equivalent if on every legal database instance the two expressions generate the same set of tuples
Note: order of tuples is irrelevant
In SQL, inputs and outputs are bags (multi-sets) of tuples
Two expressions in the bag version of the relational algebra are said to be equivalent if on every legal database instance the two expressions generate the same bag of tuples
An equivalence rule says that expressions of two forms are equivalent
Can replace expression of first form by second, or vice versa
For pushing down selections into a (theta) join we have the following cases:
(push 1) When all the attributes in 0 involve only the attributes of one of the expressions ( E 1 ) being joined. 0 E 1 E 2 ) = ( 0 (E 1 )) E 2
(split) When 1 involves only the attributes of E 1 and 2 involves only the attributes of E 2 .
1 E 1 E 2 ) = ( 1 (E 1 )) ( (E 2 ))
(impossible) When involves both attributes of E 1 and E 2 (it is a join condition)
Equivalence Rules (Cont.)
10.
Pushing Selection thru Cartesian Product and Join R S cond cond S R The right direction requires that cond refers to S attributes only R S cond cond S R
11.
Projection Decomposition p XY p X p Y p Y X Y X p p X p p Y p X.tax Y.price total=X.tax*Y.price p {total} {tax} {price} p XY p X p Y
Could compute account depositor first, and join result with branch-city = “Brooklyn” ( branch) but account depositor is likely to be a large relation.
Since it is more likely that only a small fraction of the bank’s customers have accounts in branches located in Brooklyn, it is better to compute
17.
The role of Query Optimization SQL physical algebra logical algebra parsing, normalization logical query optimization physical query optimization query execution
18.
The role of Query Optimization SQL physical algebra logical algebra parsing, normalization logical query optimization physical query optimization query execution Compare different relational algebra plan on result size (Practicum 2A)
19.
The role of Query Optimization SQL physical algebra logical algebra parsing, normalization logical query optimization physical query optimization query execution
Consider finding the best join-order for r 1 r 2 . . . r n .
There are (2( n – 1))!/( n – 1)! different join orders for above expression. With n = 7, the number is 665280, with n = 10, the number is greater than 176 billion!
No need to generate all the join orders. Using dynamic programming, the least-cost join order for any subset of { r 1 , r 2 , . . . r n } is computed only once and stored for future use.
procedure findbestplan( S ) if ( bestplan [ S ]. cost ) return bestplan [ S ] // else bestplan [ S ] has not been computed earlier, compute it now for each non-empty subset S 1 of S such that S 1 S P1= findbestplan( S 1) P2= findbestplan( S - S 1) A = best algorithm for joining results of P 1 and P 2 cost = P 1. cost + P 2. cost + cost of A if cost < bestplan [ S ]. cost bestplan [ S ]. cost = cost bestplan [ S ]. plan = “execute P 1. plan ; execute P 2. plan ; join results of P 1 and P 2 using A ” return bestplan [ S ]
Must consider the interaction of evaluation techniques when choosing evaluation plans: choosing the cheapest algorithm for each operation independently may not yield best overall algorithm. E.g.
merge-join may be costlier than hash-join, but may provide a sorted output which reduces the cost for an outer level aggregation.
nested-loop join may provide opportunity for pipelining
An interesting sort order is a particular sort order of tuples that could be useful for a later operation.
Generating the result of r 1 r 2 r 3 sorted on the attributes common with r 4 or r 5 may be useful, but generating it sorted on the attributes common only r 1 and r 2 is not useful.
Using merge-join to compute r 1 r 2 r 3 may be costlier, but may provide an output sorted in an interesting order.
Not sufficient to find the best join order for each subset of the set of n given relations; must find the best join order for each subset, for each interesting sort order
Simple extension of earlier dynamic programming algorithms
Usually, number of interesting orders is quite small and doesn’t affect time/space complexity significantly
1. Deconstruct conjunctive selections into a sequence of single selection operations (Equiv. rule 1.).
2. Move selection operations down the query tree for the earliest possible execution (Equiv. rules 2, 7a, 7b, 11).
3. Execute first those selection and join operations that will produce the smallest relations (Equiv. rule 6).
4. Replace Cartesian product operations that are followed by a selection condition by join operations (Equiv. rule 4a).
5. Deconstruct and move as far down the tree as possible lists of projection attributes, creating new projections where needed (Equiv. rules 3, 8a, 8b, 12).
6. Identify those subtrees whose operations can be pipelined, and execute them using pipelining).
30.
Heuristic Join Order: the Wong-Youssefi algorithm (INGRES) Sample TPC-H Schema Nation(NationKey, NName) Customer(CustKey, CName, NationKey) Order(OrderKey, CustKey, Status) Lineitem(OrderKey, PartKey, Quantity) Product(SuppKey, PartKey, PName) Supplier(SuppKey, SName) SELECT SName FROM Nation, Customer, Order, LineItem, Product, Supplier WHERE Nation.NationKey = Cuctomer.NationKey AND Customer.CustKey = Order.CustKey AND Order.OrderKey=LineItem.OrderKey AND LineItem.PartKey= Product.Partkey AND Product.Suppkey = Supplier.SuppKey AND NName = “Canada” Find the names of suppliers that sell a product that appears in a line item of an order made by a customer who is in Canada
31.
Challenges with Large Natural Join Expressions
For simplicity, assume that in the query
All joins are natural
whenever two tables of the FROM clause have common
attributes we join on them
Consider Right-Index only
Nation Customer Order LineItem Product Supplier σ NName=“Canada” π SName One possible order RI RI RI RI RI Index
32.
Wong-Yussefi algorithm assumptions and objectives
Assumption 1 (weak): Indexes on all join attributes (keys and foreign keys)
Assumption 2 (strong): At least one selection creates a small relation
A join with a small relation results in a small relation
Objective: Create sequence of index-based joins such that all intermediate results are small
can extend for non-natural equality joins by merging nodes
Nation Customer Order LineItem Product Supplier
34.
Small Relations/Hypergraph Reduction CName CustKey NationKey NName Status OrderKey Quantity PartKey SuppKey PName SName Nation Customer Order LineItem Product Supplier NationKey NName “ Nation” is small because it has the equality selection NName = “Canada” Nation σ NName=“Canada” Index Pick a small relation (and its conditions) to start the plan
35.
CName CustKey NationKey NName Status OrderKey Quantity PartKey SuppKey PName SName Nation Customer Order LineItem Product Supplier NationKey NName Nation σ NName=“Canada” Index RI (1) Remove small relation (hypergraph reduction) and color as “small” any relation that joins with the removed “small” relation Customer (2) Pick a small relation (and its conditions if any) and join it with the small relation that has been reduced
36.
After a bunch of steps… Nation Customer Order LineItem Product Supplier σ NName=“Canada” π SName RI RI RI RI RI Index
In our example, the original nested query would be transformed to create table t 1 as select distinct customer-name from depositor select customer-name from borrower , t 1 where t 1 . customer-name = borrower.customer-name
The process of replacing a nested query by a query with a join (possibly with a temporary relation) is called decorrelation .
Decorrelation is more complicated when
the nested subquery uses aggregation, or
when the result of the nested subquery is used to test for equality, or
when the condition linking the nested subquery to the other query is not exists ,
Be the first to comment