Overview of Query Evaluation

Slides:



Advertisements
Similar presentations
Evaluation of Relational Operators CS634 Lecture 11, Mar Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
Advertisements

1 Overview of Query Evaluation Chapter Outline  Query Optimization Overview  Algorithm for Relational Operations.
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.
Advanced Databases: Lecture 2 Query Optimization (I) 1 Query Optimization (introduction to query processing) Advanced Databases By Dr. Akhtar Ali.
1 Overview of Query Evaluation Chapter Objectives  Preliminaries:  Core query processing techniques  Catalog  Access paths to data  Index matching.
SPRING 2004CENG 3521 Query Evaluation Chapters 12, 14.
Implementation of Relational Operations CS186, Fall 2005 R&G - Chapter 14 First comes thought; then organization of that thought, into ideas and plans;
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Overview of Query Evaluation Chapter 12.
Evaluation of Relational Operators 198:541. Relational Operations  We will consider how to implement: Selection ( ) Selects a subset of rows from relation.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Overview of Query Evaluation Chapter 12.
Overview of Query Evaluation R&G Chapter 12 Lecture 13.
Query Optimization II R&G, Chapters 12, 13, 14 Lecture 9.
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.
Overview of Implementing Relational Operators and Query Evaluation
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Query Evaluation Chapter 12: Overview.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Overview of Query Evaluation Chapter 12.
1 Overview of Query Evaluation Chapter Overview of Query Evaluation  Plan : Tree of R.A. ops, with choice of alg for each op.  Each operator typically.
CPSC 404, Laks V.S. Lakshmanan1 Evaluation of Relational Operations: Other Operations Chapter 14 Ramakrishnan & Gehrke (Sections ; )
Lec3/Database Systems/COMP4910/031 Evaluation of Relational Operations Chapter 14.
Copyright © Curt Hill Query Evaluation Translating a query into action.
Database Systems/comp4910/spring20031 Evaluation of Relational Operations Why does a DBMS implements several algorithms for each algebra operation? What.
1 Database Systems ( 資料庫系統 ) December 3, 2008 Lecture #10.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Overview of Implementing Relational Operators and Query Evaluation Chapter 12.
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.
Relational Operator Evaluation. Overview Application Programmer (e.g., business analyst, Data architect) Sophisticated Application Programmer (e.g.,
CPSC 404, Laks V.S. Lakshmanan1 Overview of Query Evaluation Chapter 12 Ramakrishnan & Gehrke (Sections )
Database Management Systems 1 Raghu Ramakrishnan Evaluation of Relational Operations Chpt 14.
Relational Operator Evaluation. overview Projection Two steps –Remove unwanted attributes –Eliminate any duplicate tuples The expensive part is removing.
Alon Levy 1 Relational Operations v We will consider how to implement: – Selection ( ) Selects a subset of rows from relation. – Projection ( ) Deletes.
1 Database Systems ( 資料庫系統 ) Chapter 12 Overview of Query Evaluation November 22, 2004 By Hao-hua Chu ( 朱浩華 )
1 Overview of Query Evaluation Chapter Outline  Query Optimization Overview  Algorithm for Relational Operations.
Relational Operator Evaluation. Application Programmer (e.g., business analyst, Data architect) Sophisticated Application Programmer (e.g., SAP admin)
Implementation of Relational Operations Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein, Mike Franklin, and etc for.
Database Management System
Storage and Indexes Chapter 8 & 9
CS222P: Principles of Data Management Lecture #15 Query Optimization (System-R) Instructor: Chen Li.
Chapter 12: Query Processing
Introduction to Query Optimization
Evaluation of Relational Operations
Evaluation of Relational Operations: Other Operations
Introduction to Database Systems
Examples of Physical Query Plan Alternatives
Database Applications (15-415) DBMS Internals- Part VI Lecture 18, Mar 25, 2018 Mohammad Hammoud.
Relational Operations
Implementing Relational Operators Query Evaluation
CS222P: Principles of Data Management Notes #11 Selection, Projection
Database Applications (15-415) DBMS Internals- Part VI Lecture 15, Oct 23, 2016 Mohammad Hammoud.
Relational Query Optimization
Overview of Query Evaluation
Overview of Query Evaluation
Overview of Query Evaluation
Implementation of Relational Operations
CS222: Principles of Data Management Notes #11 Selection, Projection
Evaluation of Relational Operations: Other Techniques
Overview of Query Evaluation: JOINS
Database Systems (資料庫系統)
Relational Query Optimization (this time we really mean it)
Overview of Query Evaluation
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:

Overview of Query Evaluation Chapter 12 The slides for this text are organized into chapters. This lecture covers Chapter 12, providing an overview of query optimization and execution. This chapter is the first of a sequence (Chapters 12, 13, 14, 15) on query evaluation that might be covered in full in a course with a systems emphasis. It can also be used stand-alone, as a self-contained overview of these issues, in a course with an application emphasis. It covers the essential concepts in sufficient detail to support a discussion of physical database design and tuning in Chapter 20. 1

Outline Query Optimization Overview Algorithm for Relational Operations

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 2

Query Plan Evaluation Query Plan Execution: Reserves Sailors sid=sid bid=100 rating > 5 sname 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 2

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!

Access Paths An access path Method: Note : 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. 13

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

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 ? 13

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

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

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(log2M)

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.

Example : Using B+ Index for Selections 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 SELECT * FROM Reserves R WHERE R.rname < ‘D%’ 12

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. 12

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.

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

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. 14

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. 15

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) 14

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.

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 ???

Query Evaluation of Projection

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

Algorithms for Projection SELECT DISTINCT R.sid, R.bid FROM 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 16

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

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