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

Slides:



Advertisements
Similar presentations
1 Query Evaluation Partially using Prof. Hector Garcia-Molina’s slides (Notes06, Notes07) Donghui Zhang Northeastern.
Advertisements

Relational Calculus   Logic, like whiskey, loses its beneficial effect when taken in too large quantities. --Lord Dunsany.
CS CS4432: Database Systems II Logical Plan Rewriting.
16.3 Parser to Logical Query Plans SQL(not RAE) Figure 16.2 select distinct movietitle from starsIn where starname in (select name from moviestar.
Algebraic and Logical Query Languages Spring 2011 Instructor: Hassan Khosravi.
Relational Operations on Bags Extended Operators of Relational Algebra.
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.
Query Compiler. The Query Compiler Parses SQL query into parse tree Transforms parse tree into expression tree (logical query plan) Transforms logical.
Cost-Based Transformations. Why estimate costs? Well, sometimes we don’t need cost estimations to decide applying some heuristic transformation. –E.g.
The Query Compiler Varun Sud ID: 104. Agenda Parsing  Syntax analysis and Parse Trees.  Grammar for a simple subset of SQL  Base Syntactic Categories.
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
CMPT 454, Simon Fraser University, Fall 2009, Martin Ester 183 Database Systems II Query Compiler.
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.
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.
SQL. 1.SQL is a high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details that would be necessary in.
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.
Relational Operations on Bags Extended Operators of Relational Algebra.
SQL SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details that would be necessary in.
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 Basic Operations Algebra of Bags.
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:
Database Management 9. course. Execution of queries.
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.
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.
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.
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.
1 Algebra of Queries Classical Relational Algebra It is a collection of operations on relations. Each operation takes one or two relations as its operand(s)
CS 245Notes 61 CS 245: Database System Principles Notes 6: Query Processing Hector Garcia-Molina.
The Relational Model of Data Prof. Yin-Fu Huang CSIE, NYUST Chapter 2.
CS4432: Database Systems II Query Processing- Part 1 1.
1. Chapter 2: The relational Database Modeling Section 2.4: An algebraic Query Language Chapter 5: Algebraic and logical Query Languages Section 5.1:
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.
Deletion in AVL Tree There are 5 cases to consider.
CPSC-608 Database Systems
Query Compiler By:Payal Gupta Shirali Choksi Professor :Tsau Young Lin.
Presentation transcript:

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 ::= A toms(constants), (variable), ::= (can be expressed/defined as)

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

Another query equivalent 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

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 (  ) For intersection, the selection is required to be pushed to one argument. What about intersection?

If all attributes in the condition C are in R (for binary operators)

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)

Some Trivial Laws Watch for some extreme cases: –an empty relation: e.g., R  S = S, if R =  –a selection or theta-join whose condition is always satisfied e.g.,  C (R) = R, if C = true –a projection on all attributes is “better” not to be done at all!!

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 join attr. Projections cannot be pushed below  S, or either set/bag versions of  and – Example: Consider R(A,B) and S(A,C). Supp. R = {(1,2)} and S = {(1,3)}.   A (R  S) =  A (  ) but  A (R)   A (S) = {(1)}

Example Schema : StarsIn(title,year,starName) Query: SELECT starName FROM StarsIn WHERE year = 1996; Should we can transform to   starName  year=1996 StarsIn  starName  year=1996 StarsIn  starName,year