16.2.Algebraic Laws for Improving Query Plans

Slides:



Advertisements
Similar presentations
16.4 Estimating the Cost of Operations Project GuidePrepared By Dr. T. Y. LinVinayan Verenkar Computer Science Dept San Jose State University.
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.
Algebraic and Logical Query Languages Spring 2011 Instructor: Hassan Khosravi.
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.
Subqueries Example Find the name of the producer of ‘Star Wars’.
Indexes. An index on an attribute A of a relation is a data structure that makes it efficient to find those tuples that have a fixed value for attribute.
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.
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.
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.
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.
Chapter 5 Algebraic and Logical Query Languages pp.54 is added Pp 61 updated.
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.
16.2.Algebraic Laws for Improving Query Plans Algebraic Laws for Improving Query Plans Commutative and Associative Laws Laws Involving.
SQL By: Toan Nguyen. Download Download the software at During the installation –Skip sign up for fast installation.
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:
CPS216: Advanced Database Systems Notes 08:Query Optimization (Plan Space, Query Rewrites) Shivnath Babu.
Introduction to Data Manipulation in SQL CIS 4301 Lecture Notes Lecture /03/2006.
Introduction to Indexes. Indexes An index on an attribute A of a relation is a data structure that makes it efficient to find those tuples that have a.
CPS216: Advanced Database Systems Query Rewrite Rules for Subqueries Shivnath Babu.
Query Compiler A.Sri Harsha From Parse Trees to Logical Query Plans.
© 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 2015, Fall Pusan National University Ki-Joune Li.
More Relation Operations 2014, Fall Pusan National University Ki-Joune Li.
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.
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)
SQL Exercises – Part I April
The Relational Model of Data Prof. Yin-Fu Huang CSIE, NYUST Chapter 2.
Subqueries CIS 4301 Lecture Notes Lecture /23/2006.
1 Ullman et al. : Database System Principles Notes 6: Query Processing.
Basic Operations Algebra of Bags
Chap 5. The DB Language (SQL)
Relational Algebra - Part 1
16.2.Algebraic Laws for Improving Query Plans
CS257 Query Optimization.
Introduction to Structured Query Language (SQL)
THE RELATIONAL MODEL OF DATA
CS157B Query Optimization.
The Query Compiler Parsing and Preprocessing. Meghna Jain(205)
Operators Expression Trees Bag Model of Data
Relational Algebra : #I
Focus: Relational System
More Relation Operations
Basic Operations Algebra of Bags
Algebraic Laws.
Algebraic and Logical Query Languages pp.54 is added
Chapter 2: Intro to Relational Model
CPSC-608 Database Systems
5.1 Relational Operations on Bags
CPSC-608 Database Systems
16.3 Parser to Logical Query Plans
Query Compiler By:Payal Gupta Shirali Choksi Professor :Tsau Young Lin.
Presentation transcript:

16.2.Algebraic Laws for Improving Query Plans Skip 16.1 1

16.2 Algebraic Laws for Improving Query Plans 16.2.1 Commutative and Associative Laws 16.2.2 Laws Involving Selection 16.2.3 Pushing Selections 16.2.4 Laws Involving Projection 16.2.5 Laws About Joins and Products 16.2.6 Laws Involving Duplicate Elimination 16.2.7 Laws Involving Grouping and Aggregation 16.2.8 Exercises for Section 16.2

16.2.1 Commutative and Associative Laws  

16.2.2 Laws Involving Selection Selections reduce the size of relations. To make efficient query, the selection must be moved down the tree without the changing what the expression does. When the condition for the selection is complex, it helps to break the condition into its constituent parts.

16.2.2 Laws Involving Selection first two laws for σ are the splitting laws, σc1 AND c2 (R) = σc1(σc2(R)) σc1 OR c2 (R) = (σc1(R)) s (σc2(R)) The second law for OR works only if the relation R is the set .If R is a bag, then the set union Us will eliminate the duplicates incorrectly. σc1(σc2(R))= σc2(σc1(R))

16.2.2 Laws Involving Selection Laws of selection with binary operators like product, union, intersection, difference, join. (3 laws) For a union, the selection must be pushed to both arguments. σc (R U S) = σc (R) U σc (S) For a difference, the selection must be pushed to first argument and optionally to second. σc (R - S) = σc (R) – S σc (R - S) = σc (R) - σc (S) it is only required that the selection must be pushed to one or both argument. σc(R x S) = σc (R) x S σc (R S) = σ (R)  S σc (RD S) = σ (R)  D S σc ( R ∩ S) = σc (R) ∩ S

16.2.2 Laws Involving Selection Laws of selection with binary operators like product, union, intersection, 3. it is only required that the selection must be pushed to one or both argument. σc(R  S) = R  σc (S) σc (R S) = σc (R)  σc(S)

16.2.3 Pushing Selections Pushing Selection down the expression tree( i.e replacing the left side of one of the rules by the right side )is one of the best method to optimize query (Distributive Laws). An example for Pushing Selection is illustrated as follows

16.2.3 Pushing Selections Suppose we have relations StarsIn(title ,year , starName) Movie(title ,year, length,inColor, studioName) We Define a view(copy of relation)Movies1996 as CREATE VIEW Movie1996 AS SELECT * FROM MOVIE

16.2.3 Pushing Selections The query to find out which stars worked in which studios in 2009 SELECT starname ,studioName FROM Movie2009 NATURAL JOIN StarsIn; The view is Movie 2009 is defined by σ year = 2009 (Movie)

16.2.4 Laws Involving Projection Projection, like selection can be pushed down through many other operators Pushing Projection usually involves introducing a new projection somewhere below an existing projection. Projection differs from selection in the aspect that projection reduces the length of the tuples whereas selection reduces the number of the tuples

Select MS.studioname, MS.starname From (Select M.title, M.year, M.length, M.inColor, M.studioname, M.producerC#, S.Starname (Select * From Movies where year=2009)M, (Select * From starsIn where Movieyear=2009)S where M.year= S.movieyear and S.movietitle=M.title) MS ;

Whole Tree SELECT MSN.STUDIONAME,MSN.STARNAME From (Select * From (SELECT M.title, M.year, M.length, M.incolor, M.studioname, M.producerC#, S.starname FROM Movies M, starsIn S Where M.year= S.movieyear and M.Title=S.movietitle) MS Where MS.year =2009 )MSN;

16.2.3a Pushing Selections π starName ,studioName σ year = 1996 StarsIn Movie π starName ,studioName (σ year = 1996(Movie)  StarsIn )= π starName ,studioName (σ year = 1996(Movie  StarsIn )

16.2.3b Pushing Selections π starName ,studioName σ year = 1996 StarsIn Movie π starName ,studioName (σ year = 1996(Movie)  StarsIn )= π starName ,studioName (σ year = 1996(Movie  StarsIn )

16.2.3c Pushing Selections starName ,studioName StarsIn π σ year = 1996 σ year = 1996 Movie StarsIn π starName ,studioName (σ year = 1996(Movie  StarsIn )= π starName ,studioName (σ year = 1996(Movie)  σ year = 1996(StarsIn ))

Projection: L(R) Select L from R

16.2.4. Laws involving Projection Consider term π E  x E : attribute, or expression involving attributes and constants. All attributes in E are input attributes of projection and x is output attribute Simple projection: if a projection consists of only attributes. Example: π a,b,c (R) is simple. a,b,c are input and output attributes. Projection can be introduced anywhere in expression tree as long as it only eliminates attributes that are never used.

16.2.4. Laws involving Projection (cont….) πL(R S) = πL(πM(R) πN(S)) ; M and N are all attributes of R and S that are either join (in schema of both R and S) or input attributes of L πL(R c S) = πL(πM(R) c πN(S)) ; M and N are all attributes of R and S that are either join(mentioned in condition of C ) or input attributes of L πL(R x S) = πL(πM(R) x πN(S)) ; M and N are all attributes of R and S that are input attributes of L Projections cannot be pushed below set unions or either of set or bag versions of intersection or difference at all.

16.2.4 Laws Involving Projection SELECT starName FROM StarsIn WHERE year = 1996 π starName σ movieYear = 1996 StarsIn Fig : Logical query plan for the above query We can introduce a projection in the above Figure

16.2.4 Laws Involving Projection π starName σ movieYear = 1996 π starName, movieYear StarsIn Convert the tree into relational algebra, then simplify as much as you can (Watch out when you drop primary keys)

16.2.5 Laws About Joins and Products RCS= C(R S) RS= L (C(R S)) Where C is the condition that equates each pair of atrribute from R and S with the same name, and L is the list that includes one attribute from each equted attributed and all other attributes of R and S.

16.2.6 Laws Involving Duplicate Elimination The operator δ , which eliminates duplicates from a bag can be pushed through only some of the operators Moving δ down the tree reduces the size of intermediate relation and may therefore be beneficial In some cases, we can move δ to a position where it can be eliminated because it is applied to a relation that does not have any duplicates

16.2.6 Laws Involving Duplicate Elimination δ( R ) = R if R has no duplicates Important cases of such a relation R include 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 δ cannot be moved across the operators like U , - , π.

16.2.6 Laws involving duplicate elimination Laws that “push” δ (delta) through other operator δ(R x S) = δ(R) x δ(S) δ(R S) = δ(R) δ(S) δ(R c S) = δ(R) c δ(S) δ( c(R)) = c(δ(R)) δ eliminates duplicates from a bag, but cannot be pushed through all the operators

16.2.7 Laws Involving Grouping and Aggregation While using grouping and aggregation ,the applicability of many transformation depends on the details of the aggregation used. Due to the above ,we cannot state laws in generality. One exception is the law below that γ absorbs δ δ(γL(R)) = γL ( R )

16.2.7 Laws Involving Grouping and Aggregation We may project useless attributes prior to applying γ operation γL ( R ) = γL(πM (R ) where M is the list containing at least all those attributes of R that are mentioned in L.

Laws involving grouping and aggregation (cont…) Some aggregations like MIN and MAX are not affected by presence or absence of duplicates Others like SUM,COUNT,AVG produce different values if duplicates are eliminated prior to aggregation.

16.2.7 Laws Involving Grouping and Aggregation Suppose we have the relation MovieStar(name ,addr, gender ,birthdate) StarsIn(movieTitle ,movieYear ,starName) Consider the query below Select movieYear ,MAX(birthDate) FROM MovieStar ,StarsIn WHERE name = starName GROUP BY movieYear Comment: (Blue  RED)

16.2.7 Laws Involving Grouping and Aggregation The FROM list is expressed by a product and the WHERE clause by a selection above it. The grouping and aggregation are expressed by the γ. Combine the selection and product into an equijoin Generate a δ below the γ ,since the γ is duplicate-impervious Generate a π between the γ and the introduced δ to project onto movieYear and birthDate ,the only attributes relevant to the γ

16.2.7 Laws Involving Grouping and Aggregation γmovieYear ,MAX(birthDate) σname = starName  MovieStar StarsIn 1. Use 16.2.5. (and following 2 reasons) we can rewrite the tree 2. There is no duplication in output (because γ), we can add  3. By projection law We can add .

Laws involving grouping and aggregation (cont…) Write SQL (Due 3/12) And Query Algebra For previous slide

16.2.7 Laws Involving Grouping and Aggregation γmovieYear ,MAX(birthDate) movieYear ,birthDate δ name = starName MovieStar StarsIn Figure : Second query plan

16.2.7 Laws Involving Grouping and Aggregation γ movieYear ,MAX(birthDate) π movieYear ,birthDate name = starName δ δ πbirthDate,name πbirthDate,name MovieStar StarsIn Figure : Third query plan  can be push down

16.2.7a Additional Example From DB1

16.2.7a Laws Involving Grouping and Aggregation SELECT P.PNAME, SUM(S.QTY) FROM SHIPMENTs S, Parts P WHERE P.PNUM=S.PNUM GROUP BY P.PNAME;

16.2.7b Laws Involving Grouping and Aggregation γpname ,SUM(qty) sum σpnum = pnum  Shipments (Sh) Parts(P) Figure : Initial Logical query plan for the query γpname ,SUM(qty)  sum(σsh.pnum=p.pnum (ShipmentsParts))

16.2.7c Laws Involving Grouping and Aggregation γpname ,SUM(qty)sum pname ,QTY δ Shipments Parts γpname ,SUM(qty)sum ( pname.qty (Shipments Parts))

16.2.7d Laws Involving Grouping and Aggregation γpname ,SUM(qty)sum π pname ,qty δ δ πqty,pnum pnum, pname Shipments Parts γpname ,Sum(qty) sum ( pname.qty ((  pname.qty (Shipments)) (  pname.qty (Parts))))

16.2.8 Exercises for Section 16.2 Thanks

A B 1 2 3 4 Relation R X S A R.B S.B C D 1 2 5 6 4 7 8 9 10 11 3 Relation R B C D 2 5 6 4 7 8 9 10 11 Relation S

A B 1 2 3 4 Relation R X S A R.B S.B C D 1 2 5 6 4 7 8 9 10 11 3   A B 1 2 3 4 Relation R X S A R.B S.B C D 1 2 5 6 4 7 8 9 10 11 3 Relation R B C D 2 5 6 4 7 8 9 10 11 Relation S

Quiz2/19 Convert the SQL into Query Algebra Select MS.studioname, MS.starname From (Select M.title, M.year, M.length, M.inColor, M.studioname, M.producerC#, S.Starname (Select * From Movies where year=2009)M, (Select * From starsIn where Movieyear=2009)S where M.year= S.movieyear and S.movietitle=M.title) MS ;