Query Compiler The Query Compiler Parses SQL query

  • Slides: 24
Download presentation
Query Compiler

Query Compiler

The Query Compiler • Parses SQL query into parse tree • Transforms parse tree

The Query Compiler • Parses SQL query into parse tree • Transforms parse tree into expression tree (logical query plan) • Transforms logical query plan into physical query plan

SQL query parse tree convert logical query plan apply laws “improved” l. q. p

SQL query parse tree convert logical query plan apply laws “improved” l. q. p estimate result sizes l. q. p. +sizes consider physical plans {P 1, P 2, …. . } answer execute Pi statistics pick best {P 1, C 1>. . . } estimate costs

Grammar for simple SQL <Query> : : = <SFW> <Query> : : = (<Query>)

Grammar for simple SQL <Query> : : = <SFW> <Query> : : = (<Query>) <SFW> : : = SELECT <Sel. List> FROM <From. List> WHERE <Cond> <Sel. List> : : = <Attr>, <Sel. List> : : = <Attr> <From. List> : : = <Relation>, <From. List> : : = <Relation> <Cond> : : = <Cond> AND <Cond> : : = <Tuple> IN <Query> <Cond> : : = <Attr> LIKE <Pattern> <Tuple> : : = <Attr> Atoms(constants), <syntactic categories>(variable), : : = (can be expressed/defined as)

Query Stars. In(title, year, star. Name) Movie. Star(name, address, gender, birthdate) Query: Give titles

Query Stars. In(title, year, star. Name) Movie. Star(name, address, gender, birthdate) Query: Give titles of movies that have at least one star born in 1960 SELECT title FROM Stars. In, Movie. Star WHERE star. Name = name AND birthdate LIKE '%1960%' ;

Parse Tree <Query> <SFW> SELECT <Sel. List> FROM <Attribute> <From. List> WHERE <Condition> <Rel.

Parse Tree <Query> <SFW> SELECT <Sel. List> FROM <Attribute> <From. List> WHERE <Condition> <Rel. Name> , <From. List> title Stars. In AND <Rel. Name> Movie. Star <Condition> <Attribute> star. Name = <Attribute> name <Condition> <Attribute> LIKE <Pattern> birthdate ‘%1960’

Another query equivalent SELECT title FROM Stars. In WHERE star. Name IN ( SELECT

Another query equivalent SELECT title FROM Stars. In WHERE star. Name IN ( SELECT name FROM Movie. Star WHERE birthdate LIKE '%1960%' );

The Preprocessor (expand query & semantic checking) • Checks against schema definition: – Relation

The Preprocessor (expand query & semantic checking) • Checks against schema definition: – Relation uses – Attribute uses, resolve names ( A to R. A) – Use of types (strings, integers, dates, etc) and operators’ arguments type/arity These preprocessing functions are called semantic checking • If all tests are passed, then the parse tree is said to be valid

Algebraic laws for transforming logical query plans • Commutative and associative laws: Above laws

Algebraic laws for transforming logical query plans • Commutative and associative laws: Above laws are applicable for both sets and bags

Theta-join • Commutative: • Not always associative: – On schema R(a, b), S(b, c),

Theta-join • Commutative: • Not always associative: – On schema R(a, b), S(b, c), T(c, d) the first query can not be transformed into the second: (Why? ) Because, we can’t join S and T using the condition a<d since a is an attribute of neither S nor T.

Laws Involving Selection ( ) Splitting laws Order is flexible Only if R is

Laws Involving Selection ( ) Splitting laws Order is flexible Only if R is a set. The union is “set union”

Laws Involving Selection ( ) What about intersection?

Laws Involving Selection ( ) What about intersection?

Algebraic Laws involving selection For the binary operators, we push the selection only if

Algebraic Laws involving selection For the binary operators, we push the selection only if all attributes in the condition C are in R.

Example: • Consider relation schemas R(A, B) and S(B, C) and the expression below:

Example: • Consider relation schemas R(A, B) and S(B, C) and the expression below: (A=1 OR A=3) AND B<C(R S) 1. Splitting AND A=1 OR A=3 ( B < C(R S)) 2. Push to S A=1 OR A=3 (R B < C(S)) 3. Push to R A=1 OR A=3 (R) B < C(S)

Pushing selections • Usually selections are pushed down the expression tree. • The following

Pushing selections • Usually selections are pushed down the expression tree. • The following example shows that it is sometimes useful to pull selection up in the tree. Stars. In(title, year, star. Name) Movie(title, year, length, studio. Name) CREATE VIEW Movies. Of 1996 AS SELECT * FROM MOVIE WHERE year=1996; Query: Which stars worked for which studios in 1996? SELECT star. Name, studio. Name FROM Movies. Of 1996 NATURAL JOIN Stars. IN;

pull selection up then push down

pull selection up then push down

Laws for (bag) Projection • A simple law: Project out attributes that are not

Laws for (bag) Projection • A simple law: Project out attributes that are not needed later. – i. e. keep only the input attr. and any join attribute.

Examples for pushing projection Schema R(a, b, c), S(c, d, e)

Examples for pushing projection Schema R(a, b, c), S(c, d, e)

Example: Pushing Projection • Schema: Stars. In(title, year, star. Name) • Query: SELECT star.

Example: Pushing Projection • Schema: Stars. In(title, year, star. Name) • Query: SELECT star. Name FROM Stars. In WHERE year = 1996; star. Name Should we transform to year=1996 ? Depends! Is Stars. In stored or computed? year=1996 star. Name, year Stars. In

Reasons for not pushing the projection • If Stars. In is stored, then for

Reasons for not pushing the projection • If Stars. In is stored, then for the projection we have to scan the relation. • If the relation is pipelined from some previous computation, then yes, we better do the projection (on the fly). • Also, if for example there is an index on year for Stars. In, such index is useless in the projected relation star. Name, year(Stars. In) – While such an index is very useful for the selection on “year=1996”

Laws for duplicate elimination and grouping Try to move in a position where it

Laws for duplicate elimination and grouping Try to move in a position where it can be eliminated altogether E. g. when is applied on • 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. absorbs

Improving logical query plans • Push as far down as possible (sometimes pull them

Improving logical query plans • Push as far down as possible (sometimes pull them up first). • Do splitting of complex conditions in order to push even further. • Push as far down as possible, introduce new early (but take care for exceptions) • Combine with to produce -joins or equi-joins • Choose an order for joins

Example of improvement SELECT title FROM Stars. In, Movie. Star WHERE star. Name =

Example of improvement SELECT title FROM Stars. In, Movie. Star WHERE star. Name = name AND birthdate LIKE ‘%1960’; title starname=name AND birthdate LIKE ‘%1960’ star. Name=name birthdate LIKE ‘%1960’ Stars. In Movie. Star

And a better plan introducing a projection to filter out useless attributes: title star.

And a better plan introducing a projection to filter out useless attributes: title star. Name=name Stars. In birthdate LIKE ‘%1960’ Movie. Star