1 Query Processing: The Basics Chapter 13. 2 Topics How does DBMS compute the result of a SQL queries? The most often executed operations: –Sort –Projection,

Slides:



Advertisements
Similar presentations
Lecture 13: Query Execution. Where are we? File organizations: sorted, hashed, heaps. Indexes: hash index, B+-tree Indexes can be clustered or not. Data.
Advertisements

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.
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;
1 Chapter 10 Query Processing: The Basics. 2 External Sorting Sorting is used in implementing many relational operations Problem: –Relations are typically.
Lecture 24: Query Execution Monday, November 20, 2000.
Evaluation of Relational Operators 198:541. Relational Operations  We will consider how to implement: Selection ( ) Selects a subset of rows from relation.
1  Simple Nested Loops Join:  Block Nested Loops Join  Index Nested Loops Join  Sort Merge Join  Hash Join  Hybrid Hash Join Evaluation of Relational.
1 Overview of Storage and Indexing Chapter 8 (part 1)
CSCI 5708: Query Processing I Pusheng Zhang University of Minnesota Feb 3, 2004.
Query Optimization 3 Cost Estimation R&G, Chapters 12, 13, 14 Lecture 15.
1 Evaluation of Relational Operations: Other Techniques Chapter 12, Part B.
CSCI 5708: Query Processing I Pusheng Zhang University of Minnesota Feb 3, 2004.
Chapter 19 Query Processing and Optimization
1 Evaluation of Relational Operations Yanlei Diao UMass Amherst March 01, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Evaluation of Relational Operations. Relational Operations v We will consider how to implement: – Selection ( ) Selects a subset of rows from relation.
1 Relational Operators. 2 Outline Logical/physical operators Cost parameters and sorting One-pass algorithms Nested-loop joins Two-pass algorithms.
1 Physical Data Organization and Indexing Lecture 14.
©Silberschatz, Korth and Sudarshan13.1Database System Concepts Chapter 13: Query Processing Overview Measures of Query Cost Selection Operation Sorting.
CPSC 404, Laks V.S. Lakshmanan1 Evaluation of Relational Operations: Other Operations Chapter 14 Ramakrishnan & Gehrke (Sections ; )
Relational Operator Evaluation. Overview Index Nested Loops Join If there is an index on the join column of one relation (say S), can make it the inner.
Lec3/Database Systems/COMP4910/031 Evaluation of Relational Operations Chapter 14.
Copyright © Curt Hill Query Evaluation Translating a query into action.
Chapter 12 Query Processing. Query Processing n Selection Operation n Sorting n Join Operation n Other Operations n Evaluation of Expressions 2.
Chapter 12 Query Processing (1) Yonsei University 2 nd Semester, 2013 Sanghyun Park.
CS411 Database Systems Kazuhiro Minami 11: Query Execution.
CS 257 Chapter – 15.9 Summary of Query Execution Database Systems: The Complete Book Krishna Vellanki 124.
ICOM 6005 – Database Management Systems Design Dr. Manuel Rodríguez-Martínez Electrical and Computer Engineering Department Lecture 13 – Query Evaluation.
Relational Operator Evaluation. Overview Application Programmer (e.g., business analyst, Data architect) Sophisticated Application Programmer (e.g.,
Advance Database Systems Query Optimization Ch 15 Department of Computer Science The University of Lahore.
CPSC 404, Laks V.S. Lakshmanan1 Evaluation of Relational Operations – Join Chapter 14 Ramakrishnan and Gehrke (Section 14.4)
Query Processing CS 405G Introduction to Database Systems.
Lecture 17: Query Execution Tuesday, February 28, 2001.
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.
CS 440 Database Management Systems Lecture 5: Query Processing 1.
Database Management Systems 1 Raghu Ramakrishnan Evaluation of Relational Operations Chpt 14.
Hash Tables and Query Execution March 1st, Hash Tables Secondary storage hash tables are much like main memory ones Recall basics: –There are n.
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 Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Evaluation of Relational Operations Chapter 14, Part A (Joins)
1 Lecture 23: Query Execution Monday, November 26, 2001.
1 Overview of Query Evaluation Chapter Outline  Query Optimization Overview  Algorithm for Relational Operations.
Chapter 10 The Basics of Query Processing. Copyright © 2005 Pearson Addison-Wesley. All rights reserved External Sorting Sorting is used in implementing.
Relational Operator Evaluation. Application Programmer (e.g., business analyst, Data architect) Sophisticated Application Programmer (e.g., SAP admin)
Database Management System
Chapter 12: Query Processing
Evaluation of Relational Operations
Evaluation of Relational Operations: Other Operations
Relational Operations
CS222P: Principles of Data Management Notes #11 Selection, Projection
Database Applications (15-415) DBMS Internals- Part VI Lecture 15, Oct 23, 2016 Mohammad Hammoud.
Selected Topics: External Sorting, Join Algorithms, …
Lecture 2- Query Processing (continued)
Chapter 12 Query Processing (1)
Overview of Query Evaluation
Implementation of Relational Operations
Lecture 13: Query Execution
CS222: Principles of Data Management Notes #11 Selection, Projection
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
CS222/CS122C: Principles of Data Management UCI, Fall 2018 Notes #10 Selection, Projection Instructor: Chen Li.
Presentation transcript:

1 Query Processing: The Basics Chapter 13

2 Topics How does DBMS compute the result of a SQL queries? The most often executed operations: –Sort –Projection, Union, Intersection, Set Difference –Selection –Join

3 External Sorting Sorting is used in implementing many relational operations Problem: –Relations are typically large, do not fit in main memory –So cannot use traditional in-memory sorting algorithms Approach used: –Combine in-memory sorting with clever techniques aimed at minimizing I/O –I/O costs dominate => cost of sorting algorithm is measured in the number of page transfers

4 External Sorting (cont’d) External sorting has two main components: –Computation involved in sorting records in buffers in main memory –I/O necessary to move records between mass store and main memory

5 Simple Sort Algorithm M = number of main memory page buffers F = number of pages in file to be sorted Typical algorithm has two phases: runs –Partial sort phase: sort M pages at a time; create F/M sorted runs on mass store, cost = 2F Example: M = 2, F = 7 run Original file Partially sorted file

6 Simple Sort Algorithm – Merge Phase: merge all runs into a single run using M-1 buffers for input and 1 output buffer Merge step: divide runs into groups of size M-1 and merge each group into a run; cost = 2F each step reduces number of runs by a factor of M-1 M pages Buffer

7 Simple Sort Algorithm Cost of merge phase: –(F/M)/(M-1) k runs after k merge steps –  Log M-1 (F/M)  merge steps needed to merge an initial set of F/M sorted runs –cost =  2F Log M-1 (F/M)   2F(Log M-1 F -1) Total cost = cost of partial sort phase + cost of merge phase  2F Log M-1 F

8 Duplicate Elimination A major step in computing projection, union, and difference relational operators Algorithm: –Sort –At the last stage of the merge step eliminate duplicates on the fly –No additional cost (with respect to sorting) in terms of I/O

9 Sort-Based Projection Algorithm: –Sort rows of relation at cost of 2F Log M-1 F –Eliminate unwanted columns in partial sort phase (no additional cost) –Eliminate duplicates on completion of last merge step (no additional cost) Cost: the cost of sorting

10 Hash-Based Projection Phase 1: –Input rows –Project out columns –Hash remaining columns using a hash function with range 1…M-1 creating M-1 buckets on disk –Cost = 2F Phase 2: –Sort each bucket to eliminate duplicates –Cost (assuming a bucket fits in M-1 buffer pages) = 2F Total cost = 4F M pages Buffer

11 Computing Selection  (attr op value) No index on attr: –If rows are not sorted on attr: Scan all data pages to find rows satisfying selection condition Cost = F –If rows are sorted on attr and op is =, >, < then: Use binary search (at log 2 F ) to locate first data page containing row in which (attr = value) Scan further to get all rows satisfying (attr op value) Cost = log 2 F + (cost of scan)

12 Computing Selection  (attr op value) Clustered B + tree index on attr (for “=” or range search): Cost –Locate first index entry corresponding to a row in which (attr = value). Cost = depth of tree –Rows satisfying condition packed in sequence in successive data pages; scan those pages. Cost Cost: number of pages occupied by qualifying rows B + tree index entries (containing rows) that satisfy condition

13 Computing Selection  (attr op value) Unclustered B + tree index on attr (for “=” or range search): –Locate first index entry corresponding to a row in which (attr = value). Cost Cost = depth of tree –Index entries with pointers to rows satisfying condition are packed in sequence in successive index pages Scan entries and sort record Ids to identify table data pages with qualifying rows Any page that has at least one such row must be fetched once. CostCost: number of rows that satisfy selection condition

14 Unclustered B + Tree Index index entries (containing row Ids) that satisfy condition data page Data file B + Tree

15 Computing Selection  (attr = value) Hash index on attr (for “=” search only): Cost –Hash on value. Cost  – typical average cost of hashing (> 1 due to possible overflow chains) Finds the (unique) bucket containing all index entries satisfying selection condition Clustered index – all qualifying rows packed in the bucket (a few pages) Cost Cost: number of pages occupies by the bucket Unclustered index – sort row Ids in the index entries to identify data pages with qualifying rows Each page containing at least one such row must be fetched once Cost Cost: number of rows in bucket

16 Computing Selection  (attr = value) Unclustered hash index on attr (for equality search) buckets data pages

17 Access Path Access pathAccess path is the notion that denotes algorithm + data structure used to locate rows satisfying some condition Examples: –File scan: can be used for any condition –Hash: equality search; all search key attributes of hash index are specified in condition –B + tree: equality or range search; a prefix of the search key attributes are specified in condition B + tree supports a variety of access paths –Binary search: Relation sorted on a sequence of attributes and some prefix of that sequence is specified in condition

18 Access Paths Supported by B + tree Example: Given a B + tree whose search key is the sequence of attributes a2, a1, a3, a4 –Access path for search  a1>5  a2=3  a3=‘x’ (R): find first entry having a2=3  a1>5  a3=‘x’ and scan leaves from there until entry having a2>3 or a3  ‘x’. Select satisfying entries –Access path for search  a2=3  a3 >‘x’ (R): locate first entry having a2=3 and scan leaves until entry having a2>3. Select satisfying entries –Access path for search  a1>5  a3 =‘x’ (R): Scan of R

19 Choosing an Access Path SelectivitySelectivity of an access path = number of pages retrieved using that path If several access paths support a query, DBMS chooses the one with lowest selectivity Size of domain of attribute is an indicator of the selectivity of search conditions that involve that attribute Example:  CrsCode=‘CS305’  Grade=‘B’ –a B + tree with search key CrsCode has lower selectivity than a B + tree with search key Grade

20 Computing Joins The cost of joining two relations makes the choice of a join algorithm crucial Block-nested loopsBlock-nested loops join algorithm for computing r A=B s foreach page p r in r do foreach tuple t r in p r do foreach page p s in s do foreach tuple t s in p s do if t r. A = t s.B then output (t r, t s )

21 Block-Nested Loops Join If  r and  s are the number of pages in r and s, the cost of algorithm is  r +  r   s + cost of outputting final result –If r and s have 10 3 pages each, cost is * 10 3 –Choose smaller relation for the outer loop: If  r <  s then  r +  r   s <  s +  r   s Number of scans of relation s

22 Block-Nested Loops Join Cost can be reduced to  r + (  r /(M-2))   s + cost of outputting final result by using M buffer pages instead of 1. Number of scans of relation s

23 Index-Nested Loop Join r A= B s Use an index on s with search key B (instead of scanning s) to find rows of s that match t r –Cost –Cost =  r +  r   + cost of outputting final result –Effective if number of rows of s that match tuples in r is small (i.e.,  << 1) and index is clustered foreach tuple t r in r do { use index to find all tuples t s in s satisfying t r.A=t s.B; output (t r, t s ) } Number of rows in r avg cost of retrieving all rows in s that match t r

24 Sort-Merge Join r A= B s sort r on A; sort s on B; while !eof(r) and !eof(s) do { scan r and s concurrently until t r.A=t s.B; if (t r.A=t s.B=c) { output  A=c (r)  B=c (s) } } r s   B=c (s)  A=c (r)

25 Sort-Merge Join CostCost of sorting assuming M buffers: 2  r log M-1  r + 2  s log M-1  s + cost of final result CostCost of merging depends on whether  A=c (r) and  B=c (s) can be fit in buffers –If yes, merge step takes  r +  s –In no, then each  A=c (r)  B=c (s) has to be computed using block-nested join. (Think why indexed methods or sort-merge are inapplicable.)

26 Hash-Join r A= B s Step 1: Hash r on A and s on B into the same set of buckets Step 2: Since matching tuples must be in same bucket, read each bucket in turn and output the result of the join Cost:Cost: 3 (  r +  s ) + cost of output of final result –assuming each bucket fits in memory

27 Hash Join

28 Star Joins r cond 1 r 1 cond 2 … cond n r n –Each cond i involves only the attributes of r i and r r r1r1 r2r2 r3r3 r4r4 r5r5 cond 1 cond 2 cond 3 cond 4 cond 5 Star relation Satellite relations

29 Star Join

30 Computing Star Joins join indexUse join index (Chapter 11) –Scan r and the join index { } (which is a set of tuples of rids) in one scan –Retrieve matching tuples in r 1,…,r n –Output result

31 Computing Star Joins bitmap indicesUse bitmap indices (Chapter 11) –Use one bitmapped join index, J i, per each partial join r cond i r i –Recall: J i is a set of, where v is an rid of a tuple in r i and bitmap has 1 in k-th position iff k-th tuple of r joins with the tuple pointed to by v 1.Scan J i and logically OR all bitmaps. We get all rids in r that join with r i 2.Now logically AND the resulting bitmaps for J 1, …, J n. 3.Result: a subset of r, which contains all tuples that can possibly be in the star join Rationale: only a few such tuples survive, so can use indexed loops

32 Choosing Indices DBMSs may allow user to specify –Type (hash, B + tree) and search key of index –Whether or not it should be clustered Using information about the frequency and type of queries and size of tables, designer can use cost estimates to choose appropriate indices Several commercial systems have tools that suggest indices –Simplifies job, but index suggestions must be verified

33 Choosing Indices – Example If a frequently executed query that involves selection or a join and has a large result set, use a clustered B + tree index Transcript Example: Retrieve all rows of Transcript for StudId If a frequently executed query is an equality search and has a small result set, an unclustered hash index is best –Since only one clustered index on a table is possible, choosing unclustered allows a different index to be clustered Transcript Example: Retrieve all rows of Transcript for (StudId, CrsCode)