Presentation is loading. Please wait.

Presentation is loading. Please wait.

16.2 ALGEBRAIC LAWS FOR IMPROVING QUERY PLANS Ramya Karri ID: 206.

Similar presentations


Presentation on theme: "16.2 ALGEBRAIC LAWS FOR IMPROVING QUERY PLANS Ramya Karri ID: 206."— Presentation transcript:

1 16.2 ALGEBRAIC LAWS FOR IMPROVING QUERY PLANS Ramya Karri ID: 206

2 Optimizing the Logical Query Plan The translation rules converting a parse tree to a logical query tree do not always produce the best logical query tree. It is often possible to optimize the logical query tree by applying relational algebra laws to convert the original tree into a more efficient logical query tree. Optimizing a logical query tree using relational algebra laws is called heuristic optimization

3 Relational Algebra Laws These laws often involve the properties of: – commutativity - operator can be applied to operands independent of order. E.g. A + B = B + A - The “+” operator is commutative. – associativity - operator is independent of operand grouping. E.g. A + (B + C) = (A + B) + C - The “+” operator is associative.

4 Associative and Commutative Operators The relational algebra operators of cross-product (×), join ( ⋈ ), union, and intersection are all associative and commutative. Commutative R X S = S X R R ⋈ S = S ⋈ R R  S = S  R R ∩ S = S ∩ R Associative (R X S) X T = S X (R X T) (R ⋈ S) ⋈ T= S ⋈ (R ⋈ T) (R  S)  T = S  (R  T) (R ∩ S) ∩ T = S ∩ (R ∩ T)

5 Laws Involving Selection Complex selections involving AND or OR can be broken into two or more selections: (splitting laws) σ C1 AND C2 (R) = σ C1 ( σ C2 (R)) σ C1 OR C2 (R) = ( σ C1 (R) )  S ( σ C2 (R) ) Example – R={a,a,b,b,b,c} – p1 satisfied by a,b, p2 satisfied by b,c – σ p1vp2 (R) = {a,a,b,b,b,c} – σ p1 (R) = {a,a,b,b,b} – σ p2 (R) = {b,b,b,c} – σ p1 (R) U σ p2 (R) = {a,a,b,b,b,c}

6 Laws Involving Selection (Contd..) Selection is pushed through both arguments for union: σ C (R  S) = σ C (R)  σ C (S) Selection is pushed to the first argument and optionally the second for difference: σ C (R - S) = σ C (R) - S σ C (R - S) = σ C (R) - σ C (S)

7 Laws Involving Selection (Contd..) All other operators require selection to be pushed to only one of the arguments. For joins, may not be able to push selection to both if argument does not have attributes selection requires. σ C (R × S) = σ C (R) × S σ C (R ∩ S) = σ C (R) ∩ S σ C (R ⋈ S) = σ C (R) ⋈ S σ C (R ⋈ D S) = σ C (R) ⋈ D S

8 Laws Involving Selection (Contd..) Example Consider relations R(a,b) and S(b,c) and the expression σ (a=1 OR a=3) AND b<c (R ⋈ S) σ a=1 OR a=3 (σ b<c (R ⋈ S)) σ a=1 OR a=3 (R ⋈ σ b<c (S)) σ a=1 OR a=3 (R) ⋈ σ b<c (S)

9 Laws Involving Projection Like selections, it is also possible to push projections down the logical query tree. However, the performance gained is less than selections because projections just reduce the number of attributes instead of reducing the number of tuples.

10 Laws Involving Projection Laws for pushing projections with joins: π L (R × S) = π L( π M (R) × π N (S)) π L (R ⋈ S) = π L(( π M (R) ⋈ π N (S)) π L (R ⋈ D S) = π L(( π M (R) ⋈ D π N (S))

11 Laws Involving Projection Laws for pushing projections with set operations. Projection can be performed entirely before union. π L (R U B S) = π L (R) U B π L (S) Projection can be pushed below selection as long as we also keep all attributes needed for the selection ( M = L  attr(C)). π L ( σ C (R)) = π L ( σ C ( π M (R)))

12 Laws Involving Join We have previously seen these important rules about joins: 1. Joins are commutative and associative. 2. Selection can be distributed into joins. 3. Projection can be distributed into joins.

13 Laws Involving Duplicate Elimination The duplicate elimination operator (δ) can be pushed through many operators. R has two copies of tuples t, S has one copy of t, δ (RUS)=one copy of t δ (R) U δ (S)=two copies of t

14 Laws Involving Duplicate Elimination Laws for pushing duplicate elimination operator (δ): δ (R × S) = δ (R) × δ (S) δ (R S) = δ (R) δ (S) δ (R D S) = δ (R) D δ (S) δ ( σ C(R) = σ C ( δ (R))

15 Laws Involving Duplicate Elimination The duplicate elimination operator (δ) can also be pushed through bag intersection, but not across union, difference, or projection in general. δ (R ∩ S) = δ (R) ∩ δ (S)

16 Laws Involving Grouping The grouping operator (γ) laws depend on the aggregate operators used. There is one general rule, however, that grouping subsumes duplicate elimination: δ(γL(R)) = γL(R) The reason is that some aggregate functions are unaffected by duplicates (MIN and MAX) while other functions are (SUM, COUNT, and AVG).


Download ppt "16.2 ALGEBRAIC LAWS FOR IMPROVING QUERY PLANS Ramya Karri ID: 206."

Similar presentations


Ads by Google