Lecture 5 on Query Optimization

Slides:



Advertisements
Similar presentations
Chapter 6 The Relational Algebra
Advertisements

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
Copyright © 2004 Pearson Education, Inc.. Chapter 15 Algorithms for Query Processing and Optimization.
CS CS4432: Database Systems II Logical Plan Rewriting.
Copyright © 2004 Ramez Elmasri and Shamkant Navathe Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 15-1 Query Processing and.
Chapter 15 Algorithms for Query Processing and Optimization Copyright © 2004 Pearson Education, Inc.
Query Optimization CS634 Lecture 12, Mar 12, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
Distributed Query Processing –An Overview
Distributed DBMS © M. T. Özsu & P. Valduriez Ch.7/1 Outline Introduction Background Distributed Database Design Database Integration Semantic Data Control.
1 CSE 480: Database Systems Lecture 22: Query Optimization Reference: Read Chapter 15.6 – 15.8 of the textbook.
Advanced Database Systems September 2013 Dr. Fatemeh Ahmadi-Abkenari 1.
Query Evaluation. An SQL query and its RA equiv. Employees (sin INT, ename VARCHAR(20), rating INT, age REAL) Maintenances (sin INT, planeId INT, day.
15/1/20091 Lecture 2 on Relational Algebra This lecture introduces relational data model with relational algebra as its mathematical foundation Relational.
Copyright © 2004 Pearson Education, Inc.. Chapter 11 Relational Database Design Algorithms and Further Dependencies.
CS263 Lecture 19 Query Optimisation.  Motivation for Query Optimisation  Phases of Query Processing  Query Trees  RA Transformation Rules  Heuristic.
1 Distributed Databases Review CS347 June 6, 2001.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
16.2 ALGEBRAIC LAWS FOR IMPROVING QUERY PLANS Ramya Karri ID: 206.
Distributed DBMS © M. T. Özsu & P. Valduriez Ch.7/1 Outline Introduction Background Distributed Database Design Database Integration Semantic Data Control.
CS 255: Database System Principles slides: From Parse Trees to Logical Query Plans By:- Arunesh Joshi Id:
Query Processing Presented by Aung S. Win.
CS 255: Database System Principles slides: From Parse Trees to Logical Query Plans By:- Arunesh Joshi Id:
CSCE Database Systems Chapter 15: Query Execution 1.
Database Management 9. course. Execution of queries.
Query optimization in relational DBs Leveraging the mathematical formal underpinnings of the relational model.
Session-9 Data Management for Decision Support
Overview of Query Processing
Department of Computer Science and Engineering, HKUST Slide Query Processing and Optimization Query Processing and Optimization.
PMIT-6102 Advanced Database Systems By- Jesmin Akhter Assistant Professor, IIT, Jahangirnagar University.
Copyright © 2004 Pearson Education, Inc.. Chapter 15 Algorithms for Query Processing and Optimization.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
PMIT-6102 Advanced Database Systems By- Jesmin Akhter Assistant Professor, IIT, Jahangirnagar University.
PMIT-6102 Advanced Database Systems By- Jesmin Akhter Assistant Professor, IIT, Jahangirnagar University.
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.
Query optimization in distributed database systems.
Chapter 6 The Relational Algebra Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Chapter 18 Query Processing. 2 Chapter - Objectives u Objectives of query processing and optimization. u Static versus dynamic query optimization. u How.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Al-Maarefa College for Science and Technology INFO 232: Database systems Chapter 3 “part 2” The Relational Algebra and Calculus Instructor Ms. Arwa Binsaleh.
CS 257 Chapter – 15.9 Summary of Query Execution Database Systems: The Complete Book Krishna Vellanki 124.
1 ICS 214B: Transaction Processing and Distributed Data Management Lecture 9: Fragmentation and Distributed Query Processing Professor Chen Li.
Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.
Query Processing – Query Trees. Evaluation of SQL Conceptual order of evaluation – Cartesian product of all tables in from clause – Rows not satisfying.
CSC271 Database Systems Lecture # 7. Summary: Previous Lecture  Relational keys  Integrity constraints  Views.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Chapter 15 Algorithms for Query Processing and Optimization Copyright © 2004 Pearson Education, Inc.
Copyright © 2004 Pearson Education, Inc.. Chapter 6 The Relational Algebra and Relational Calculus.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Introduction to Query Processing (1) Query optimization: The process of choosing a suitable execution.
Lecture 15: Query Optimization. Very Big Picture Usually, there are many possible query execution plans. The optimizer is trying to chose a good one.
Distributed Database Design Bayu Adhi Tama, MTI Fasilkom-Unsri Adapted from Connolly, et al., Database Systems 4 th Edition, Pearson Education Limited,
Chapter 13: Query Processing
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus تنبيه : شرائح العرض.
CS742 – Distributed & Parallel DBMSPage 3. 1M. Tamer Özsu Outline Introduction & architectural issues Data distribution  Distributed query processing.
Query Processing and Query Optimization Database System Implementation CSE 507 Slides adapted from Silberschatz, Korth and Sudarshan Database System Concepts.
Copyright © 2004 Pearson Education, Inc.. Chapter 24 Enhanced Data Models for Advanced Applications.
Query Processing and Optimization, and Database Tuning
Query Optimization Heuristic Optimization
COMP3017 Advanced Databases
Database System Implementation CSE 507
Fundamental of Database Systems
Query Processing B.Ramamurthy Chapter 12 11/27/2018 B.Ramamurthy.
Distributed Database Management Systems
Data Manipulation using Relational Algebra
Lecture 3 Relational Algebra and SQL
Presentation transcript:

Lecture 5 on Query Optimization This lecture demonstrates the techniques of optimizing SQL query command for efficiency performance. The optimizer will transform SQL query into an equivalent SQL query in the form of relational algebra with less cost. It shows how to apply heuristics rules in reducing the cost of query. 11-2-2009

Query Optimizer We can interpret an expression of relational algebra not only as the specification of the semantics of a query, but also as the specification of sequence of operations. From this viewpoint, two expressions with the same semantics can describe two different sequences of operations. Given: Relation EMP(Empnum, Name, Sal, Tax, Mgrnum, Deptnum) Relation DEPT(Deptnum, Name, Area, Mgrnum) Relation SUPPLIER(Snum, Name, City) Relation SUPPLY(Snum, Pnum, Deptnum, Quan) PJ NAME, DEPTNUM SL DEPTNUM=15 EMP = SLDEPTNAM=15 PJ NAME, DEPTNUM EMP condition SL DEPTNUM  PJ DEPTNUMEMP (projected data must be in selected data) Are equivalent expressions but define two different sequences of operations. 11-2-2009

11-2-2009

Bin_op -> CP | UN | DF | JNF | NJNF | SJF | NSJF The operator tree of an expression of relational algebra can be regarded as the parse tree of the expression itself, assuming the following grammar: R -> identifier R -> (R) R -> un_op R R -> R bin_op R Un_op -> SLF | PJA Bin_op -> CP | UN | DF | JNF | NJNF | SJF | NSJF Two relations are equivalent when their tuples represent the same mapping from attribute names to values, even if the order of attributes is different. 11-2-2009

Commutativity of unary operations: U1U2R <->U2U1R Commutativity of operands of binary operations R B S <-> S B R Associativity of unary operations: U R <-> U1 U2 R Distributivity of unary operations with respect to binary operations: U (R B S) -> U(R) B U(S) Factorization of unary operations (this tranforsmation is the inverse distributivity): U (R) B U(S) -> U (R B S) 11-2-2009

Commutativity of unary operations SLF1 SLF2 R → SLF2 SLF1 R SLF1 PJA2 R → PJA2 SLF1 R Attr(F1)A2 Commutativity and Associativity of binary operations R UN S → S UN R R CP S → S CP R R JNF S → S JNF R (R UN S) UN T → R UN (S UN T) (R CP S) CP T → R CP (S CP T) Idempotence of unary operations PJA R → PJA1 PJA2 R AA1 AA2 SLF R → SLF1 SLF2 R F=F1  F2 11-2-2009

Distributivity of unary operations SLF (R UN S) → (SLF R) UN (SLF S) SLF (R DF S) → (SLF R) DF (SLF S) SLF (R SJF3 S) → (SLF R) SJF3 (SLFS S) FS=true => result is not empty PJA (R CP S) → (PJAR R) CP (PJAS S) AR=A-Attr(S)=R.A AS=A-Attr(R)=S.A Factorization of unary operations from binary operations (PJAR R) CP (PJAS S) → PJA (R CP S) A=ARAS (SLFR R) JNF1 (SLFS S) → SLF (R JNF1 S) F=FR ∧ FS 11-2-2009

11-2-2009

11-2-2009

11-2-2009

11-2-2009

11-2-2009

11-2-2009

11-2-2009

11-2-2009

11-2-2009

Qualified Relations A qualified relation is a pair [R: qR] where R is a relation called the body of the qualified relation and qR is a predicate called the qualification of the qualified relation, for example horizontal fragments are qualified relations in which the qualification corresponds to the partitioning predicate. As a result, [R: qR] is an unary operation such as selection (horizontal fragmentation) and/or projection (vertical fragmentation) to relation R. 11-2-2009

Algebra of qualified relations The application of an unary operation to qualified relation R Un_op[R: qR1] → Un_op R: qR2 produces a relation un_op R as its body and the predicate qR2 as its qualifications. On the left hand side, we apply qualification qR1 followed by un_ary operation. On the right hand side, we apply Un_ary operation first, followed by qualification qR2. Rule 1: SLF[R: qR] → [SLFR: F AND qR] (horizontal fragmentation) F holds all the tuples as well as qR 21-2-2009

Rule 2: PJA[R: qR] → [PJAR: qR] (vertical fragmentation) 21-2-2009

Horizontal Fragmentation Vertical Fragmentation 11-2-2009

Rule 3: [R: qR] CP [S: qS] → [R CP S: qR AND qS] Two qualifications apply to disjoint attributes of R CP S: 11-2-2009

Rule 4: [R: qR] DF [S: qS] → [R DF S: qR] 11-2-2009

Rule 5: [R: qR] UN [S: qS] → [R UN S: qR OR qS ] 21-2-2009

Properties to simplify an operator tree R NJN R  R R UN R  R R DF R  0 R NJN SLF R  SLF R R UN SLF R  R R DF SLF R  SLNOT F R (SLF1 R) NJN (SLF2 R)  SLF1 AND F2 R (SLF1 R) UN (SLF2 R)  SLF1 OR F2 R (SLF1 R) DF (SLF2 R)  SLF1 AND NOT F2 R 11-2-2009

Sub_expression of a query is empty SLF (0)  0 PJA (0)  0 R CP 0  0 R UN 0  R R DF 0  R 0 DF R  0 R JNF 0  0 R SJF 0  0 0 SJF R  0 Where 0 is an empty set 11-2-2009

Criterions for Query Optimization Use idempotence of selection and projection to generate appropriate selection and projection for each operand relation. Push selections and projections down in the tree as far as possible. Push selections down to the leaves of the tree, and then apply them using the algebra of qualified relations, substitute the selections result with the empty relation if the qualification of the result is contradictory. Use the algebra of qualified relations to evaluate the qualification of operands of joins. Substitute the subtree, including the join and its operands, with the empty relation if the qualification of the result of the join is contradictory. In order to distribute joins which appear in the global query, unions (representing fragment collections) must be pushed up, beyond the joins that we want to distribute. 11-2-2009

A modified operator tree for query Q1 by criterion 1 (use of idempotence) Note: R DF SLF R  SL NOT F R 11-2-2009

Decompose query Q2 by criterion 2 (push selection/project down in the tree) Q2: give the names of employees who work in a department whose manager has number 373 but who do not earn more than $35,000 PJEMP.NAME((EMP JN DEPTNUM=DEPTNUM SL MGRNUM=373 DEPT) DF (SLSAL>35000 EMP JN DEPTNUM=DEPTNUM SL MGRNUM=373 DEPT)) 11-2-2009

Operator tree 11-2-2009

Idempotence 21-2-2009

Factorization 11-2-2009

Simplication 11-2-2009

Push down Selection in the tree 11-2-2009

Decompose query Q3 by criterion 3(eliminate empty set) Q3: SL DEPTNUM=1 DEPT Given: DEPT = DEPT1: DEPTNUM<10 UN DEPT2: 10<DEPTNUM<20 DEPT3:DEPTNUM>20 11-2-2009

Decompose query Q3 by criterion 3(eliminate empty set) Q3: SL DEPTNUM=1 DEPT 11-2-2009

SUPPLIER => [SUPPLIER1:CITY=“SF”] UN [SUPPLIER2:CITY=“LA”] Decompose query Q4 by criterion 4(eliminate irrelevant joins) and 5(push union up in the tree) Q4: PJ SMUM (SUPPLY NJN SUPPLIER) Given: SUPPLIER => [SUPPLIER1:CITY=“SF”] UN [SUPPLIER2:CITY=“LA”] SUPPLY => [SUPPLY1:Snum=SUPPLIER1.Snum] UN [SUPPLY2:Snum=SUPPLIER2.Snum] 11-2-2009

Decompose query Q4 by criterion 4(eliminate irrelevant joins) and 5(push union up in the tree) Q4: PJ SMUM (SUPPLY NJN SUPPLIER) 11-2-2009

Decompose query Q4 by criterion 4(eliminate irrelevant joins) and 5(push union up in the tree) Q4: PJ SMUM (SUPPLY NJN SUPPLIER) 11-2-2009

Elimination of empty relation 11-2-2009

Optimized operator tree 11-2-2009

Lecture summary Many heuristics rules can be applied to reduce the cost of SQL query as follows: Idempotence of select and project. Push select and project down in the operator tree. Qualify relation first for eliminating contradictory relation(s). Evaluate join operations with qualified relation(s). Push Union operation up in the operator tree. 11-2-2009

(2) Show an example of qualifying following Review Question 5 (1) Discuss the reasons for converting SQL queries into relational algebra queries before optimization is done. (2) Show an example of qualifying following Relation R (Key, Attribute 1, Attribute 2) by Horizontal fragmentation Vertical fragmentation Into relations R1 and R2. (3) Show how to reconstruct relations R1 and R2 back into relation R from (a) Horizontal fragmentation (b) Vertical fragmentation 11-2-2009

Make-up Tutorial Question 5 Given: Relation PATIENT (Pnum, Name, Dept, Treat, Dnum) Relation CARE (*Pnum, Drug, Quan) A query search is needed for the names of the patients who are taking ‘asprin’ drug. List the Query in SQL (20%) in relational algebra (20%) in operator tree (20%) (iv) Given its qualified relations (fragments) as follows: Relation PATIENT1 = SL Dept=’surgery” and Treat=”intensive” PATIENT Relation PATIENT2 = SL Dept=’surgery” and Treat”intensive” PATIENT Relation PATIENT3 = SL Dept’surgery” PATIENT Relation CARE1 = CARE SJ Pnum=Pnum PATIENT1 Relation CARE2 = CARE SJ Pnum=Pnum PATIENT2 Relation CARE3 = CARE SJ Pnum=Pnum PATIENT3 Translate the query in into fragment queries (20%) and simplify them for optimization. (20%) 11-2-2009

Reading Assignment Chapter 15 Algorithm for Query Processing and Optimization of “Fundamentals of Database Systems” by Elmasri and Navathe, 5th edition, Pearson, 2007. 11-2-2009