Overview of Query Evaluation. Chapter 12

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

Database Applications (15-415)

Implementation of Relational Operations: Other Operations

Overview of Implementing Relational Operators and Query Evaluation

CS330. Query Processing

Database Applications (15-415)

Evaluation of Relational Operations: Other Techniques

Overview of Query Evaluation. Overview of Query Evaluation

Evaluation of Relational Operations: Other Techniques

Evaluation of Relational Operations: Other Techniques

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

Overview of Query Evaluation

Evaluation of Relational Operations

Evaluation of relational operations

ECS 165B: Database System Implementa6on Lecture 7

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

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

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

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

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

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

15-415/615 Faloutsos 1

External Sorting Implementing Relational Operators

Administriva. CS 133: Databases. General Themes. Goals for Today. Fall 2018 Lec 11 10/11 Query Evaluation Prof. Beth Trushkowsky

Evaluation of Relational Operations

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

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

Overview of Query Evaluation

Relational Query Optimization. Highlights of System R Optimizer

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

Relational Query Optimization

Query Evaluation! References:! q [RG-3ed] Chapter 12, 13, 14, 15! q [SKS-6ed] Chapter 12, 13!

Query Evaluation Overview, cont.

QUERY EXECUTION: How to Implement Relational Operations?

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

Query Evaluation Overview, cont.

RELATIONAL OPERATORS #1

CSIT5300: Advanced Database Systems

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

CSE 444: Database Internals. Section 4: Query Optimizer

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

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

ATYPICAL RELATIONAL QUERY OPTIMIZER

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

Overview of DB & IR. ICS 624 Spring Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa

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

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

TotalCost = 3 (1, , 000) = 6, 000

Overview of Query Processing

CS330. Some Logistics. Three Topics. Indexing, Query Processing, and Transactions. Next two homework assignments out today Extra lab session:

CSE 444: Database Internals. Sec2on 4: Query Op2mizer

Operator Implementation Wrap-Up Query Optimization

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

192 Chapter 14. TotalCost=3 (1, , 000) = 6, 000

CompSci 516 Data Intensive Computing Systems

Query Evaluation (i)

Midterm Review CS634. Slides based on Database Management Systems 3 rd ed, Ramakrishnan and Gehrke

CSIT5300: Advanced Database Systems

Database Applications (15-415)

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

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

Implementation of Relational Operations

Database Applications (15-415)

Relational Algebra. Note: Slides are posted on the class website, protected by a password written on the board

Evaluation of Relational Operations. Relational Operations

Database Applications (15-415)

Database System Concepts

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

Database Management System

Evaluation of Relational Operations. SS Chung

Administrivia. Relational Query Optimization (this time we really mean it) Review: Query Optimization. Overview: Query Optimization

Evaluation of Relational Operations

Chapter 12: Query Processing. Chapter 12: Query Processing

Hash-Based Indexing 165

Relational Query Optimization

Implementing Joins 1

Query Processing: The Basics. External Sorting

Relational Query Languages. Preliminaries. Formal Relational Query Languages. Example Schema, with table contents. Relational Algebra

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

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

Chapter 12: Query Processing

Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Administrivia. Administrivia. Faloutsos/Pavlo CMU /615

Query Processing and Query Optimization. Prof Monika Shah

Chapter 13: Query Processing

Final Exam Review. Kathleen Durant CS 3200 Northeastern University Lecture 22

! 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

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

IMPORTANT: Circle the last two letters of your class account:

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

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

Final Exam Review. Kathleen Durant PhD CS 3200 Northeastern University

Chapter 12: Query Processing

CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #11: Query Processing and Midterm Review

Lecture 36 12/4/15. CMPSC431W: Database Management Systems. Instructor: Yu- San Lin

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

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

Advanced Database Systems

Storage and Indexing

Query Processing & Optimization. CS 377: Database Systems

Transcription:

Overview of Query Evaluation Chapter 12 1

Outline Query Optimization Overview Algorithm for Relational Operations 2

Overview of Query Evaluation DBMS keeps descriptive data in system catalogs. SQL queries are translated into an extended form of relational algebra: Query Plan Reasoning: Tree of operators with choice of one among several algorithms for each operator 3

Query Plan Evaluation sname bid=100 rating > 5 sid=sid Reserves Sailors Query Plan Execution: Each operator typically implemented using a `pull interface when an operator is `pulled for next output tuples, it `pulls on its inputs and computes them. 4

Overview of Query Evaluation Query Plan Optimization : Ideally: Want to find best plan. Practically: Avoid worst plans! Two main issues in query optimization: For a given query, what plans are considered? Algorithm to search plan space for cheapest (estimated) plan. How is the cost of a plan estimated? Cost models based on I/O estimates 5

Query Processing Common Techniques for Query Processing Algorithms: Indexing: Can use WHERE conditions to retrieve small set of tuples from large relation Iteration: Examine all tuples in an input table, one after the other (like in sorting algorithm). Partitioning: By using sorting or hashing, we partition input tuples and replace expensive operation by similar operations on smaller inputs. Watch for these techniques as we discuss query evaluation! 9

Access Paths An access path A method of retrieving tuples from a table Method: File scan, Index that matches a selection (in the query) Note : Contributes significantly to cost of relational operator. 10

Access Path Most selective access path: An index or file scan that we estimate will require fewest page I/Os. 11

Matching an Access Path A tree index matches (a conjunction of) terms that involve only attributes in a prefix of search key. Example : Given tree index on <a, b, c> selection a=5 AND b=3? selection a=5 AND b>6? selection b=3? 12

Matching an Access Path A hash index matches (a conjunction of) terms that has a term attribute = value for every attribute in search key of index. Example : Given hash index on <a, b, c> selection a=5 AND b=3 and c =6? selection c = 5 AND b = 6? selection a = 5? selection a > 5 AND b=3 and c =5? 13

Query Evaluation of Selection 14

Selection Example : R.attr OP value (R) Case 1: No Index, NOT sorted on R.attr Must scan the entire relation. Most selective access path = file scan Cost: M 15

Selection Example : R.attr OP value (R) Case 2: No Index, Sorted Data on R.attr Binary search for first tuple. Scan R for all satisfied tuples. Cost: O(log 2 M) 16

Selection Using B+ tree index Example : R.attr OP value (R) Case 3: B+ tree Index on R.attr Costs : cost I (finding qualifying data entries), and cost II (retrieving records) Cost I: 2-3 I/Os. (depth of B+ tree) Cost II: clustered index 1 I/O and few tuples that match unclustered index up to one I/O per qualifying tuple. 17

Example : Using B+ Index for Selections Example : SELECT * FROM WHERE Assume uniform distribution of names, Then say about ~ 10% of tuples qualify Assume R is on 100 pages and has 10,000 tuples. Clustered index: 2 IOs + little more than 10 I/Os Unclustered index : 2 IOs + up to 1,000 I/Os Reserves R R.rname < D% 18

Selection --- B+ Index Refinement for unclustered indexes: 1. Find qualifying data entries. 2. Sort rids of data records to be retrieved. 3. Fetch rids in order. Avoid retrieving the same page multiple times. However, # of such pages likely to be still higher than with clustering. Use of unclustered index for a range selection could be expensive. Often better (and simpler) to instead scan data file. 19

Selection Hash Index Hash index is good for equality selection. Costs: Cost I (retrieve index bucket page) + Cost II (retrieving qualifying tuples from R) Cost I is one I/O Cost II could up to one I/O per satisfying tuple. 20

General Condition : Conjunction A condition with several predicates combined by conjunction (AND): Example : day<8/9/94 AND bid=5 AND sid=3. 21

General Selections (Conjunction) First approach: (utilizing single index) Find the most selective access path, retrieve tuples using it. To reduce the number of tuples retrieved Apply any remaining terms that don t match the index: To discard some retrieved tuples This does not affect number of tuples/pages fetched. Example : Consider day<8/9/94 AND bid=5 AND sid=3. A B+ tree index on day can be used; then bid=5 and sid=3 must be checked for each retrieved tuple. Hash index on <bid, sid> could be used day<8/9/94 must then be checked on fly. 22

General Selections Second approach (utilizing multiple indices) Assuming 2 or more matching indexes that use Alternatives (2) or (3) for data entries. Get sets of rids of data records using each matching index. Then intersect these sets of rids Retrieve records and apply any remaining terms. Example : Consider day<8/9/94 AND bid=5 AND sid=3. A B+ tree index I on day and an index II on sid, both Alternative (2). - Retrieve rids of records satisfying day<8/9/94 using index I, - Retrieve rids of recs satisfying sid=3 using Index II - Intersect rids - Retrieve records and check bid=5. 23

General Condition : Disjunction Disjunction condition: one or more terms (R.attr op value) connected by OR ( ). Example : (day<8/9/94) OR (bid=5 AND sid=3) 24

General Selection (Disjunction) Case 1: Index is not available for one of terms. Then : Need a file scan anyways. Thus check other conditions in this file scan. E.g., Consider day<8/9/94 OR rname ='Joe' No index on day. Need a File scan. Even index is available in rname does not help. 25

General Selection (Disjunction) Case 2: Every term has a matching index. Retrieve candidate tuples using respective index. Then Union the results Example : consider day<8/9/94 OR rname ='Joe' Assume two B+ tree indexes on day and rname. Retrieve tuples satisfying day < 8/9/94 Retrieve tuples satisfying rname = 'Joe' Union the retrieved tuples. Duplicate Removal??? 26

Query Evaluation of Projection 27

Algorithms for Projection SELECT R.sid, R.bid FROM Reserves R 28

Algorithms for Projection SELECT FROM DISTINCT R.sid, R.bid Reserves R The expensive part is removing duplicates. SQL systems don t remove duplicates unless keyword DISTINCT is specified in query. Sorting Approach: Sort on <sid, bid> and remove duplicates. (Can optimize this by dropping unwanted information while sorting.) Hashing Approach: Hash on <sid, bid> to create partitions. Load partitions into memory one at a time, build in-memory hash structure, and eliminate duplicates. Indexing Approach : If there is an index with both R.sid and R.bid in the search key, may be cheaper than sorting data entries 29

Summary There are several alternative evaluation algorithms for each relational operator. 30

Conclusion Not one method wins. Optimizer must assess situation to select best possible candidate 31