21
Query Compiler By:Payal Gupta Roll No:106(225)

Query Compiler By:Payal Gupta Roll No:106(225) Professor :Tsau Young Lin

  • View
    227

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Query Compiler By:Payal Gupta Roll No:106(225) Professor :Tsau Young Lin

Query Compiler

By:Payal Gupta

Roll No:106(225)

Professor :Tsau Young Lin

Page 2: Query Compiler By:Payal Gupta Roll No:106(225) Professor :Tsau Young Lin

Pushing Selections It is, replacing the left side of one of the

rules by its right side.In pushing selections we first a selection as

far up the tree as it would go, and then push the selections down all possible branches.

Page 3: Query Compiler By:Payal Gupta Roll No:106(225) Professor :Tsau Young Lin

Let’s take an example:S t a r s I n ( t i t l e , year, starName)Movie(title, year, length, incolor, studioName,

producerC#)

Define view MoviesOf 1996 by: CREATE VIEW MoviesOfl996 AS SELECT * FROM Movie ,WHERE year = 1996;

Page 4: Query Compiler By:Payal Gupta Roll No:106(225) Professor :Tsau Young Lin

"which stars worked for which studios in 1996?“ can be given by a SQL Query:

SELECT starName, studioName FROM MoviesOfl996 NATURAL JOIN

StarsIn;

Page 5: Query Compiler By:Payal Gupta Roll No:106(225) Professor :Tsau Young Lin

ΠstarName,studioName

O Year=1996 StarsIn

Movie

Logical query plan constructed from definition of a query and view

Page 6: Query Compiler By:Payal Gupta Roll No:106(225) Professor :Tsau Young Lin

ΠstarName,studioName

O Year=1996

StarsInMovie

Year=1996O

Improving the query plan by moving selections up and down the tree

Page 7: Query Compiler By:Payal Gupta Roll No:106(225) Professor :Tsau Young Lin

"pushing" projections really involves introducing a new projection somewhere below an existing projection.

projection keeps the number of tuples the same and only reduces the length of tuples.

To describe the transformations of extended projection Consider a term E + x on the list for a projection, where E is an attribute or an expression involving attributes and constants and x is an output attribute.

Laws Involving Projection

Page 8: Query Compiler By:Payal Gupta Roll No:106(225) Professor :Tsau Young Lin

ExampleLet R(a, b, c) and S(c, d, e) be two relations.

Consider the expression x,+,,,, b+y(R w S). The input attributes of the projection are a,b, and e, and c is the only join attribute. We may apply the law for pushing projections below joins to get the equivalent expression:

Πa+e->x,b->y(Πa,b,c(R) Πc,e(S))

Eliminating this projection and getting a third equivalent expression:Πa+e->x, b->y( R Πc,e(S))

Page 9: Query Compiler By:Payal Gupta Roll No:106(225) Professor :Tsau Young Lin

In addition, we can perform a projection entirely before a bag union. That is:

ΠL(R UB S)= ΠL(R) )UB ΠL(S)

Page 10: Query Compiler By:Payal Gupta Roll No:106(225) Professor :Tsau Young Lin

Laws About Joins and Productslaws that follow directly from the definition of the

join:

R c S = c( R * S)

R S = ΠL( c ( R * S) ) , where C is the condition that equates each pair of attributes from R and S with the same name. and L is a list that includes one attribute from each equated pair and all the other attributes of R and S.

We identify a product followed by a selection as a join of some kind.

O

O

Page 11: Query Compiler By:Payal Gupta Roll No:106(225) Professor :Tsau Young Lin

Laws Involving Duplicate EliminationThe operator δ which eliminates duplicates

from a bag can be pushed through many but not all operators.

In general, moving a δ down the tree reduces the size of intermediate relations and may therefore beneficial.

Moreover, sometimes we can move δ to a position where it can be eliminated altogether,because it is applied to a relation that is known not to possess duplicates.

Page 12: Query Compiler By:Payal Gupta Roll No:106(225) Professor :Tsau Young Lin

δ (R)=R if R has no duplicates. Important cases of such a relation R include:

a) A stored relation with a declared primary key, and

b) A relation that is the result of a γ operation, since grouping creates a relation with no duplicates.

Page 13: Query Compiler By:Payal Gupta Roll No:106(225) Professor :Tsau Young Lin

Several laws that "push" δ through other operators are:

δ (R*S) =δ(R) * δ(S)δ (R S)=δ(R) δ(S)δ (R c S)=δ(R) c δ(S)δ ( c (R))= c (δ(R))

We can also move the δ to either or both of the arguments of an intersection:

δ (R ∩B S) = δ(R) ∩B S = R ∩B δ (S) = δ(R) ∩B

δ (S)

O O

Page 14: Query Compiler By:Payal Gupta Roll No:106(225) Professor :Tsau Young Lin

Laws Involving Grouping and AggregationWhen we consider the operator γ, we find that

the applicability of many transformations depends on the details of the aggregate operators used. Thus we cannot state laws in the generality that we used for the other operators. One exception is that a γ absorbs a δ . Precisely:

δ(γL(R))=γL(R)

Page 15: Query Compiler By:Payal Gupta Roll No:106(225) Professor :Tsau Young Lin

let us call an operator γ duplicate-impervious if the only aggregations in L are MIN and/or MAX then:

γ L(R) = γ L (δ(R)) provided γL is duplicate-impervious.

Page 16: Query Compiler By:Payal Gupta Roll No:106(225) Professor :Tsau Young Lin

ExampleSuppose we have the relations MovieStar(name , addr , gender, birthdate) StarsIn(movieTitle, movieyear, starname) and we want to know for each year the

birthdate of the youngest star to appear in a movie that year. We can express this query as:

SELECT movieyear, MAX(birth date) FROM MovieStar, StarsIn WHERE name = starName GROUP BY movieyear;

Page 17: Query Compiler By:Payal Gupta Roll No:106(225) Professor :Tsau Young Lin

γ movieYear, MAX ( birthdate )

name = starName

MovieStar StarsIn

Initial logical query plan for the query

O

Page 18: Query Compiler By:Payal Gupta Roll No:106(225) Professor :Tsau Young Lin

Some transformations that we can apply to Fig are

1. Combine the selection and product into an equijoin.

2.Generate a δ below the γ , since the γ is duplicate- impervious.

3. Generate a Π between the γ and the introduced δ to project onto movie-Year and birthdate, the only attributes relevant to the γ

Page 19: Query Compiler By:Payal Gupta Roll No:106(225) Professor :Tsau Young Lin

γ movieYear, MAX ( birthdate )

Π movieYear, birthdate

δ

name = starName

MovieStar StarsIn

Another query plan for the query

Page 20: Query Compiler By:Payal Gupta Roll No:106(225) Professor :Tsau Young Lin

γ movieYear, MAX ( birthdate )

Π movieYear, birthdate

name = starName

δ δ

Π birthdate,name Π movieYear,starname

MovieStar StarsIn

third query plan for Example

Page 21: Query Compiler By:Payal Gupta Roll No:106(225) Professor :Tsau Young Lin

Thank You