Presentation is loading. Please wait.

Presentation is loading. Please wait.

16.2.Algebraic Laws for Improving Query Plans. 16.2 Algebraic Laws for Improving Query Plans 16.2.1 Commutative and Associative Laws 16.2.2 Laws Involving.

Similar presentations


Presentation on theme: "16.2.Algebraic Laws for Improving Query Plans. 16.2 Algebraic Laws for Improving Query Plans 16.2.1 Commutative and Associative Laws 16.2.2 Laws Involving."— Presentation transcript:

1 16.2.Algebraic Laws for Improving Query Plans

2 16.2 Algebraic Laws for Improving Query Plans 16.2.1 Commutative and Associative Laws 16.2.2 Laws Involving Selection 16.2.3 Pushing Selections 16.2.4 Laws Involving Projection 16.2.5 Laws About Joins and Products 16.2.6 Laws Involving Duplicate Elimination 16.2.7 Laws Involving Grouping and Aggregation 16.2.8 Exercises for Section 16.2

3 16.2.1 Commutative and Associative Laws Commutativity for Sets and Bags (Ch5): R x S = S x R (Proof) R  S = S  R (ch5 e) R U S = S U R(ch5) R ∩ S = S ∩ R (ch5) Associativity Sets and Bags: : (R x S) x T = R x (S x T)‏ (R  S)  T = R  (S  T)‏ (R U S) U T = R U (S U T)‏ (ch5) (R ∩ S) ∩ T = R ∩ (S ∩ T)‏ (ch5)

4 16.2.2 Laws Involving Selection Selections reduce the size of relations. To make efficient query, the selection must be moved down the tree without the changing what the expression does. When the condition for the selection is complex, it helps to break the condition into its constituent parts.

5 16.2.2 Laws Involving Selection first two laws for σ are the splitting laws, σ c1 AND c2 (R) = σ c1 (σ c2 (R)) σ c1 OR c2 (R) = (σ c1 (R))  s (σ c2 (R)) The second law for OR works only if the relation R is the set.If R is a bag, then the set union Us will eliminate the duplicates incorrectly. σ c1 (σ c2 (R))= σ c2 (σ c1 (R))

6 16.2.2 Laws Involving Selection Laws of selection with binary operators like product, union, intersection, difference, join. (3 laws) 1.For a union, the selection must be pushed to both arguments. 1.σ c (R U S) = σ c (R) U σ c (S) 2.For a difference, the selection must be pushed to first argument and optionally to second. 1.σ c (R - S) = σ c (R) – S 2.σ c (R - S) = σ c (R) - σ c (S) 3.it is only required that the selection must be pushed to one or both argument. –σ c (R x S) = σ c (R) x S –σ c (R  S) = σ (R)  S –σ c (R  D S) = σ (R)  D S –σ c ( R ∩ S) = σ c (R) ∩ S

7 16.2.2 Laws Involving Selection Laws of selection with binary operators like product, union, intersection, 3. it is only required that the selection must be pushed to one or both argument. –σ c (R x S) = R x σ c (S) –σ c (R  S) = σ c (R)  σ c (S)

8 16.2.3 Pushing Selections Pushing Selection down the expression tree( i.e replacing the left side of one of the rules by the right side )is one of the best method to optimize query. An example for Pushing Selection is illustrated as follows

9 16.2.3 Pushing Selections Suppose we have relations StarsIn(title,year, starName) Movie(title,year, length,inColor, studioName) We Define a view Movies1996 as CREATE VIEW Movie1996 AS SELECT * FROM MOVIE WHERE year = 1996;

10 10 Selection:  C (R)  SQL: Select * from R where C C involves computable formulas

11 11 Projection:  L (R) Select L from R

12 16.2.3 Pushing Selections The query to find out which stars worked in which studios in 1996 SELECT starName,studioName FROM Movie1996 NATURAL JOIN StarsIn The view is Movie1996 is defined by σ year = 1996 (Movie)

13 Select MS.studioname, MS.starname From(Select M.title, M.year, M.length, M.inColor, M.studioname, M.producerC#, S.starname From (Select * From Movies where year=2009)M, (Select * From starsIn where Movieyear=2009)S where M.year= S.movieyear and S.movietitle=M.title)MS ;

14 16.2.3a Pushing Selections π starName,studioName π starName,studioName (σ year = 1996 (Movie)  StarsIn )= π starName,studioName (σ year = 1996 (Movie  StarsIn ) StarsIn σ year = 1996 Movie

15 16.2.3b Pushing Selections π starName,studioName π starName,studioName (σ year = 1996 (Movie)  StarsIn )= π starName,studioName (σ year = 1996 (Movie  StarsIn ) StarsIn σ year = 1996 Movie

16 16.2.3c Pushing Selections π starName,studioName π starName,studioName (σ year = 1996 (Movie  StarsIn )= π starName,studioName (σ year = 1996 (Movie)  σ year = 1996 (StarsIn )) StarsIn σ year = 1996 Movie σ year = 1996

17 16.2.4 Laws Involving Projection Projection, like selection can be pushed down through many other operators Pushing Projection usually involves introducing a new projection somewhere below an existing projection. Projection differs from selection in the aspect that projection reduces the length of the tuples whereas selection reduces the number of the tuples

18 16.2.4. Laws involving Projection Consider term π E  x –E : attribute, or expression involving attributes and constants. –All attributes in E are input attributes of projection and x is output attribute Simple projection: if a projection consists of only attributes. –Example: π a,b,c (R) is simple. a,b,c are input and output attributes. Projection can be introduced anywhere in expression tree as long as it only eliminates attributes that are never used.

19 16.2.4. Laws involving Projection (cont….) π L (R S) = π L ( π M (R) π N (S)) ; M and N are all attributes of R and S that are either join (in schema of both R and S) or input attributes of L π L (R c S) = π L ( π M (R) c π N (S)) ; M and N are all attributes of R and S that are either join(mentioned in condition of C ) or input attributes of L π L (R x S) = π L ( π M (R) x π N (S)) ; M and N are all attributes of R and S that are input attributes of L Projections cannot be pushed below set unions or either of set or bag versions of intersection or difference at all.

20 16.2.4 Laws Involving Projection SELECT starName FROM StarsIn WHERE year = 1996 Fig : Logical query plan for the above query π starName σ movieYear = 1996 StarsIn We can introduce a projection in the above Figure

21 16.2.4 Laws Involving Projection Convert the tree into relational algebra, then simplify as much as you can π starName σ movieYear = 1996 StarsIn π starName, movieYear

22 16.2.5 Laws About Joins and Products R  C S=  C (R  S) R  S=  L (  C (R  S)) Where C is the condition that equates each pair of atrribute from R and S with the same name, and L is the list that includes one attribute from each equted attributed and all other attributes of R and S.

23 16.2.6 Laws Involving Duplicate Elimination The operator δ, which eliminates duplicates from a bag can be pushed through only some of the operators Moving δ down the tree reduces the size of intermediate relation and may therefore be beneficial In some cases, we can move δ to a position where it can be eliminated because it is applied to a relation that does not have any duplicates

24 16.2.6 Laws Involving Duplicate Elimination δ( R ) = R if R has no duplicates Important cases of such a relation R include 1.A stored relation with a declared primary key 2.A relation that is the result of a γ operation,since grouping creates a relation with no duplicates δ cannot be moved across the operators like U, -, π.

25 16.2.6 Laws involving duplicate elimination Laws that “push” δ (delta) through other operator δ(R x S) = δ(R) x δ(S) δ(R S) = δ(R) δ(S) δ(R c S) = δ(R) c δ(S) δ( c(R)) = c(δ(R)) δ eliminates duplicates from a bag, but cannot be pushed through all the operators

26 16.2.7 Laws Involving Grouping and Aggregation While using grouping and aggregation,the applicability of many transformation depends on the details of the aggregation used. Due to the above,we cannot state laws in generality. One exception is the law below that γ absorbs δ δ(γ L (R)) = γ L ( R )

27 16.2.7 Laws Involving Grouping and Aggregation We may project useless attributes prior to applying γ operation γ L ( R ) = γ L (π M (R ) where M is the list containing at least all those attributes of R that are mentioned in L.

28 Laws involving grouping and aggregation (cont…) Some aggregations like MIN and MAX are not affected by presence or absence of duplicates Others like SUM,COUNT,AVG produce different values if duplicates are eliminated prior to aggregation.

29 16.2.7 Laws Involving Grouping and Aggregation Suppose we have the relation MovieStar(name,addr,gender,birthdate) StarsIn(movieTitle,movieYear,starName) Consider the query below Select movieYear,MAX(birthDate) FROM MovieStar,StarsIn WHERE name = starName GROUP BY movieYear

30 16.2.7 Laws Involving Grouping and Aggregation The FROM list is expressed by a product and the WHERE clause by a selection above it. The grouping and aggregation are expressed by the γ. Combine the selection and product into an equijoin Generate a δ below the γ,since the γ is duplicate-impervious Generate a π between the γ and the introduced δ to project onto movieYear and birthDate,the only attributes relevant to the γ

31 16.2.7 Laws Involving Grouping and Aggregation 1. Use 16.2.5. (and following 2 reasons) we can rewrite the tree 2. There is no duplication in output (because γ), we can add  3. By projection law We can add . γ movieYear,MAX(birthDate) σ name = starName MovieStarStarsIn 

32 16.2.7 Laws Involving Grouping and Aggregation Figure : Second query plan γ movieYear,MAX(birthDate) name = starName MovieStarStarsIn  movieYear,birthDate δ

33 16.2.7 Laws Involving Grouping and Aggregation Figure : Third query plan  can be push down γ movieYear,MAX(birthDate) name = starName MovieStar StarsIn π movieYear,birthDate δδ π birthDate,name

34 16.2.7a Additional Example From DB1

35 16.2.7a Laws Involving Grouping and Aggregation SELECTPNUM, SUM(QTY) FROMSHIPMENTs, Parts GROUPBY PNAME;

36 16.2.7b Laws Involving Grouping and Aggregation Figure : Initial Logical query plan for the query γ pname,SUM(qty)  sum (σ sh.pnum=p.pnum (Shipments  Parts)) γ pname,SUM(qty)  sum σ pnum = pnum Shipments (Sh)Parts(P) 

37 16.2.7c Laws Involving Grouping and Aggregation γ pname,SUM(qty)  sum (  pname.qty  (Shipments  Parts)) γ pname,SUM(qty)  sum Shipments Parts  pname,QTY δ

38 16.2.7d Laws Involving Grouping and Aggregation γ pname,Sum(qty)  sum (  pname.qty ((   pname.qty (Shipments))  (   pname.qty (Parts)))) γ pname,SUM(qty)  sum Shipments Parts π pname,qty δδ π qty,pnum  pnum, pname

39 16.2.8 Exercises for Section 16.2


Download ppt "16.2.Algebraic Laws for Improving Query Plans. 16.2 Algebraic Laws for Improving Query Plans 16.2.1 Commutative and Associative Laws 16.2.2 Laws Involving."

Similar presentations


Ads by Google