Running Example – Airline

Slides:



Advertisements
Similar presentations
Examples of Physical Query Plan Alternatives
Advertisements

Overview of Query Evaluation (contd.) Chapter 12 Ramakrishnan and Gehrke (Sections )
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Query Optimization Chapters 14.
Database Management Systems 3ed, R. Ramakrishnan and Johannes Gehrke1 Evaluation of Relational Operations: Other Techniques Chapter 14, Part B.
Implementation of Other Relational Algebra Operators, R. Ramakrishnan and J. Gehrke1 Implementation of other Relational Algebra Operators Chapter 12.
Database Management Systems, R. Ramakrishnan and Johannes Gehrke1 Evaluation of Relational Operations: Other Techniques Chapter 12, Part B.
Database Management Systems, R. Ramakrishnan and Johannes Gehrke1 Evaluation of Relational Operations: Other Techniques Chapter 12, Part B.
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.
Query Evaluation. SQL to ERA SQL queries are translated into extended relational algebra. Query evaluation plans are represented as trees of relational.
1 Relational Query Optimization Module 5, Lecture 2.
1 Implementation of Relational Operations Module 5, Lecture 1.
SPRING 2004CENG 3521 Join Algorithms Chapter 14. SPRING 2004CENG 3522 Schema for Examples Similar to old schema; rname added for variations. Reserves:
Query Optimization 3 Cost Estimation R&G, Chapters 12, 13, 14 Lecture 15.
CS186 Final Review Query Optimization.
1 Evaluation of Relational Operations: Other Techniques Chapter 12, Part B.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Query Optimization Chapter 15.
Query Optimization Overview Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems December 2, 2004 Some slide content derived.
Evaluation of Relational Operations. Relational Operations v We will consider how to implement: – Selection ( ) Selects a subset of rows from relation.
1 Implementation of Relational Operations: Joins.
Introduction to Database Systems1 Relational Query Optimization Query Processing: Topic 2.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Query Evaluation Chapter 12: Overview.
Query Optimization. overview Histograms A histogram is a data structure maintained by a DBMS to approximate a data distribution Equiwidth vs equidepth.
Query Processing. Steps in Query Processing Validate and translate the query –Good syntax. –All referenced relations exist. –Translate the SQL to relational.
CPSC 404, Laks V.S. Lakshmanan1 Evaluation of Relational Operations: Other Operations Chapter 14 Ramakrishnan & Gehrke (Sections ; )
Introduction to Query Optimization, R. Ramakrishnan and J. Gehrke 1 Introduction to Query Optimization Chapter 13.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Introduction to Query Optimization Chapter 13.
CPSC 404, Laks V.S. Lakshmanan1 Evaluation of Relational Operations – Join Chapter 14 Ramakrishnan and Gehrke (Section 14.4)
Query Execution. Where are we? File organizations: sorted, hashed, heaps. Indexes: hash index, B+-tree Indexes can be clustered or not. Data can be stored.
Relational Operator Evaluation. overview Projection Two steps –Remove unwanted attributes –Eliminate any duplicate tuples The expensive part is removing.
Implementation of Database Systems, Jarek Gryz1 Evaluation of Relational Operations Chapter 12, Part A.
Query Execution Query compiler Execution engine Index/record mgr. Buffer manager Storage manager storage User/ Application Query update Query execution.
Alon Levy 1 Relational Operations v We will consider how to implement: – Selection ( ) Selects a subset of rows from relation. – Projection ( ) Deletes.
Database Applications (15-415) DBMS Internals- Part IX Lecture 20, March 31, 2016 Mohammad Hammoud.
Database Management System
CS222P: Principles of Data Management Lecture #15 Query Optimization (System-R) Instructor: Chen Li.
Introduction to Query Optimization
Evaluation of Relational Operations
Overview of Query Optimization
Chapter 15 QUERY EXECUTION.
Database Management Systems (CS 564)
Evaluation of Relational Operations: Other Operations
Introduction to Database Systems
Examples of Physical Query Plan Alternatives
Relational Operations
CS222P: Principles of Data Management Notes #11 Selection, Projection
Database Applications (15-415) DBMS Internals- Part VII Lecture 19, March 27, 2018 Mohammad Hammoud.
Database Applications (15-415) DBMS Internals- Part VI Lecture 15, Oct 23, 2016 Mohammad Hammoud.
Query processing and optimization
Database Applications (15-415) DBMS Internals- Part IX Lecture 21, April 1, 2018 Mohammad Hammoud.
Highlights of Query Processing And Optimization
Chapters 15 and 16b: Query Optimization
Relational Query Optimization
Overview of Query Evaluation
Chapter 12 Query Processing (1)
Overview of Query Evaluation
Implementation of Relational Operations
Lecture 13: Query Execution
Relational Query Optimization
CS222: Principles of Data Management Notes #11 Selection, Projection
Evaluation of Relational Operations: Other Techniques
Overview of Query Evaluation: JOINS
Database Systems (資料庫系統)
External Sorting Sorting is used in implementing many relational operations Problem: Relations are typically large, do not fit in main memory So cannot.
Lecture 11: B+ Trees and Query Execution
Relational Query Optimization (this time we really mean it)
CS222: Principles of Data Management Lecture #15 Query Optimization (System-R) Instructor: Chen Li.
Relational Query Optimization
Relational Query Optimization
Evaluation of Relational Operations: Other Techniques
CS222/CS122C: Principles of Data Management UCI, Fall 2018 Notes #10 Selection, Projection Instructor: Chen Li.
Presentation transcript:

Running Example – Airline Employees (sin INT, ename VARCHAR(20), rating INT, age REAL) Maintenances (sin INT, planeId INT, day DATE, descCode CHAR(10)) Assume that each tuple of Maintenances is 40 bytes long a block can hold 100 Maintenances tuples (4K block) we have 1000 blocks of such tuples = 100,000 tuples each tuple of Employees is 50 bytes long, a block can hold 80 Employees tuples we have 500 blocks of such tuples = 40,000 tuples

Query evaluations plans Recall: A query evaluation plan consists of an extended relational algebra tree, with additional annotations at each node indicating the implementation method to use for each relational operator. Sometimes it might be possible, to pipeline the result of one operator to another operator without creating a temporary table for the intermediate result. This saves in cost. When the input to a unary operator (e.g.  or ) is pipelined into it, we say the operator is applied on-the-fly. ename (on the fly) planeId=100 AND rating>5 (on the fly) (file scan) Employees Maintenances (file scan) (nested loops) Method to use

Alternative query evaluation plans Let’s look at two (naïve plans) Plan 1 ename (on the fly) ename (on the fly) Plan 2 planeId=100 AND rating>5 (on the fly) planeId=100 AND rating>5 (on the fly) (nested loops join) (sort merge join) Maintenances (file scan) Employees (file scan) Maintenances (file scan) Employees (file scan) Cost for this plan: 500,000 I/Os for the join.  and  are done in the fly; no I/O cost for them. Cost for this plan: 7,500 I/Os for the join.  and  are done in the fly; no I/O cost for them. We don’t consider the cost of writing the final result, since it is the same for any plan.

Plan 3: Pushing selections I Good heuristic for joins is to reduce the sizes of the tables to be joined as much as possible. One approach is to apply selections early; i.e. 'push' the selection ahead of the join. ename (on the fly) planeId=100 Maintenances (file scan) Employees rating>5 (sort-merge join) (scan write to temp T2) (scan write to temp T1)

Plan 3: Pushing selections II Cost of applying planeId=100 to Maintenances is: the cost of scanning Maintenances (1000 blocks) plus the cost of writing the result to a temporary table Tl. To estimate the size of T1, we reason as follows: if we know that there are 100 planes, we can assume that maintenances are spread out uniformly across all planes and estimate the number of blocks in T1 to be 1000/100 = 10. i.e. 1000+10 = 1010 I/Os. ename (on the fly) planeId=100 Maintenances (file scan) Employees rating>5 (sort-merge join) (scan write to temp T2) (scan write to temp T1)

Plan 3: Pushing selections III The cost of applying rating> 5 to Employees is: the cost of scanning Employees (500 blocks) plus the cost of writing out the result to a temporary table, say, T2. If we assume that ratings are uniformly distributed over the range 1 to 10, we can approximately estimate the size of T2 as 250 blocks. I.e. 500 + 250 = 750 I/Os ename (on the fly) planeId=100 Maintenances (file scan) Employees rating>5 (sort-merge join) (scan write to temp T2) (scan write to temp T1)

Plan 3: Pushing selections IV To do a sort-merge join of T1 and T2, they are first completely sorted and then merged. Assume we do that in two passes for each one. Thus, the cost of sorting T1 is 2 * 2 * 10 = 40 I/Os. the cost of sorting T2 is 2 * 2 * 250 = 1000 I/Os. To merge (for the join) the sorted versions of T1 and T2, we need to scan these tables: the cost of this step is 10+250=260 I/Os. The final projection is done on-the-fly, and by convention we ignore the cost of writing the final result. The total cost of the plan shown is: (1010+750)+(40+1000+260) = 3060 I/Os ename (on the fly) planeId=100 Maintenances (file scan) Employees rating>5 (sort-merge join) (scan write to temp T2) (scan write to temp T1)

Plan 3b: Pushing projections A further refinement is to push projections, just like we pushed selections past the join. Observe that only the SIN attribute of T1 and the SIN and ename attributes of T2 are really required. When we scan Maintenances and Employees to do the selections, we could also eliminate unwanted columns. This on-the-fly projection reduces the sizes of the temporary tables T1 and T2. The reduction in the size of T1 is substantial because only an integer field is retained. In fact, T1 now fits within one blocks (can be in MM), and we can perform a block nested loops join with a single scan of T2. We estimate the size of projected T2 to be 125 blocks The cost of the join step drops to under 125 page I/Os, and the total cost of the plan drops to under 2000 I/Os.

Plan 4: using Indexes I Suppose that we have a clustering B-Tree index on the planeId field of Maintenances and another B-Tree index on the SIN field of Employees. We can use the plan in the figure. ename (on the fly) planeId=100 Maintenances (clustering index on planeId) Employees (index on SIN) rating>5 (on the fly) (Index nested loops with pipelining ) (Use clustering index, don’t write results to temp)

Plan 4: using Indexes II Assume that there are 100 planes and assume that the maintenances are spread out uniformly across all planes. We can estimate the number of selected tuples to be 100,000/100=1000. Since the index on planeId is clustering, these 1000 tuples appear consecutively and therefore, the cost is: 10 blocks I/Os + 2 I/Os (the 2 I/Os are for finding the first block via the index. ename (on the fly) planeId=100 Maintenances (clustering index on planeId) Employees (index on SIN) rating>5 (on the fly) (Index nested loops with pipelining ) (Use clustered index, don’t write results to temp)

Plan 4: using Indexes III For each selected tuple, we retrieve matching Employees tuples using the index on the SIN field; The join field SIN is a key for Employees. Therefore, at most one Employees tuple matches a given Maintenances tuple. The cost of retrieving this matching tuple is 3 I/Os. So, for the 1000 Maintenances tuples we get 3000 I/O’s. For each tuple in the result of the join, we perform the selection rating>5 and the projection of ename on-the-fly. So, total: 3000+12 = 3012 I/Os. ename (on the fly) planeId=100 Maintenances (clustering index on planeId) Employees (index on SIN) rating>5 (on the fly) (Index nested loops with pipelining ) (Use clustered index, don’t write results to temp)

Plan 4: using Indexes IV Why didn’t we push the selection rating>5 ahead of the join? Had we performed the selection before the join, the selection would involve scanning Employees (since no index is available on the rating field of Employees). Also, once we apply such a selection, we have no index on the SIN field of the result of the selection. Thus, pushing selections ahead of joins is a good heuristic, but not always the best strategy. Typically, the existence of useful indexes is the reason a selection is not pushed. ename (on the fly) planeId=100 Maintenances (clustering index on planeId) Employees (index on SIN) rating>5 (on the fly) (Index nested loops with pipelining ) (Use clustered index, don’t write results to temp)

Plan 5: using Indexes V What about this different plan? The estimated size of T would be the number of blocks for the 1,000 Maintenances tuples that have planeId=100. i.e. T is 10 blocks. The cost of planeId=100 as before is 12 I/Os to retrieve plus 10 additional I/Os I/Os to write T. Then, sort T on the SIN attribute. 2passes = 40 I/Os. Employees is already sorted since it has a clustering index on SIN. The merge phase of the join needs a scan of both T and Employees. So, 10+500=510 I/Os. Total: (12+10)+40+510 = 572 I/Os ename (on the fly) rating>5 (on the fly) (Use clustering index, write results to temp T) (sort-merge join) planeId=100 Maintenances (clustering index on planeId) Employees (clust. index on SIN) This improved plan also demonstrates that pipelining is not always the best strategy.