Presentation is loading. Please wait.

Presentation is loading. Please wait.

16.2.Algebraic Laws for Improving Query Plans

Similar presentations


Presentation on theme: "16.2.Algebraic Laws for Improving Query Plans"— Presentation transcript:

1 16.2.Algebraic Laws for Improving Query Plans

2 16.2 Algebraic Laws for Improving Query Plans
Commutative and Associative Laws Laws Involving Selection Pushing Selections Laws Involving Projection Laws About Joins and Products Laws Involving Duplicate Elimination Laws Involving Grouping and Aggregation 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 MOVIETITLE MOVIEYEAR STARNAME
Blood Diamond 2006 Leonardo Dicaprio The Quick and the Dead 1995 Titanic 1997 The Departed Body of lies 2008 Inception 2010 Somersault 2004 Samuel Henry Macbeth Love my Way The Great Raid 2005 Terminator Salvation 2009 Avatar Perseus Autumn in 2000 Vera A Farmiga Dust 2001 Mind the Gap

7 yesa=2006 and year >1998(StarsIn)

8 16.2.2 Laws Involving Selection
Laws of selection with binary operators like product, union, intersection, difference, join. (3 laws) For a union, the selection must be pushed to both arguments. σc (R U S) = σc (R) U σc (S) For a difference, the selection must be pushed to first argument and optionally to second. σc (R - S) = σc (R) – S σc (R - S) = σc (R) - σc (S) 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

9 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)

10 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

11 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;

12 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 16.2.3a Pushing Selections π starName ,studioName
σ year = 1996 StarsIn Movie π starName ,studioName (σ year = 1996(Movie)  StarsIn )= (red rule) R=Movie, S=StasIN π starName ,studioName (σ year = 1996(Movie  StarsIn )

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

15 16.2.3c Pushing Selections starName ,studioName StarsIn π
σ year = 1996 σ year = 1996 Movie StarsIn π starName ,studioName (σ year = 1996(Movie  StarsIn )=Blue Law R=Movie S=sratsin π starName ,studioName (σ year = 1996(Movie)  σ year = 1996(StarsIn ))

16 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

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

18 16.2.4 Laws Involving Projection
π starName σ movieYear = 1996 π starName, movieYear StarsIn Fig : Result of introducing a projection

19 16.2.5 Laws About Joins and Products
RCS= 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.

20 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

21 Laws Involving Duplicate Elimination
δ( R ) = R if R has no duplicates Important cases of such a relation R include A stored relation with a declared primary key 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 , - , π.

22 16.2.6 Laws Involving Duplicate Elimination
δ( R ) = R if R has no duplicates Important cases of such a relation R include A stored relation with a declared primary key 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 , - , π.

23 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 )

24 16.2.7 Laws Involving Grouping and Aggregation
One exception is the law below that γ absorbs δ δ(γL(R)) = γL ( R )

25 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.

26 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

27 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 γ

28 16.2.7 Laws Involving Grouping and Aggregation
γ movieYear ,MAX(birthDate) σ name = starName MovieStar StarsIn Figure : Initial Logical query plan for the query

29 16.2.7 Laws Involving Grouping and Aggregation
γ movieYear ,MAX(birthDate) π movieYear ,birthDate δ name = starName MovieStar StarsIn Figure : Second query plan

30 16.2.7 Laws Involving Grouping and Aggregation
γ movieYear ,MAX(birthDate) π movieYear ,birthDate name = starName δ δ π birthDate,name π birthDate,name MovieStar StarsIn Figure : Third query plan

31 Exercises for Section 16.2


Download ppt "16.2.Algebraic Laws for Improving Query Plans"

Similar presentations


Ads by Google