Algebraic Laws.

Slides:



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

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.
CS4432: Database Systems II Query Operator & Algebraic Expressions 1.
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.
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.
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.
Cost-Based Transformations. Why estimate costs? Sometimes we don’t need cost estimations to decide applying some heuristic transformation. –E.g. Pushing.
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.
Query Optimization. General Overview Relational model - SQL  Formal & commercial query languages Functional Dependencies Normalization Physical Design.
CS 4432logical query rewriting - lecture 151 CS4432: Database Systems II Lecture #15 Logical Query Rewriting Professor Elke A. Rundensteiner.
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.
Cs3431 Relational Algebra : #I Based on Chapter 2.4 & 5.1.
Algebraic Laws. {P1,P2,…..} {P1,C1>...} parse convert apply laws estimate result sizes consider physical plans estimate costs pick best execute Pi answer.
Nov 18, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11.
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.
Murali Mani Relational Algebra. Murali Mani What is Relational Algebra? Defines operations (data retrieval) for relational model SQL’s DML (Data Manipulation.
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:
Cost based transformations Initial logical query plan Two candidates for the best logical query plan.
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.
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.
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.
More Relation Operations 2015, Fall Pusan National University Ki-Joune Li.
More Relation Operations 2014, Fall Pusan National University Ki-Joune Li.
Relational Algebra Instructor: Mohamed Eltabakh 1 Part II.
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.
CS4432: Database Systems II Query Processing- Part 1 1.
Subqueries CIS 4301 Lecture Notes Lecture /23/2006.
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
The Query Compiler Parsing and Preprocessing. Meghna Jain(205)
CS 245: Database System Principles
Focus: Relational System
More Relation Operations
16.2.Algebraic Laws for Improving Query Plans
CS 245: Database System Principles
More Complex Operators
Lecture 33: The Relational Model 2
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:

Algebraic Laws

consider physical plans {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

Algebraic laws for improving query plans Commutative and associative laws: Proofs of the above identities? Above laws are applicable for sets and bags

Theta-join Commutative: Not always associative: On schema R(a,b), S(b,c), T(c,d) can not be transformed into:

Laws Involving Selection ()

If all attributes in the condition C are in R (for binary operators) Note that in union R and S have to have the same attributes.

Example: Consider relation schemas R(A,B) and S(B,C) and the RA 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 Cond to R  A=1 OR A=3 (R) ⊳⊲ B < C(S) 4. Splitting OR  (A=1 (R) A=3(R)) ⊳⊲ B < C(S) Step 4 above may or may not be useful.

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 output attr. and any join attribute.

Laws for (bag) Projection … Bag projections cannot be pushed below ⋃S, and no projection can be pushed below either set/bag versions of ⋂ and – Example: Consider relation schemas R(A,B) and S(A,B). Supp. R has only (1,2) and S has only (1,3).  A(R ⋂ S) = A(∅) but A(R) ⋂ A(S) = {(1)}

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; starName year=1996 StarsIn starName,year starName year=1996 StarsIn Should we transform to  ? Depends!

Reasons for not pushing the projection If there is an index on StarsIn.year, 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, grouping, and aggregations Two general rules: ( absorbs ) , L ⊆ M Note: (R)=R, if R has no duplicates (e.g., base relation or result of a grouping) Defn: L is duplicate-impervious if it is not affected by duplicates, i.e., MIN or MAX. In this case,

Example Schema: StarsIn(title,year,starName) MovieStar(name,address,gender,birthdate) Query: Find for each year, the birth date of the youngest star to appear in a movie in that year.

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) Remove or move  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 StarsIn MovieStar starName=name birthdate LIKE ‘%1960’ title starname=name AND birthdate LIKE ‘%1960’ StarsIn  MovieStar

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