Advance Database Systems

Slides:



Advertisements
Similar presentations
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide
Advertisements

Copyright © 2011 Ramez Elmasri and Shamkant Navathe Algorithms for SELECT and JOIN Operations (8) Implementing the JOIN Operation: Join (EQUIJOIN, NATURAL.
Copyright © 2004 Ramez Elmasri and Shamkant Navathe Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 15-1 Query Processing and.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19 Algorithms for Query Processing and Optimization.
Chapter 15 Algorithms for Query Processing and Optimization Copyright © 2004 Pearson Education, Inc.
1 CSE 480: Database Systems Lecture 22: Query Optimization Reference: Read Chapter 15.6 – 15.8 of the textbook.
Database Management Systems, R. Ramakrishnan and Johannes Gehrke1 Evaluation of Relational Operations: Other Techniques Chapter 12, Part B.
SPRING 2004CENG 3521 Query Evaluation Chapters 12, 14.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19 Algorithms for Query Processing and Optimization.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19 Algorithms for Query Processing and Optimization.
CS263 Lecture 19 Query Optimisation.  Motivation for Query Optimisation  Phases of Query Processing  Query Trees  RA Transformation Rules  Heuristic.
©Silberschatz, Korth and Sudarshan13.1Database System Concepts Chapter 13: Query Processing Overview Measures of Query Cost Selection Operation Sorting.
Query Processing (overview)
QUERY OPTIMIZATION AND QUERY PROCESSING.
CSCI 5708: Query Processing I Pusheng Zhang University of Minnesota Feb 3, 2004.
ACS-4902 Ron McFadyen Chapter 15 Algorithms for Query Processing and Optimization.
Introduction to Query Processing and Query Optimization Techniques
Chapter 15 Algorithms for Query Processing and Optimization Copyright © 2004 Pearson Education, Inc.
ACS-4902 Ron McFadyen Chapter 15 Algorithms for Query Processing and Optimization See Sections 15.1, 2, 3, 7.
ICS (072)Query Processing and Optimization 1 Chapter 15 Algorithms for Query Processing and Optimization ICS 424 Advanced Database Systems Dr.
1 Evaluation of Relational Operations: Other Techniques Chapter 12, Part B.
CSCI 5708: Query Processing I Pusheng Zhang University of Minnesota Feb 3, 2004.
1 Query Processing: The Basics Chapter Topics How does DBMS compute the result of a SQL queries? The most often executed operations: –Sort –Projection,
Algorithms for Query Processing and Optimization
Query Processing & Optimization
Chapter 19 Query Processing and Optimization
Copyright © 2004 Ramez Elmasri and Shamkant Navathe Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 15-1 Query Processing and.
©Silberschatz, Korth and Sudarshan13.1Database System Concepts Chapter 13: Query Processing Overview Measures of Query Cost Selection Operation Sorting.
Chapter 13 Query Processing Melissa Jamili CS 157B November 11, 2004.
Chapter 15 Algorithms for Query Processing and Optimization Copyright © 2004 Pearson Education, Inc.
Query Processing and Optimization
Query Processing. Steps in Query Processing Validate and translate the query –Good syntax. –All referenced relations exist. –Translate the SQL to relational.
Algorithms for Query Processing and Optimization
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
CPSC 404, Laks V.S. Lakshmanan1 Evaluation of Relational Operations: Other Operations Chapter 14 Ramakrishnan & Gehrke (Sections ; )
Search Algorithm Lecture Chapter 15-2 Algorithms for SELECT and JOIN Operations Implementing the SELECT Operation : Search Methods for Simple Selection:
Computing & Information Sciences Kansas State University Tuesday, 03 Apr 2007CIS 560: Database System Concepts Lecture 29 of 42 Tuesday, 03 April 2007.
Chapter 15 Algorithms for Query Processing and Optimization Copyright © 2004 Pearson Education, Inc.
Chapter 12 Query Processing. Query Processing n Selection Operation n Sorting n Join Operation n Other Operations n Evaluation of Expressions 2.
1 Chapter 18 Query Processing and Optimization Query Processing and Optimization Scanner: identify language components. keywords, attribute,
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide
Chapter 15 Algorithms for Query Processing and Optimization Copyright © 2004 Pearson Education, Inc.
Advance Database Systems Query Optimization Ch 15 Department of Computer Science The University of Lahore.
1 B + -Trees: Search  If there are n search-key values in the file,  the path is no longer than  log  f/2  (n)  (worst case).
File Processing : Query Processing 2008, Spring Pusan National University Ki-Joune Li.
Query Processing – Implementing Set Operations and Joins Chap. 19.
Chapter 15 Algorithms for Query Processing and Optimization Copyright © 2004 Pearson Education, Inc.
Chapter 15 Algorithms for Query Processing and Optimization Copyright © 2004 Pearson Education, Inc.
Copyright © 2011 Ramez Elmasri and Shamkant Navathe CPSC 8620: Database Management System Design Notes 7 Query Processing.
Query optimization Algorithms for execution query elements Execution strategy Query optimization when using indices.
CHAPTER 19 Query Optimization. CHAPTER 19 Query Optimization.
Algorithms for Query Processing and Optimization
Chapter 4: Query Processing
CS 540 Database Management Systems
CS 440 Database Management Systems
Database Management System
Chapter 12: Query Processing
Chapter 15 QUERY EXECUTION.
Database Management Systems (CS 564)
Evaluation of Relational Operations: Other Operations
File Processing : Query Processing
Database Applications (15-415) DBMS Internals- Part VI Lecture 15, Oct 23, 2016 Mohammad Hammoud.
1/3/2019.
Lecture 2- Query Processing (continued)
Chapter 12 Query Processing (1)
Evaluation of Relational Operations: Other Techniques
External Sorting Sorting is used in implementing many relational operations Problem: Relations are typically large, do not fit in main memory So cannot.
Database Administration
Evaluation of Relational Operations: Other Techniques
Algorithms for Query Processing and Optimization
Presentation transcript:

Advance Database Systems Query Optimization Ch 15 Department of Computer Science The University of Lahore 1

Algorithms for Query Processing and Optimization 2/18/2019 Algorithms for Query Processing and Optimization

Scope of Lecturer Introduction to Query Processing 2/18/2019 Scope of Lecturer Introduction to Query Processing Translating SQL Queries into Relational Algebra Algorithms for External Sorting Algorithms for SELECT and JOIN Operations Algorithms for PROJECT and SET Operations Implementing Aggregate Operations and Outer Joins Combining Operations using Pipelining

Introduction to Query Processing 2/18/2019 Introduction to Query Processing Query optimization: the process of choosing a suitable execution strategy for processing a query. Two internal representations of a query Query TreeAn internal representation of the query is then created, usually as a tree data structure called a query tree. Query Graph It is also possible to represent the query using a graph data structure called a query graph

Introduction to Query Processing 2/18/2019 Introduction to Query Processing

Translating SQL Queries into Relational Algebra 2/18/2019 Translating SQL Queries into Relational Algebra Query block: the basic unit that can be translated into the algebraic operators and optimized. A query block contains a single SELECT-FROM-WHERE expression, as well as GROUP BY and HAVING clause if these are part of the block. Nested queries within a query are identified as separate query blocks. Aggregate operators in SQL must be included in the extended algebra.

Correlated vs. Noncorrelated Subqueries Do not depend on data from the outer query Execute once for the entire outer query Correlated subqueries: Make use of data from the outer query Execute once for each row of the outer query Can use the EXISTS operator 2/18/2019

Processing a noncorrelated subquery Figure Processing a noncorrelated subquery No reference to data in outer query, so subquery executes once only The subquery executes and returns the customer IDs from the ORDER_T table The outer query on the results of the subquery These are the only customers that have IDs in the ORDER_T table 2/18/2019

Figure Processing a correlated subquery Subquery refers to outer-query data, so executes once for each row of outer query Note: only the orders that involve products with Natural Ash will be included in the final results 2/18/2019

Translating SQL Queries into Relational Algebra 2/18/2019 Translating SQL Queries into Relational Algebra SELECT LNAME, FNAME FROM EMPLOYEE WHERE SALARY > ( SELECT MAX (SALARY) FROM EMPLOYEE WHERE DNO = 5); SELECT LNAME, FNAME FROM EMPLOYEE WHERE SALARY > C SELECT MAX (SALARY) FROM EMPLOYEE WHERE DNO = 5 πLNAME, FNAME (σSALARY>C(EMPLOYEE)) ℱMAX SALARY (σDNO=5 (EMPLOYEE))

Algorithms for External Sorting 2/18/2019 Algorithms for External Sorting External sorting: refers to sorting algorithms that are suitable for large files of records stored on disk that do not fit entirely in main memory, such as most database files. Sort-Merge strategy: starts by sorting small subfiles (runs) of the main file and then merges the sorted runs, creating larger sorted subfiles that are merged in turn.

Input file PASS 0 PASS 1 PASS 2 PASS 3 9 3,4 6,2 9,4 8,7 5,6 3,1 2 2,6 2/18/2019 Input file PASS 0 PASS 1 PASS 2 PASS 3 9 3,4 6,2 9,4 8,7 5,6 3,1 2 2,6 4,9 7,8 1,3 2,3 4,6 4,7 8,9 4,4 6,7 1,2 3,5 6 4,5 6,6

External Sorting File having blocks, b Available buffer, nB 2/18/2019 External Sorting File having blocks, b Available buffer, nB No. of initial runs, nR= ceiling (b/nB) Sorting Phase Merging Phase Degree of Merging dM min(nB -1 , nR) No. of Passes, Ceiling (logdM(nR))

Example File having blocks, b = 1024 Available buffer, nB = 5 2/18/2019 Example File having blocks, b = 1024 Available buffer, nB = 5 No. of initial runs, nR = ? Degree of Merging dM = ? No. of Passes = ?

Example File having blocks, b = 1024 Available buffer, nB = 5 2/18/2019 Example File having blocks, b = 1024 Available buffer, nB = 5 No. of initial runs, nR = ceil (1024 / 5 ) Degree of Merging dM = ? No. of Passes = ?

3. Algorithms for SELECT and JOIN Operations (1) 2/18/2019 3. Algorithms for SELECT and JOIN Operations (1) Implementing the SELECT Operation: Examples: (OP1): s SSN='123456789' (EMPLOYEE) (OP2): s DNUMBER>5(DEPARTMENT) (OP3): s DNO=5(EMPLOYEE) (OP4): s DNO=5 AND SALARY>30000 AND SEX=F(EMPLOYEE) (OP5): s ESSN=123456789 AND PNO=10(WORKS_ON)

Algorithms for SELECT and JOIN Operations 2/18/2019 Algorithms for SELECT and JOIN Operations Implementing the SELECT Operation Search Methods for Simple Selection: S1. Linear search (brute force): Retrieve every record in the file, and test whether its attribute values satisfy the selection condition. S2. Binary search: If the selection condition involves an equality comparison on a key attribute on which the file is ordered, binary search (which is more efficient than linear search) can be used. (See OP1). S3. Using a primary index or hash key to retrieve a single record: If the selection condition involves an equality comparison on a key attribute with a primary index (or a hash key), use the primary index (or the hash key) to retrieve the record.

Algorithms for SELECT and JOIN Operations 2/18/2019 Algorithms for SELECT and JOIN Operations Implementing the SELECT Operation (cont.): Search Methods for Simple Selection: S4. Using a primary index to retrieve multiple records: If the comparison condition is >, ≥, <, or ≤ on a key field with a primary index, use the index to find the record satisfying the corresponding equality condition, then retrieve all subsequent records in the (ordered) file. S5. Using a clustering index to retrieve multiple records: If the selection condition involves an equality comparison on a non-key attribute with a clustering index, use the clustering index to retrieve all the records satisfying the selection condition.

Algorithms for SELECT and JOIN Operations 2/18/2019 Algorithms for SELECT and JOIN Operations Implementing the SELECT Operation (cont.): Search Methods for Complex Selection: S7. Conjunctive selection: If an attribute involved in any single simple condition in the conjunctive condition has an access path that permits the use of one of the methods S2 to S6, use that condition to retrieve the records and then check whether each retrieved record satisfies the remaining simple conditions in the conjunctive condition for example, if an index has been created on the composite key (ESSN, PNO) of the WORKS_ON file for oP5-we can use the index directly.. S8. Conjunctive selection using a composite index: If two or more attributes are involved in equality conditions in the conjunctive condition and a composite index (or hash structure) exists on the combined field, we can use the index directly.

Algorithms for SELECT and JOIN Operations 2/18/2019 Algorithms for SELECT and JOIN Operations Implementing the SELECT Operation (cont.): Search Methods for Complex Selection: S9. Conjunctive selection by intersection of record pointers: This method is possible if secondary indexes are available on all (or some of) the fields involved in equality comparison conditions in the conjunctive condition and if the indexes include record pointers (rather than block pointers). Each index can be used to retrieve the record pointers that satisfy the individual condition. The intersection of these sets of record pointers gives the record pointers that satisfy the conjunctive condition, which are then used to retrieve those records directly. If only some of the conditions have secondary indexes, each retrieved record is further tested to determine whether it satisfies the remaining conditions.

Algorithms for SELECT and JOIN Operations (7) 2/18/2019 Algorithms for SELECT and JOIN Operations (7) Implementing the SELECT Operation (cont.): Whenever a single condition specifies the selection, we can only check whether an access path exists on the attribute involved in that condition. If an access path exists, the method corresponding to that access path is used; otherwise, the “brute force” linear search approach of method S1 is used. (See OP1, OP2 and OP3) For conjunctive selection conditions, whenever more than one of the attributes involved in the conditions have an access path, query optimization should be done to choose the access path that retrieves the fewest records in the most efficient way . Disjunctive selection conditions

2/18/2019 Where simple conditions are connected by the OR logical connective rather than by AND) is muchharder to process and optimize. For example, consider op4': (op49): (J" DND=5 OR SALARY>30000 OR SEX=' F' (EMPLOYEE)

Algorithms for SELECT and JOIN Operations 2/18/2019 Algorithms for SELECT and JOIN Operations Implementing the JOIN Operation: Join (EQUIJOIN, NATURAL JOIN) two–way join: a join on two files e.g. R A=B S Examples (OP6): EMPLOYEE DNO=DNUMBER DEPARTMENT (OP7): DEPARTMENT MGRSSN=SSN EMPLOYEE

Algorithms for SELECT and JOIN Operations 2/18/2019 Algorithms for SELECT and JOIN Operations Implementing the JOIN Operation (cont.): Methods for implementing joins: J1. Nested-loop join (brute force): For each record t in R (outer loop), retrieve every record s from S (inner loop) and test whether the two records satisfy the join condition t[A] = s[B]. J2. Single-loop join (Using an access structure to retrieve the matching records): If an index (or hash key) exists for one of the two join attributes — say, B of S — retrieve each record t in R, one at a time, and then use the access structure to retrieve directly all matching records s from S that satisfy s[B] = t[A].

Algorithms for SELECT and JOIN Operations 2/18/2019 Algorithms for SELECT and JOIN Operations Implementing the JOIN Operation (cont.): Methods for implementing joins: J3. Sort-merge join: If the records of R and S are physically sorted (ordered) by value of the join attributes A and B, respectively, we can implement the join in the most efficient way possible. Both files are scanned in order of the join attributes, matching the records that have the same values for A and B. In this method, the records of each file are scanned only once each for matching with the other file—unless both A and B are non-key attributes, in which case the method needs to be modified slightly.

Algorithms for SELECT and JOIN Operations 2/18/2019 Algorithms for SELECT and JOIN Operations Implementing the JOIN Operation (cont.): Methods for implementing joins: J4. Hash-join: The records of files R and S are both hashed to the same hash file, using the same hashing function on the join attributes A of R and B of S as hash keys. A single pass through the file with fewer records (say, R) hashes its records to the hash file buckets. A single pass through the other file (S) then hashes each of its records to the appropriate bucket, where the record is combined with all matching records from R.

Algorithms for SELECT and JOIN Operations 2/18/2019 Algorithms for SELECT and JOIN Operations Factors affecting JOIN performance Available buffer space Join selection factor Choice of inner VS outer relation

Analysis – Example Query graph Join graph SELECT ENAME,RESP FROM EMP, ASG, PROJ WHERE EMP.ENO = ASG.ENO AND ASG.PNO = PROJ.PNO AND PNAME = "CAD/CAM" AND DUR ≥ 36 AND TITLE = "Programmer" Query graph Join graph DUR≥36 ASG ASG EMP.ENO=ASG.ENO ASG.PNO=PROJ.PNO EMP.ENO=ASG.ENO ASG.PNO=PROJ.PNO TITLE = “Programmer” EMP PROJ EMP PROJ RESP ENAME RESULT PNAME=“CAD/CAM”

If the query graph is not connected, the query is wrong If the query graph is not connected, the query is wrong. SELECT ENAME,RESP FROM EMP, ASG, PROJ WHERE EMP.ENO = ASG.ENO AND PNAME = "CAD/CAM" AND DUR ≥ 36 AND TITLE = "Programmer" ASG EMP PROJ RESP ENAME RESULT PNAME=“CAD/CAM”

2/18/2019 Thanks