CS 255: Database System Principles slides: From Parse Trees to Logical Query Plans By:- Arunesh Joshi Id:-006538558.

Slides:



Advertisements
Similar presentations
Relational Calculus   Logic, like whiskey, loses its beneficial effect when taken in too large quantities. --Lord Dunsany.
Advertisements

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.
Database Modifications CIS 4301 Lecture Notes Lecture /30/2006.
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.
The Query Compiler Varun Sud ID: 104. Agenda Parsing  Syntax analysis and Parse Trees.  Grammar for a simple subset of SQL  Base Syntactic Categories.
Subqueries Example Find the name of the producer of ‘Star Wars’.
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.
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.
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.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide
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.
Joins Natural join is obtained by: R NATURAL JOIN S; Example SELECT * FROM MovieStar NATURAL JOIN MovieExec; Theta join is obtained by: R JOIN S ON Example.
The Query Compiler 16.1 Parsing and Preprocessing Meghna Jain(205) Dr. T. Y. Lin.
16.5 Introduction to Cost- based plan selection Amith KC Student Id: 109.
16.2 ALGEBRAIC LAWS FOR IMPROVING QUERY PLANS Ramya Karri ID: 206.
Summary of query compilers (Section16.8) Varun Gupta Department of Computer Science ID-216 CS 257.
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.
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.
CPS216: Advanced Database Systems Query Rewrite Rules for Subqueries Shivnath Babu.
SCUHolliday - COEN 17814–1 Schedule Today: u Query Processing overview.
Lecture 11: Query processing and optimization Jose M. Peña
Query Compiler A.Sri Harsha From Parse Trees to Logical Query Plans.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
Chapters 15-16a1 (Slides by Hector Garcia-Molina, Chapters 15 and 16: Query Processing.
© D. Wong Normalization  Purpose: process to eliminate redundancy in relations due to functional or multi-valued dependencies.  Decompose relation.
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 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)
CPSC-608 Database Systems Fall 2015 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #8.
Query Processing – Query Trees. Evaluation of SQL Conceptual order of evaluation – Cartesian product of all tables in from clause – Rows not satisfying.
Chapter 15 Algorithms for Query Processing and Optimization Copyright © 2004 Pearson Education, Inc.
The Query Compiler Parsing and Preprocessing. Meghna Jain(205)
The Relational Model of Data Prof. Yin-Fu Huang CSIE, NYUST Chapter 2.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Introduction to Query Processing (1) Query optimization: The process of choosing a suitable execution.
Chapter 13: Query Processing
Databases : SQL Multi-Relations 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman.
CS4432: Database Systems II Query Processing- Part 1 1.
Subqueries CIS 4301 Lecture Notes Lecture /23/2006.
1 Ullman et al. : Database System Principles Notes 6: Query Processing.
Compiler Design (40-414) Main Text Book:
Query Optimization Heuristic Optimization
Database System Implementation CSE 507
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
Query processing and optimization
16.2.Algebraic Laws for Improving Query Plans
Algebraic Laws.
Deletion in AVL Tree There are 5 cases to consider.
Query Compiler By:Payal Gupta Shirali Choksi Professor :Tsau Young Lin.
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Presentation transcript:

CS 255: Database System Principles slides: From Parse Trees to Logical Query Plans By:- Arunesh Joshi Id:

Agenda Conversion to Relational Algebra. Removing Sub queries From Conditions. Improving the Logical Query Plan. Grouping Associative/Commutative Operators.

Parsing Goal is to convert a text string containing a query into a parse tree data structure: – leaves form the text string (broken into lexical elements) – internal nodes are syntactic categories Uses standard algorithmic techniques from compilers – given a grammar for the language (e.g., SQL), process the string and build the tree

Two steps to turn Parse tree into Preferred Logical Query Plan Replace the nodes and structures of the parse tree, in appropriate groups, by an operator or operators of relational algebra. Take the relational algebra expression and turn it into an expression that we expect can be converted to the most efficient physical query plan.

Reference Relations StarsIn (movieTitle, movieYear, starName) MovieStar (name, address, gender, birthdate) Conversion to Relational Algebra If we have a with a that has no subqueries, then we may replace the entire construct – the select-list, from-list, and condition – by a relational-algebra expression.

Example: SQL query SELECT title FROM StarsIn WHERE starName IN ( SELECT name FROM MovieStar WHERE birthdate LIKE ‘%1960’ ); (Find the movies with stars born in 1960) Assume we have a simplified grammar for SQL.

Example: Parse Tree SELECT FROM WHERE IN title StarsIn ( ) starName LIKE name MovieStar birthDate ‘%1960’ SELECT FROM WHERE

The Preprocessor It replaces each reference to a view with a parse (sub)-tree that describes the view (i.e., a query) It does semantic checking: – are relations and views mentioned in the schema? – are attributes mentioned in the current scope? – are attribute types correct?

Convert Parse Tree to Relational Algebra The complete algorithm depends on specific grammar, which determines forms of the parse trees Here is a flavor of the approach

Conversion Suppose there are no subqueries. SELECT att-list FROM rel-list WHERE cond is converted into PROJ att-list (SELECT cond (PRODUCT(rel-list))), or  att-list (  cond ( X (rel-list)))

SELECT movieTitle FROM StarsIn, MovieStar WHERE starName = name AND birthdate LIKE '%1960'; SELECT FROM WHERE, AND movieTitle StarsIn LIKE MovieStar birthdate '%1960' = starName name

Equivalent Algebraic Expression Tree  movieTitle starname = name AND birthdate LIKE '%1960' X StarsIn MovieStar 

Handling Subqueries Recall the (equivalent) query: SELECT title FROM StarsIn WHERE starName IN ( SELECT name FROM MovieStar WHERE birthdate LIKE ‘%1960’ ); Use an intermediate format called two- argument selection

 title  StarsIn IN  name  birthdate LIKE ‘%1960’ starName MovieStar Example: Two-Argument Selection

Converting Two-Argument Selection To continue the conversion, we need rules for replacing two-argument selection with a relational algebra expression Different rules depending on the nature of the sub query Here is shown an example for IN operator and uncorrelated query (sub query computes a relation independent of the tuple being tested)

Rules for IN  R t IN S R  S  X C C is the condition that equates attributes in t with corresponding attributes in S

Example: Logical Query Plan  title  starName=name StarsIn  name  birthdate LIKE ‘%1960’ MovieStar 

What if Subquery is Correlated? Example is when subquery refers to the current tuple of the outer scope that is being tested More complicated to deal with, since subquery cannot be translated in isolation Need to incorporate external attributes in the translation Some details are in textbook

Improving the Logical Query Plan There are numerous algebraic laws concerning relational algebra operations By applying them to a logical query plan judiciously, we can get an equivalent query plan that can be executed more efficiently Next we'll survey some of these laws

Example: Improved Logical Query Plan  title starName=name StarsIn  name  birthdate LIKE ‘%1960’ MovieStar

Associative and Commutative Operations product natural join set and bag union set and bag intersection  associative: (A op B) op C = A op (B op C)  commutative: A op B = B op A

Laws Involving Selection Selections usually reduce the size of the relation Usually good to do selections early, i.e., "push them down the tree" Also can be helpful to break up a complex selection into parts

Selection Splitting  C1 AND C2 (R) =  C1 (  C2 (R))  C1 OR C2 (R) = (  C1 (R)) U set (  C2 (R)) if R is a set  C1 (  C2 (R)) =  C2 (  C1 (R))

Selection and Binary Operators Must push selection to both arguments: –  C (R U S) =  C (R) U  C (S) Must push to first arg, optional for 2nd: –  C (R - S) =  C (R) - S –  C (R - S) =  C (R) -  C (S) Push to at least one arg with all attributes mentioned in C: – product, natural join, theta join, intersection – e.g.,  C (R X S) =  C (R) X S, if R has all the atts in C

Pushing Selection Up the Tree Suppose we have relations – StarsIn(title,year,starName) – Movie(title,year,len,inColor,studioName) and a view – CREATE VIEW MoviesOf1996 AS SELECT * FROM Movie WHERE year = 1996; and the query – SELECT starName, studioName FROM MoviesOf1996 NATURAL JOIN StarsIn;

The Straightforward Tree  starName,studioName  year=1996 StarsIn Movie Remember the rule  C (R S) =  C (R) S ?

The Improved Logical Query Plan  starName,studioName  year=1996 StarsIn Movie  starName,studioName  year=1996 Movie StarsIn  starName,studioName  year=1996 Movie StarsIn push selection up tree push selection down tree

Grouping Associative/ Commutative Operators An operator that is associative and commutative operators may be though of as having any number of operands. We need to reorder these operands so that the multiway join is executed as sequence of binary joins. Its more time consuming to execute them in the order suggested by parse tree. For each portion of subtree that consists of nodes with the same associative and commutative operator (natural join, union, and intersection), we group the nodes with these operators into a single node with many children.

Grouping Assoc/Comm Operators Groups together adjacent joins, adjacent unions, and adjacent intersections as siblings in the tree Sets up the logical QP for future optimization when physical QP is constructed: determine best order for doing a sequence of joins (or unions or intersections) U D E F U U A BC DEF A B C