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

Slides:



Advertisements
Similar presentations
CS CS4432: Database Systems II Logical Plan Rewriting.
Advertisements

Algebraic and Logical Query Languages Spring 2011 Instructor: Hassan Khosravi.
Query Evaluation. An SQL query and its RA equiv. Employees (sin INT, ename VARCHAR(20), rating INT, age REAL) Maintenances (sin INT, planeId INT, day.
Query Execution Since our SQL queries are very high level the query processor does a lot of processing to supply all the details. An SQL query is translated.
Cost-Based Transformations. Why estimate costs? Well, sometimes we don’t need cost estimations to decide applying some heuristic transformation. –E.g.
Query Execution Optimizing Performance. Resolving an SQL query Since our SQL queries are very high level, the query processor must do a lot of additional.
COMP 451/651 Optimizing Performance
The Query Compiler Parses SQL query into parse tree Transforms parse tree into expression tree (logical query plan) Transforms logical query plan into.
Algebraic Laws For the binary operators, we push the selection only if all attributes in the condition C are in R.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
Query Compiler By:Payal Gupta Roll No:106(225) Professor :Tsau Young Lin.
16.2.Algebraic Laws for Improving Query Plans Algebraic Laws for Improving Query Plans Commutative and Associative Laws Laws Involving.
Relational Algebra on Bags A bag is like a set, but an element may appear more than once. –Multiset is another name for “bag.” Example: {1,2,1,3} is a.
CS263 Lecture 19 Query Optimisation.  Motivation for Query Optimisation  Phases of Query Processing  Query Trees  RA Transformation Rules  Heuristic.
The Query Compiler Section 16.3 DATABASE SYSTEMS – The Complete Book Presented By:Under the supervision of: Deepti KunduDr. T.Y.Lin.
1 Query Compilation Parsing Logical Query Plan Source: our textbook, slides by Hector Garcia-Molina.
Cost based transformations Initial logical query plan Two candidates for the best logical query plan.
CS Spring 2002Notes 61 CS 277: Database System Implementation Notes 6: Query Processing Arthur Keller.
16.2.Algebraic Laws for Improving Query Plans Algebraic Laws for Improving Query Plans Commutative and Associative Laws Laws Involving.
CS 4432query processing1 CS4432: Database Systems II.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
Relational Operations on Bags Extended Operators of Relational Algebra.
Query Optimization. General Overview Relational model - SQL  Formal & commercial query languages Functional Dependencies Normalization Physical Design.
The Query Compiler 16.1 Parsing and Preprocessing Meghna Jain(205) Dr. T. Y. Lin.
16.2 ALGEBRAIC LAWS FOR IMPROVING QUERY PLANS Ramya Karri ID: 206.
Algebraic Laws 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.
Algebraic Laws. {P1,P2,…..} {P1,C1>...} parse convert apply laws estimate result sizes consider physical plans estimate costs pick best execute Pi answer.
T HE Q UERY C OMPILER Prepared by : Ankit Patel (226)
16.2.Algebraic Laws for Improving Query Plans Algebraic Laws for Improving Query Plans Commutative and Associative Laws Laws Involving.
CS 4432query processing - lecture 121 CS4432: Database Systems II Lecture #12 Query Processing Professor Elke A. Rundensteiner.
CS 255: Database System Principles slides: From Parse Trees to Logical Query Plans By:- Arunesh Joshi Id:
Relational Algebra CIS 4301 Lecture Notes Lecture /28/2006.
CS 255: Database System Principles slides: From Parse Trees to Logical Query Plans By:- Arunesh Joshi Id:
Advanced Databases: Lecture 8 Query Optimization (III) 1 Query Optimization Advanced Databases By Dr. Akhtar Ali.
Advanced Database Systems Notes:Query Processing (Overview) Shivnath Babu.
Cost based transformations Initial logical query plan Two candidates for the best logical query plan.
Database Management 9. course. Execution of queries.
DBMS 2001Notes 6: Query Compilation1 Principles of Database Management Systems 6: Query Compilation and Optimization Pekka Kilpeläinen (partially based.
CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites) Shivnath Babu.
CS 245Notes 61 CS 245: Database System Principles Notes 6: Query Processing Hector Garcia-Molina.
CS 245Notes 61 CS 245: Database System Principles Notes 6: Query Processing Hector Garcia-Molina.
Introduction to Data Manipulation in SQL CIS 4301 Lecture Notes Lecture /03/2006.
CPS216: Advanced Database Systems Query Rewrite Rules for Subqueries Shivnath Babu.
SCUHolliday - COEN 17814–1 Schedule Today: u Query Processing overview.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra.
CPS216: Data-Intensive Computing Systems Introduction to Query Processing Shivnath Babu.
Lecture 11: Query processing and optimization Jose M. Peña
Query Compiler A.Sri Harsha From Parse Trees to Logical Query Plans.
Chapters 15-16a1 (Slides by Hector Garcia-Molina, Chapters 15 and 16: Query Processing.
Databases : Relational Algebra - Complex Expression 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof.
More Relation Operations 2015, Fall Pusan National University Ki-Joune Li.
More Relation Operations 2014, Fall Pusan National University Ki-Joune Li.
CS 245Notes 61 CS 245: Database System Principles Notes 6: Query Processing Hector Garcia-Molina.
Relational Algebra p BIT DBMS II.
Chapter 13: Query Processing
CS4432: Database Systems II Query Processing- Part 1 1.
CPS216: Advanced Database Systems Notes 02:Query Processing (Overview) Shivnath Babu.
1 Ullman et al. : Database System Principles Notes 6: Query Processing.
16.2.Algebraic Laws for Improving Query Plans
Prepared by : Ankit Patel (226)
The Query Compiler Parsing and Preprocessing. Meghna Jain(205)
Focus: Relational System
More Relation Operations
16.2.Algebraic Laws for Improving Query Plans
CS 245: Database System Principles
Algebraic Laws.
CPSC-608 Database Systems
CPSC-608 Database Systems
Query Compiler By:Payal Gupta Shirali Choksi Professor :Tsau Young Lin.
CPS216: Data-Intensive Computing Systems Query Processing (Overview)
Presentation transcript:

Query Compiler

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

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

Grammar for simple SQL ::= ::= ( ) ::= SELECT FROM WHERE ::=, ::= ::=, ::= ::= AND ::= IN ::= = ::= LIKE ::= Atoms(constants), (variable), ::= (can be expressed/defined as)

Query StarsIn(title,year,starName) MovieStar(name,address,gender,birthdate) Query: Give titles of movies that have at least one star born in 1960 SELECT title FROM StarsIn, MovieStar WHERE starName = name AND birthdate LIKE '%1960%' ;

Parse Tree SELECT FROM WHERE, AND title StarsIn = LIKE starName name birthdate ‘%1960’ MovieStar

Another query equivalent SELECT title FROM StarsIn WHERE starName IN ( SELECT name FROM MovieStar WHERE birthdate LIKE '%1960%' );

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 are applicable for both sets and bags

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 Only if R is a set. The union is “set union” Order is flexible

Laws Involving Selection (  ) What about intersection?

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:  (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 example shows that it is sometimes useful to pull selection up in the tree. StarsIn(title,year,starName) Movie(title,year,length,studioName) CREATE VIEW MoviesOf1996 AS SELECT * FROM MOVIE WHERE year=1996; Query: Which stars worked for which studios in 1996? SELECT starName,studioName FROM MoviesOf1996 NATURAL JOIN StarsIN;

pull selection up then push down

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)

Example: Pushing Projection Schema : StarsIn(title,year,starName) Query: SELECT starName FROM StarsIn WHERE year = 1996; Should we transform to  ? Depends! Is StarsIn stored or computed ?  starName  year=1996 StarsIn  starName  year=1996 StarsIn  starName,year

Reasons for not pushing the projection If StarsIn 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 StarsIn, such index is useless in the projected relation  starName,year (StarsIn) –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 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 up first). Do splitting of complex conditions in  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 StarsIn, MovieStar WHERE starName = name AND birthdate LIKE ‘%1960’;  title  starname=name AND birthdate LIKE ‘%1960’ StarsIn  MovieStar  title StarsIn MovieStar starName=name  birthdate LIKE ‘%1960’

 title StarsIn MovieStar starName=name  birthdate LIKE ‘%1960’  name And a better plan introducing a projection to filter out useless attributes: