CS257 Query Optimization.

Slides:



Advertisements
Similar presentations
SQL, RA, Sets, Bags Fundamental difference between theoretical RA and practical application of it in DBMSs and SQL RA  uses sets SQL  uses bags (multisets)
Advertisements

Tallahassee, Florida, 2014 COP4710 Database Systems Relational Algebra Fall 2014.
Relational Algebra Ch. 7.4 – 7.6 John Ortiz. Lecture 4Relational Algebra2 Relational Query Languages  Query languages: allow manipulation and retrieval.
CS4432: Database Systems II Query Operator & Algebraic Expressions 1.
16.2.Algebraic Laws for Improving Query Plans Algebraic Laws for Improving Query Plans Commutative and Associative Laws Laws Involving.
Oct 28, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11.
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.
CS 4432query processing1 CS4432: Database Systems II.
Relational Algebra Chapter 4 - part I. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
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.
Instructor: Mohamed Eltabakh
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.
Murali Mani Relational Algebra. Murali Mani What is Relational Algebra? Defines operations (data retrieval) for relational model SQL’s DML (Data Manipulation.
Midterm 1 Concepts Relational Algebra (DB4) SQL Querying and updating (DB5) Constraints and Triggers (DB11) Unified Modeling Language (DB9) Relational.
Relational Algebra Basic Operations Algebra of Bags.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
Relational Algebra - Chapter (7th ed )
RELATIONAL ALGEBRA CHAPTER 6 1. LECTURE OUTLINE  Unary Relational Operations: SELECT and PROJECT  Relational Algebra Operations from Set Theory  Binary.
From Relational Algebra to SQL CS 157B Enrique Tang.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Advanced Relational Algebra & SQL (Part1 )
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.
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)
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
Relational Algebra Instructor: Mohamed Eltabakh 1.
1 Introduction to Database Systems, CS420 Relational Algebra.
1 Database Design: DBS CB, 2 nd Edition Relational Algebra: Basic Operations & Algebra of Bags Ch. 5.
1. Chapter 2: The relational Database Modeling Section 2.4: An algebraic Query Language Chapter 5: Algebraic and logical Query Languages Section 5.1:
CPS216: Advanced Database Systems Notes 02:Query Processing (Overview) Shivnath Babu.
CSE202 Database Management Systems
Summary of Relational Algebra
Basic Operations Algebra of Bags
Query Optimization Heuristic Optimization
CPSC-310 Database Systems
Module 2: Intro to Relational Model
COP4710 Database Systems Relational Algebra.
Chapter # 6 The Relational Algebra and Calculus
Fundamental of Database Systems
An Algebraic Query Language
Relational Algebra - Part 1
16.2.Algebraic Laws for Improving Query Plans
CS157B Query Optimization.
Chapter 2: Intro to Relational Model
Relational Algebra Chapter 4, Part A
Chapter 15 QUERY EXECUTION.
Relational Algebra 461 The slides for this text are organized into chapters. This lecture covers relational algebra, from Chapter 4. The relational calculus.
Operators Expression Trees Bag Model of Data
April 6th – relational algebra
The Relational Algebra and Relational Calculus
Relational Algebra Chapter 4 - part I.
Relational Algebra : #I
CS 3630 Database Design and Implementation
Instructor: Mohamed Eltabakh
Relational Algebra Chapter 4, Sections 4.1 – 4.2
16.2.Algebraic Laws for Improving Query Plans
Basic Operations Algebra of Bags
More Complex Operators
Chapter 2: Intro to Relational Model
The Relational Algebra
Chapter 2: Intro to Relational Model
Example of a Relation attributes (or columns) tuples (or rows)
Chapter 2: Intro to Relational Model
5.1 Relational Operations on Bags
CPSC-608 Database Systems
Query Compiler By:Payal Gupta Shirali Choksi Professor :Tsau Young Lin.
Relational Algebra Chapter 4 - part I.
Presentation transcript:

CS257 Query Optimization

2.4 An Algebraic Query Language

2.4 An Algebraic Query Language 2.4.1 Why Do We Need a Special Query Language? 2.4.2 What is an Algebra? 2.4.3 Overview of Relational Algebra 2.4.4 Set Operations on Relations 2.4.5 Projection 2.4.6 Selection 2.4.7 Cartesian Product 2.4.8 Natural Joins 2.4.9 Theta-Joins 2.4.10 Combining Operations to Form Queries 2.4.11 Naming and Renaming 2.4.12 Relationships Among Operations 2.4.13 A Linear Notation for Algebraic Expressions 2.4.14 Exercises for Section 2.4

2.4.1 Why Do We Need a Special Query Language?

2.4.2 What is an Algebra? + - * / Projection Selection Natural Join Cartesian Product Union Intersect Minus

2.4.3 Overview of Relational Algebra Both numerical algebra and relational algebra are defined by (respective sets of) Algebraic Laws.

2.4.3 Overview of Relational Algebra Algebraic laws of Numbers that we have learned in calculus, algebra and etc since high school days

2.4.3 Overview of Relational Algebra Algebraic laws of relational algebra are “New” to most of us

2.4.3 Overview of Relational Algebra Special Relational Operators Projection Selection Natural Join Traditional Set Operators 4. Cartesian Product 5. Union 6. Intersect 7. Minus

QUERY OPTIMIZATION In query optimization the query is transformed by compiler into such a form that can solve the problem “fastest ”

QUERY OPTIMIZATION Illustration: 2 * 3 + 5 * 3 From the laws of Numerical algebra, it can be computed as follows:. Method I - It computes in three operations 2 * 3 + 5 * 3 = 6 (first) + 15(second) = 21 (third)

QUERY OPTIMIZATION Illustration: 2 * 3 + 5 * 3 From the laws of Numerical algebra, it can Also be computed as follows:. Method II- transform into equivalent one. = (2 + 5) * 3 = 7 (first) * 3 = 21 (second) It computes in two operations So compiler choose Method II

Roles of Relational Algebra QUERY OPTIMIZATION Similar Idea is used in Relational Algebra So we need to know the Algebraic Laws of Relational Algebra well That is the main goal for Ch5 and part of Ch16

2.4.4 Set Operations on Relations Recall Venn Diagram Y X b c d e a

Query Algebra In DBMS a query is turn into a sequence of operators on U These operators forms the query algebra (Extended RA)

Query Algebra RS  SUM (# of times in R, - - - - - - S) Bag operations: RS  SUM (# of times in R, - - - - - - S) R∩S  Min (# of times in R, - - - - - - S)

Examples Two bags: R = {A, B, B} S = {C, A, B, C} R  S ={A, A, B, B, B, C, C} R ∩ S = {A, B} R—S ={B} (Obvious?)

Selection: C(R) from R where C C can involve computable formulas SQL: Select * from R where C C can involve computable formulas

Projection: L(R) Select L from R

Projection: L(R) L can have : 1. A single attribute of R. 2. An expression x  y: It means we take the attribute x of R and rename it as y. 3. An expression E  z where E is an expression involving attributes of R and z is a new name for the attribute that results from the calculation . Ex: a+b  x

Theta Join:  Theta Join: R c S = σc(RS) R c S = πL(σc(RS)) Where Natural Join (Special Case of Theta Join) R c S = πL(σc(RS)) Where L: The list that meets the condition c of equality and redundant attributes are dropped

SORTING OPERATOR ()  L (R) This is the SQL ORDER BY clause and denoted by the operator   L (R) where R is a relation and L a list of some of R’s attributes in the relation R but with the tuples of R sorted in the order indicated by L. If L is a1, a2…an, then tuples are first sorted by a1, then a2 until an. By default sorting is in ascending order.

Grouping and Aggregation: L(R) Select L(=A,B) from R group by A A: aggregating attribute B: aggregated attributes

Grouping and Aggregation: L(R) It returns a relation that partitions the tuples of R in to groups. Each group consists of all tuples having one particular assignment of values to the grouping attributes A in L. L also contains B, Aggregated attributes, in the form: Aggregation operator  Name

Grouping and Aggregation Aggregation operators : AVG, SUM, COUNT, MIN, MAX Grouping: GROUP BY clause in SQL Having clause must follow a GROUP BY clause

Grouping and Aggregation Grouping and aggregation are generally implemented together. So we have a single operator defining it It is a generalized Projection Operator Delicate-elimination operator  is a special Aggregation operator.

 pnum, sum(qty)sum(SP) Select pnum, sum(qty) as sum from SP group by pnum;

Some rules on selection: 1.σc1 and c2(R) = σc1(σc2 (R) ) 2.σc1 or c2(R) = (σc1R) s (σc2 R) whenever c apply to R or S

Some Rules about Selection: 3.σc(R  S) = (σcR )  (σc S) 4.σc(R  S) = (σcR)  (σc S) 5.σc(R  S) = (σcR)  (σcS) whenever c apply to R or S

Rules about (generalized) Projection: 6. We may introduce a (generalized) projection anywhere in an expression tree, as long as it eliminates only attributes that are never used by any operator above

Some Rules about Projection: 7. L(R c S ) =L(M(R) c N(R)) 8. L(R  S ) =L(R) L(S)

Query Optimization Example Select p.pname, p.pnum, sum(sp.qty) as sum from Parts p, Shipments sp where p.pnum = sp.pnum and p.weight > 10 group by p.pname, p.pnum having sum(sp.qty) >= 200;

Translating to Query Algebra Step 1 (P  SP) Step 2σP.PNUM = SP.PNUM and P.WEIGHT >10(PSP) =  Step 3σSUM >= 200 (γP.PNAME, P. PNUM, SUM(SP.QTY) → SUM ())=,  Step 4 πP.PNAME, P. PNUM, SUM () = 

P SP πP.PNAME, P. PNUM, SUM σSUM >= 200  σSUM >= 200 γP.PNAME, P. PNUM, SUM(SP.QTY) → SUM σP.PNUM = SP.PNUM and P.WEIGHT >10 P SP

Computing in QA  = πP.PNAME, P. PNUM, SUM(   γP.PNAME, P. PNUM, SUM(SP.QTY) → SUM ( σP.WEIGHT >10 and P.PNUM = SP.PNUM (PSP)   ) =(1)πP.PNAME, P. PNUM, SUM(   γP.PNAME, P. PNUM, SUM(SP.QTY) → SUM ( σP.WEIGHT >10 (σP.PNUM = SP.PNUM (PSP)   )

πP.PNAME, P. PNUM, SUM(   γP.PNAME, P. PNUM, SUM(SP.QTY) → SUM ( Computing in QA πP.PNAME, P. PNUM, SUM(   γP.PNAME, P. PNUM, SUM(SP.QTY) → SUM ( σP.WEIGHT >10 (σP.PNUM = SP.PNUM (PSP)   )=(6) σP.WEIGHT >10 (πLσP.PNUM = SP.PNUM (PSP)   )

πP.PNAME, P. PNUM, SUM(   γP.PNAME, P. PNUM, SUM(SP.QTY) → SUM ( Computing in QA πP.PNAME, P. PNUM, SUM(   γP.PNAME, P. PNUM, SUM(SP.QTY) → SUM ( σP.WEIGHT >10 (πLσP.PNUM = SP.PNUM (PSP)   )=D πP.PNAME, P. PNUM, SUM(   γP.PNAME, P. PNUM, SUM(SP.QTY) → SUM ( σP.WEIGHT >10 (P P.PNUM = SP.PNUM SP   )

πP.PNAME, P. PNUM, SUM(   γP.PNAME, P. PNUM, SUM(SP.QTY) → SUM ( Computing in QA πP.PNAME, P. PNUM, SUM(   γP.PNAME, P. PNUM, SUM(SP.QTY) → SUM ( σP.WEIGHT >10 (P P.PNUM = SP.PNUM SP   )=(5) πP.PNAME, P. PNUM, SUM(   γP.PNAME, P. PNUM, SUM(SP.QTY) → SUM ( σP.WEIGHT >10 (P) P.PNUM = SP.PNUM σP.WEIGHT >10 (SP)   )

πP.PNAME, P. PNUM, SUM (   γP.PNAME, P. PNUM, SUM(SP.QTY) → SUM ( Computing in QA πP.PNAME, P. PNUM, SUM (   γP.PNAME, P. PNUM, SUM(SP.QTY) → SUM ( (σP.WEIGHT >10 (P) P.PNUM = SP.PNUM σP.WEIGHT >10 (SP)   ) =D πP.PNAME, P. PNUM, SUM(    γP.PNAME, P. PNUM, SUM(SP.QTY) → SUM ( σP.WEIGHT >10 (P) P.PNUM = SP.PNUM (σP.WEIGHT >10 =1)(SP)  )=

σSUM >= 200 (γP.PNAME, P. PNUM, SUM(SP.QTY) → SUM ( Final Expression = πP.PNAME, P. PNUM, SUM( σSUM >= 200 (γP.PNAME, P. PNUM, SUM(SP.QTY) → SUM ( σP.WEIGHT >10 (P) P.PNUM = SP.PNUM (SP)  )= 

γP.PNAME, P. PNUM, SUM(SP.QTY) → SUM  σSUM >= 200 γP.PNAME, P. PNUM, SUM(SP.QTY) → SUM P.PNUM = SP.PNUM σp.weight SP P

  Final Expression is computationally cheaper than σP.WEIGHT >10 (P) is smaller than P

σp.weight SP P πP.PNAME, P. PNUM, SUM σSUM >= 200 P.PNUM = SP.PNUM  σSUM >= 200 P.PNUM = SP.PNUM σp.weight γP. PNUM, SUM(SP.QTY) → SUM SP P

P.PNUM = SP.PNUM σp.weight P SP πP.PNAME, P. PNUM, SUM σSUM >= 200  P.PNUM = SP.PNUM σp.weight σSUM >= 200 P γP. PNUM, SUM(SP.QTY) → SUM SP

EXPRESSION TREES Generated by combining several Qerry Algebra operators into one expression by applying one operator to the result(s) of one or more operators. The leaves of this tree are names of relations. Interior nodes are operators, which are applied to the relations represented by its child or children