Chapter 7 The Query Compiler Query Processor : Query Parser Tree Logical Query Plan Physical Query Plan Query Structure Relational Algebraic Expression

  • View
    221

  • Download
    0

Embed Size (px)

Transcript

  • Chapter 7 The Query CompilerQuery ProcessorQuery Parser Tree Logical Query Plan Physical Query Plan Query Structure Relational Algebraic Expression Tree 1 2 3

  • The Stages of Query CompilationParser QueryPreprocessorLogical query plan generatorPreferred logic query plan 7.1 7.3Query rewriter

  • Parsing Convert a SQL statement to a parse tree which consists of the following nodes: 1. Atoms: lexical elements such as keywords, names of attributes or relations, constants, parentheses, operators and other schema elements 2. Syntactic categories: names for families of query subparts such ,

  • A Grammar of a Simple Subset of SQL 1. Query ::= ::= () 2. Select-From-Where::= SELECT FROM WHERE

  • 3. Select-Lists:::= ,::= 4. From-Lists::= ,::= 5. Conditions:::= AND ::= IN ::= = ::= LIKE 6. ::=

  • An ExampleStarsIn( title, year, starName)MovieStar( name,address, gender, birthdate)

    Find the movies with stars born in 1960 SELECT title FROM StarsIn WHERE starName IN ( SELECT name FROM MovieStar WHERE birthdate LIKE %1960 );

  • SELECT FROM WHERE IN title StarsIn ( )starName SELECT FROM WHERE LIKE name MovieStar birthdate %1960

  • SELECT titleFROM StarsIn, MovieStarWHERE starName =name AND birthdate LIKE %1960

    SELECT FROM WHERE , title StarsIn MovieStar = LIKE starName name birthdate %1960AND

  • PreprocessorView ExpansionSemantic CheckingCheck relation usesCheck and resolve attribute usesCheck types

  • Algebraic Laws for Improving Query Plans Commutative and Associative Laws Laws Involving Selection Laws Involving Projection Laws About Joins and Products Laws Involving Duplicate Elimination Laws Involving Grouping and Aggregation

  • Commutative and Associative Laws RS=SR RS=SR RS=SR RS=SR

    (RS) T=R (ST)(RS) T=R (ST)(RS) T=R (ST)(RS) T=R (ST)

  • Theta JoinR S = S R c c

    Suppose R(a,b), S(b,c) and T(c,d). (R S) T R ( S T ) R.a>S.b aS.b a

  • Laws Involving Selection

    C1 AND C2 (R)= C1(C2(R))C1 OR C2 (R)= (C1(R))s(C2(R))

    = C2(C1(R))

  • Transformation Examples

    (a=1 OR a=3)AND b

  • Law for Binary Operators 1. The selection must be pushed to both arguments. 2. : The selection must be pushed to the first argument and optionally may be pushed to the second. 3. OthersIt is only required that the selection be pushed to one argument.

  • C(RS) = C(R)C(S) C(RS) = C(R)S = C(R)C(S) C(RS) = C(R)SC(RS) = C(R)S

    C(R S) =C(R) S D DC(RS) = C(R)S For example, R(a,b) and S(b,c)a=1 OR a=3(b

  • Pushing SelectionsSometimes move a selection as far up the tree and then push the selections down all possible branchesE.g. , StarsIn (title, year, starName) Movie (title, year, length, studioName)View : CREATE VIEW MovieOf1996 AS SELECT * FROM Movie WHERE year=1996; Query: Which stars worked for which studios in 1996? SELECT starName, studioName FROM MovieOf1996 NATURAL JOIN StarsIn

  • starName, studioName year=1996 StarsIn Movie C(R S) = C(R) S year=1996(Movie) StarsIn = year=1996(Movie StarsIn) C(R S) =C(R) C(S) year=1996(Movie StarsIn) = year=1996(Movie) year=1996(StarsIn) starName, studioName year=1996 year=1996 Movie StarsIn

  • Laws Involving Projection A projection may be introduced anywhere in an expression tree, as long as it eliminates only attributes that are never used by any of the operators above, and are not in the result of the entire expression.

  • Basic Laws L(RS)=L(M(R)N(S)) L(RS)=L(M(R)N(S)) C C

    L(RS)=L(M(R) N(S))

    where M,N are attributes of R and S respectively or input attributes in L

  • Suppose there are relations R(a,b,c), S(c,d,e) a+ex,by(RS)

    a+ex,by(a,b,c(R)c,e(S))

    a+ex,by( Rc,e(S))

    L(RB S)=L(R)B L(S) Projections cannot be pushed below S,,. For example, R(a,b):{(1,2)}; S(a,b): {(1,0)} a(RS)= a(R)a(S)={(1)}

  • Projection Involving Some Computation R(a,b,c), S(c,d,e)

    a+bx,d+ey(RS) =x,y(a+bx,c(R)d+ey,c(S)) If x or y is c, we need a temporary name. a+bc,d+ey(RS) =zc,y(a+bz,c(R)d+ey,c(S))

  • Pushing a projection below a selection L(c(R))=L(c(M(R))) (M: input attributes of L or mentioned in C)For example, from StarsIn( title, years, starName)to find stars that worked in 1996SELECT starNameFROM StarsInWHERE year=1996; starName year=1996 StarsIn starName year=1996starNameyear StarsInNotice: If there is index on year, it may not improve the plan

  • Laws About Joins and ProductsRS=c(R S) c

    RS=L(c(R S))

    Usually use the rule from right to left ?

  • Laws Involving Duplicate Elimination

    (R)=R if R has no duplicates [ R:1) A stored relation with a declared primary key 2) The result of a operation] (R s S)=R s S the same as s, s

  • Several laws that push

    (RS) =(R)(S) (R S) =(R) (S)

    (R S) =(R) (S) c c (c(R))=c((R))

    Notice cannot be moved across B,B or

  • For example, R has two copies of t tupleS has one copy of tT(a,b): {(1,2),(1,3)}.(a(T)={(1)}a((T))={(1) , (1)}

  • Laws Involving Grouping and AggregationGeneral Rules (LR=L(R) L(R)=L(M(R)) (M: attributes of R mentioned in L Other Rules MIN, MAX: Not affected by duplicates L(R)= L((R)) SUM, COUNT, AVG: Affected by duplicates

  • An Example

    Relations: MovieStar( name, addr, gender, birthdate) StarsIn( title, year, starName) QueryFor each year, find the birthdate of the youngest star to appear in a movie that year

    SELECT year, MAX (birthdate)FROM MovieStar, StarsInWHERE name=starNameGROUP BY year; year, MAX (birthdate) name=starName MovieStar StarsIn

  • Combine the selection and product into an equijoinGenerate a belowGenerate a between the and the introduced to project onto year and birthdate year, MAX (birthdate) name=starName name=starNameMovieStar StarsIn year, MAX (birthdate) year,birthdate name=starName birthdate, name year, starNameMovieStar StarsIn

  • From Parse Trees to Logical Query Plans Suppose is a construct has no subqueriesconvert into a relational algebra expression from bottom to top as follows

    1. Product all relations from 2. c, C is the expression 3. L, L is the list of attributes in the

  • Translation of A Parse Tree to an Algebraic Expression Tree

    SELECT FROM WHERE , title StarsIn MovieStar = LIKE starName name birthdate %1960AND

  • title starName=name AND birthdate LIKE%1960 StarsIn MovieStar

  • Removing Subqueries From ConditionsTwo-argument selection Node: Left Child: The Relation R Right Child: The Condition C

  • title StarsIn IN name birthdate LIKE 1960 starName MovieStar

  • Replacement of Two-Argument Selection by a One-Argument Selection Uncorrelated Subquery Two-Argument Selection with a left child for R and right child for t IN S Replace the by the expression SReplace the two-argument selection c. Give c an argument that is the product of R and S

  • Uncorrelated Subquery title starName=name

    StarsIn name birthdate LIKE 1960

    MovieStar

  • Correlated Subquery: SELECT DISTINCT m1.title, m1.yearFROM StarsIn m1WHERE m1.year-40
  • m1.title,m1.year

    m1.year-40abd

    m2.title=m1.title AND m2.year=m1.year

    StarsIn m1 m2.title,m2.year,Avg(s.birthdate)abd

    m2.starName=s.name

    StarsIn m2 MovieStar s

  • m2.title,m2.year