CMPUT 391 Database Management Systems. An Overview of Query Processing. Textbook: Chapter 11 (first edition: Chapter 14)

Similar documents
CMPUT 391 Database Management Systems. Query Processing: The Basics. Textbook: Chapter 10. (first edition: Chapter 13) University of Alberta 1

Query Processing: The Basics. External Sorting

Announcement. Reading Material. Overview of Query Evaluation. Overview of Query Evaluation. Overview of Query Evaluation 9/26/17

Relational Query Optimization. Highlights of System R Optimizer

CS 4604: Introduction to Database Management Systems. B. Aditya Prakash Lecture #10: Query Processing

Database Systems. Announcement. December 13/14, 2006 Lecture #10. Assignment #4 is due next week.

R & G Chapter 13. Implementation of single Relational Operations Choices depend on indexes, memory, stats, Joins Blocked nested loops:

Relational Algebra. Relational Query Languages

Examples of Physical Query Plan Alternatives. Selected Material from Chapters 12, 14 and 15

Query Optimization. Schema for Examples. Motivating Example. Similar to old schema; rname added for variations. Reserves: Sailors:

Query Processing & Optimization

Faloutsos 1. Carnegie Mellon Univ. Dept. of Computer Science Database Applications. Outline

Relational Query Optimization

Database Applications (15-415)

Overview of Implementing Relational Operators and Query Evaluation

CAS CS 460/660 Introduction to Database Systems. Query Evaluation II 1.1

Query Optimization. Schema for Examples. Motivating Example. Similar to old schema; rname added for variations. Reserves: Sailors:

Administrivia. CS 133: Databases. Cost-based Query Sub-System. Goals for Today. Midterm on Thursday 10/18. Assignments

Evaluation of Relational Operations: Other Techniques. Chapter 14 Sayyed Nezhadi

Principles of Data Management. Lecture #12 (Query Optimization I)

Introduction to Query Processing and Query Optimization Techniques. Copyright 2011 Ramez Elmasri and Shamkant Navathe

Operator Implementation Wrap-Up Query Optimization

CSIT5300: Advanced Database Systems

Lassonde School of Engineering Winter 2016 Term Course No: 4411 Database Management Systems

QUERY OPTIMIZATION E Jayant Haritsa Computer Science and Automation Indian Institute of Science. JAN 2014 Slide 1 QUERY OPTIMIZATION

CS330. Query Processing

Query Optimization. Query Optimization. Optimization considerations. Example. Interaction of algorithm choice and tree arrangement.

ATYPICAL RELATIONAL QUERY OPTIMIZER

Announcements. Two typical kinds of queries. Choosing Index is Not Enough. Cost Parameters. Cost of Reading Data From Disk

Relational Query Languages. Relational Algebra and SQL. What is an Algebra? Select Operator. The Role of Relational Algebra in a DBMS

External Sorting Implementing Relational Operators

Relational Query Optimization. Overview of Query Evaluation. SQL Refresher. Yanlei Diao UMass Amherst October 23 & 25, 2007

Algorithms for Query Processing and Optimization. 0. Introduction to Query Processing (1)

Principles of Data Management. Lecture #9 (Query Processing Overview)

15-415/615 Faloutsos 1

DBMS Query evaluation

CompSci 516 Data Intensive Computing Systems

CompSci 516 Data Intensive Computing Systems. Lecture 11. Query Optimization. Instructor: Sudeepa Roy

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Chapter 12: Query Processing

Overview of Query Evaluation. Overview of Query Evaluation

Implementing Relational Operators: Selection, Projection, Join. Database Management Systems, R. Ramakrishnan and J. Gehrke 1

Database System Concepts

Evaluation of relational operations

Section 1: Redundancy Anomalies [10 points]

An SQL query is parsed into a collection of query blocks optimize one block at a time. Nested blocks are usually treated as calls to a subroutine

Overview of Query Evaluation. Chapter 12

Relational Algebra and SQL. Relational Query Languages

Query optimization. Elena Baralis, Silvia Chiusano Politecnico di Torino. DBMS Architecture D B M G. Database Management Systems. Pag.

Chapter 12: Query Processing

Evaluation of Relational Operations

Overview of Query Processing

Relational Query Languages. What is an Algebra? Roadmap. Relational Algebra. Chapter 5. The Role of Relational Algebra in a DBMS

Schema for Examples. Query Optimization. Alternative Plans 1 (No Indexes) Motivating Example. Alternative Plans 2 With Indexes

What happens. 376a. Database Design. Execution strategy. Query conversion. Next. Two types of techniques

Review. Relational Query Optimization. Query Optimization Overview (cont) Query Optimization Overview. Cost-based Query Sub-System

Implementation of Relational Operations. Introduction. CS 186, Fall 2002, Lecture 19 R&G - Chapter 12

Final Exam Review 2. Kathleen Durant CS 3200 Northeastern University Lecture 23

Query Processing Strategies and Optimization

Ch 5 : Query Processing & Optimization

Database Applications (15-415)

Physical Disk Structure. Physical Data Organization and Indexing. Pages and Blocks. Access Path. I/O Time to Access a Page. Disks.

Advanced Database Systems

CSIT5300: Advanced Database Systems

RELATIONAL OPERATORS #1

Evaluation of Relational Operations

Chapter 3. Algorithms for Query Processing and Optimization

CSE 544 Principles of Database Management Systems. Alvin Cheung Fall 2015 Lecture 7 - Query optimization

QUERY EXECUTION: How to Implement Relational Operations?

Chapter 12: Query Processing. Chapter 12: Query Processing

EECS 647: Introduction to Database Systems

Advances in Data Management Query Processing and Query Optimisation A.Poulovassilis

Overview of Query Evaluation

Hash-Based Indexing 165

Query Processing. Debapriyo Majumdar Indian Sta4s4cal Ins4tute Kolkata DBMS PGDBA 2016

CSE532 Relational Algebra and SQL

Relational Query Optimization. Overview of Query Evaluation. SQL Refresher. Yanlei Diao UMass Amherst March 8 and 13, 2007

Relational Query Optimization

Chapter 5. Relational Algebra and SQL

Database Management System

Query Evaluation Overview, cont.

CSE 544 Principles of Database Management Systems. Magdalena Balazinska Fall 2007 Lecture 9 - Query optimization

Chapter 13: Query Processing

Cost-based Query Sub-System. Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Last Class.

CSIT5300: Advanced Database Systems

QUERY OPTIMIZATION [CH 15]

University of Waterloo Midterm Examination Sample Solution

Overview of Query Processing. Evaluation of Relational Operations. Why Sort? Outline. Two-Way External Merge Sort. 2-Way Sort: Requires 3 Buffer Pages

Query Evaluation (i)

Implementation of Relational Operations

! A relational algebra expression may have many equivalent. ! Cost is generally measured as total elapsed time for

Chapter 13: Query Processing Basic Steps in Query Processing

Implementation of Relational Operations: Other Operations

Chapter 13: Query Optimization. Chapter 13: Query Optimization

Overview of Query Processing and Optimization

Outline. Database Management and Tuning. Outline. Join Strategies Running Example. Index Tuning. Johann Gamper. Unit 6 April 12, 2012

University of Waterloo Midterm Examination Solution

Database Tuning and Physical Design: Basics of Query Execution

Query Processing and Query Optimization. Prof Monika Shah

Outline. Query Processing Overview Algorithms for basic operations. Query optimization. Sorting Selection Join Projection

Transcription:

CMPUT 391 Database Management Systems Winter Semester 2006, Section B1, Dr. Jörg Sander An Overview of Query Processing Textbook: Chapter 11 (first edition: Chapter 14) Based on slides by Lewis, Bernstein and Kifer. University of Alberta 1

Query Evaluation Problem: An SQL query is declarative does not specify a query execution plan. A relational algebra expression is procedural there is an associated query execution plan. Solution: Convert SQL query to an equivalent relational algebra and evaluate it using the associated query execution plan. But which equivalent expression is best? University of Alberta 2

SELECT DISTINCT TargetList FROM R1, R2,, RN WHERE Condition Naive Conversion is equivalent to π TargetList (σ Condition (R1 R2... RN)) but this may imply a very inefficient query execution plan. Example: π Name (σ Id=ProfId CrsCode= CS532 (Professor Teaching)) Result can be < 100 bytes But if each relation is 50K then we end up computing an intermediate result Professor Teaching of size 1G before shrinking it down to just a few bytes. Problem: Find an equivalent relational algebra expression that can be evaluated efficiently. University of Alberta 3

Query Processing Architecture University of Alberta 4

Query Optimizer Uses heuristic algorithms to evaluate relational algebra expressions. This involves: estimating the cost of a relational algebra expression transforming one relational algebra expression to an equivalent one choosing access paths for evaluating the subexpressions Query optimizers do not optimize just try to find reasonably good evaluation strategies University of Alberta 5

Equivalence Preserving Transformations To transform a relational expression into another equivalent expression we need transformation rules that preserve equivalence Each transformation rule Is provably correct (ie, does preserve equivalence) Has a heuristic associated with it University of Alberta 6

Selection and Projection Rules Break complex selection into simpler ones: σ Cond1 Cond2 (R) σ Cond1 (σ Cond2 (R) ) Break projection into stages: π attr (R) π attr (π attr (R)), if attr attr Commute projection and selection: π attr (σ Cond (R)) σ Cond (π attr (R)), if attr all attributes in Cond University of Alberta 7

Commutativity and Associativity of Join (and Cartesian Product as Special Case) Join commutativity: R S S R used to reduce cost of nested loop evaluation strategies (smaller relation should be in outer loop) Join associativity: R (S T) (R S) T used to reduce the size of intermediate relations in computation of multirelational join first compute the join that yields smaller intermediate result N-way join has T(N) N! different evaluation plans T(N) is the number of parenthesized expressions N! is the number of permutations Query optimizer cannot look at all plans (might take longer to find an optimal plan than to compute query brute-force). Hence it does not necessarily produce optimal plan University of Alberta 8

Pushing Selections and Projections σ Cond (R S) R Cond S Cond relates attributes of both R and S Reduces size of intermediate relation since rows can be discarded sooner σ Cond (R S) σ Cond (R) S Cond involves only the attributes of R Reduces size of intermediate relation since rows of R are discarded sooner π attr (R S) π attr (π attr (R) S), if attributes(r) attr attr reduces the size of an operand of product University of Alberta 9

Equivalence Example σ C1 C2 C3 (R S) σ C1 (σ C2 (σ C3 (R S) ) ) σ C1 (σ C2 (R) σ C3 (S) ) σ C2 (R) C1 σ C3 (S) assuming C2 involves only attributes of R, C3 involves only attributes of S, and C1 relates attributes of R and S University of Alberta 10

Query Trees Query tree: tree structure that corresponds to a relational algebra expression: A leaf node represents an input relation; An internal node represents a relation obtained by applying one relational operator to its child nodes The root relation represents the answer to the query Two query trees are equivalent if their root relations are the same Left Deep Tree: right child of a join is always a base relation σ R π R.type=100 S.name R.id=S.sid S.rating > 5 SELECT DISTINCT S.sname FROM R, S WHERE R.id = S.sid AND R. type = 100 AND S.rating > 5 π S.sname (σ R.type = 100 S.rating > 5 (R R.id=S.sid S)) University of Alberta 11 S

Query Plans Query Plan: Query Tree with specification of algorithms for each operation. A query tree may have different execution plans Some plans are more efficient to execute than others. Two main issues: For a given query, what plans are considered? How is the cost of a plan estimated? Ideally: want to find best plan. Practically: avoid worst plans! σ R π S.name R.type=100 S.rating > 5 R.id=S.sid (Block-Nested-Loop Join) S Convention for joins: left child: outer relation right child: inner relation (On-the-fly) (On-the-fly) University of Alberta 12

Cost - Example 1 SELECT P.Name FROM Professor P, Teaching T WHERE P.Id = T.ProfId -- join condition AND P. DeptId = CS AND T.Semester = F1994 π Name (σ DeptId= CS Semester= F1994 (Professor Id=ProfId Teaching)) Master query tree (nothing pushed except the join cond.) π Name σ DeptId= CS Semester= F1994 Professor Id=ProfId Teaching University of Alberta 13

Metadata on Tables (in system catalogue) Professor (Id, Name, DeptId) size: 200 pages, 1000 rows, 50 departments indexes: clustered, 2-level B + tree on DeptId, hash on Id Teaching (ProfId, CrsCode, Semester) size: 1000 pages, 10,000 rows, 4 semesters indexes: clustered, 2-level B + tree on Semester; hash on ProfId Definition: Weight of an attribute average number of rows that have a particular value weight of Id = 1 (it is a key) weight of ProfId = 10 (10,000 classes/1000 professors) University of Alberta 14

Estimating Cost - Example 1 Join - block-nested loops with 52 page buffer (50 pages input for Professor, 1 page input for Teaching, 1 output page Scanning Professor (outer loop): 200 page transfers, (4 iterations, 50 pages each) In each iteration, read all 1000 pages of Teaching total cost = 200+4*1000 = 4,200 page transfers Join - index-nested loop using hash index on ProfId Use index to find matching rows in Teaching (inner loop) 1.2 I/Os to load the right bucket per professor tuple (assuming bucket fits into memory). On average 10 matching Teaching tuples per professor 10 I/Os per professor to fetch the rows from teaching Total cost = 200+1000*(1.2+10) = 11,400 page transfers University of Alberta 15

Estimating Cost - Example 1 (cont d) Selection and projection scan rows of intermediate file, discard those that don t satisfy selection, project on those that do, write result when output buffer is full. Complete algorithm (without pipelining): do join, write result to intermediate file on disk read intermediate file, do select/project, write final result Problem: unnecessary I/O University of Alberta 16

Pipelining Solution: use pipelining: join and select/project act as coroutines, operate as producer/consumer sharing a buffer in main memory. When join fills buffer; select/project filters it and outputs result Process is repeated until select/project has processed last output from join Performing select/project adds no additional I/O cost join Intermediate result select/project output final result buffer University of Alberta 17

Estimating Cost - Example 1 (cont d) Total cost BNLJ with pipelining: 4200 + (cost of outputting final result) We will disregard the cost of outputting final result in comparing with other query evaluation strategies, since this will be same for all University of Alberta 18

Cost Example 2 SELECT P.Name FROM Professor P, Teaching T WHERE P.Id = T.ProfId AND P. DeptId = CS AND T.Semester = F1994 π Name (σ Semester= F1994 (σ DeptId= CS (Professor) Id=ProfId Teaching)) π Name Partially pushed tree: selection pushed to Professor σ Semester= F1994 Id=ProfId σ DeptId= CS Professor Teaching University of Alberta 19

Cost Example 2 -- selection Compute σ DeptId= CS (Professor) (to reduce size of one join table) using clustered, 2-level B + tree on DeptId. 50 departments and 1000 professors; hence weight of DeptId is 20 (roughly 20 CS professors). These rows are in ~4 consecutive pages in Professor. Cost = 4 (to get rows) + 2 (to search index) = 6 keep resulting 4 pages in memory and pipe to next step clustered index on DeptId rows of Professor University of Alberta 20

Cost Example 2 -- join Index-nested loops join using hash index on ProfId of Teaching and looping on the selected professors (computed on previous slide) Since selection on Semester was not pushed, hash index on ProfId of Teaching can be used Note: if selection on Semester were pushed, the index on ProfId would have been lost an advantage of not using a fully pushed query execution plan University of Alberta 21

Cost Example 2 join (cont d) Each professor matches ~10 Teaching rows. Since 20 CS professors, hence 200 teaching records. All index entries for a particular ProfId are in same bucket. Assume ~1.2 I/Os to get a bucket. Cost = 1.2 20 (to fetch index entries for 20 CS professors) + 200 (to fetch Teaching rows, since hash index is unclustered) = 224 1.2 20 10 ProfId hash Teaching University of Alberta 22

Cost Example 2 select/project Pipe result of join to select (on Semester) and project (on Name) at no I/O cost Cost of output same as for Example 1 Total cost: 6 (select on Professor) + 224 (join) = 230 Comparison: 4200 (example 1) vs. 230 (example 2)!!! University of Alberta 23

Estimating Output Size It is important to estimate the size of the output of a relational expression size serves as input to the next stage and affects the choice of how the next stage will be evaluated. Size estimation uses the following measures on a particular instance of R (stored in system catalogue): Tuples(R): number of tuples Blocks(R): number of blocks/pages Values(R.A): number of distinct values of A MaxVal(R.A): maximum value of A MinVal(R.A): minimum value of A University of Alberta 24

Estimating Output Size For the query: SELECT TargetList FROM R 1, R 2,, R n WHERE Condition Reduction factor is Blocks (result set) Blocks(R 1 )... Blocks(R n ) Estimates by how much query result is smaller than input University of Alberta 25

Estimation of Reduction Factor Assume that reduction factors due to target list and query condition are independent Thus: reduction(query) = reduction(targetlist) reduction(condition) University of Alberta 26

Reduction Due to Simple Condition reduction (R i.a=val) = reduction (R i.a=r j.b) = 1 Values(R.A) 1 max(values(r i.a), Values(R j.b)) Assume that values are uniformly distributed, Tuples(R ) < Tuples(R i j ), and every row of R i matches a row of R. j Then the number of tuples that satisfy Condition is: Values(R i.a) [ (Tuples(R i )/Values(R i.a)) (Tuples(R j )/Values(R j.b)) ] reduction (R.A > val) = MaxVal(R i.a) val i Values(R i.a) University of Alberta 27

Reduction Due to Complex Condition reduction(cond 1 AND Cond 2 ) = reduction(cond 1 ) reduction(cond 2 ) reduction(cond 1 OR Cond 2 ) = min(1, reduction(cond 1 ) + reduction(cond 2 )) University of Alberta 28

Reduction Due to TargetList reduction(targetlist) = number-of-attributes (TargetList) Σ i number-of-attributes (R i ) (assuming all attributes have the same size) University of Alberta 29

Estimating Weight of Attribute weight(r.a) = Tuples(R) reduction(r.a=value) University of Alberta 30

Choosing a Query Execution Plan Step 1: Choose a logical plan Step 2: Reduce search space Step 3: Use a heuristic search to further reduce complexity University of Alberta 31

Step 1: Choosing a Logical Plan Involves choosing a query tree, which indicates the order in which algebraic operations are applied Heuristic: Pushed trees are good, but sometimes nearly fully pushed trees are better due to indexing (as we saw in the example) So: Take the initial master plan tree and produce a fully pushed tree plus several nearly fully pushed trees. University of Alberta 32

Step 2: Reduce Search Space Deal with associativity of binary operators (join, union, ) A B C D D Logical query execution plan A B C D Equivalent query tree C A B Equivalent left deep query tree University of Alberta 33

Step 2 (cont d) Two issues: Choose a particular shape of a tree (like in the previous slide) Equals the number of ways to parenthesize N-way join grows very rapidly Choose a particular permutation of the leaves E.g., 4! permutations of the leaves A, B, C, D University of Alberta 34

Step 2: Dealing With Associativity Too many trees to evaluate: settle on a particular shape: left-deep tree. (((A B) C) D) Used because it allows pipelining: P 1 P 2 P 3 A B X X C Y D Property: once a row of X has been output by P 1, it need not be output again (but C may have to be processed several times in P 2 for successive portions of X) Advantage: none of the intermediate relations (X, Y) Y have to be completely materialized and saved on disk. Important if one such relation is very large, but the final result is small University of Alberta 35 Y

Step 2: Dealing with Associativity consider the alternative: if we use the association ((A A B) B (C C D)) D P 1 P 2 A B C D X Y X Y P 3 Each row of X must be processed against all of Y. Hence all of Y (can be very large) must be stored in P 3, or P 2 has to recompute it several times. University of Alberta 36

Step 3: Heuristic Search The choice of left-deep trees still leaves open too many options (N! permutations): (((A A B) B C) D), (((C C A) A D) B),.. A heuristic (often dynamic programming based) algorithm is used to get a good plan University of Alberta 37

Index-Only Queries A B + tree index with search key attributes A 1, A 2,, A n has stored in it the values of these attributes for each row in the table. Queries involving a prefix of the attribute list A 1, A 2,.., A n can be satisfied using only the index no access to the actual table is required. Example: Transcript has a clustered B + tree index on StudId. A frequently asked query is one that requests all grades for a given CrsCode. Problem: Already have a clustered index on StudId cannot create another one (on CrsCode) Solution: Create an unclustered index on (CrsCode, Grade) Keep in mind, however, the overhead in maintaining extra indices University of Alberta 38