Sorting and Query Processing Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 29, 2005.

Slides:



Advertisements
Similar presentations
Equality Join R X R.A=S.B S : : Relation R M PagesN Pages Relation S Pr records per page Ps records per page.
Advertisements

Query Execution, Concluded Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 18, 2003 Some slide content may.
Lecture 13: Query Execution. Where are we? File organizations: sorted, hashed, heaps. Indexes: hash index, B+-tree Indexes can be clustered or not. Data.
CMSC424: Database Design Instructor: Amol Deshpande
Database Management Systems 3ed, R. Ramakrishnan and Johannes Gehrke1 Evaluation of Relational Operations: Other Techniques Chapter 14, Part B.
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.
Optimizing Query Execution Zachary G. Ives University of Pennsylvania CIS 650 – Implementing Data Management Systems January 26, 2005 Content on hashing.
Lecture 24: Query Execution Monday, November 20, 2000.
1  Simple Nested Loops Join:  Block Nested Loops Join  Index Nested Loops Join  Sort Merge Join  Hash Join  Hybrid Hash Join Evaluation of Relational.
SPRING 2004CENG 3521 Join Algorithms Chapter 14. SPRING 2004CENG 3522 Schema for Examples Similar to old schema; rname added for variations. Reserves:
Indexing, Sorting, and Execution Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 18, 2004 Some slide content.
Query Optimization 3 Cost Estimation R&G, Chapters 12, 13, 14 Lecture 15.
Indexing and Sorting Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 22, 2005.
Query Optimization Overview Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems December 1, 2005 Some slide content derived.
Introduction to Database Systems 1 Join Algorithms Query Processing: Lecture 1.
Indexing, Sorting, and Execution Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 11, 2003 Some slide content.
1 Evaluation of Relational Operations: Other Techniques Chapter 12, Part B.
Query Execution Zachary G. Ives University of Pennsylvania CIS 650 – Implementing Data Management Systems January 24, 2005 Content on hashing and sorting.
Evaluation of Relational Operations. Relational Operations v We will consider how to implement: – Selection ( ) Selects a subset of rows from relation.
Query Execution Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 23, 2004.
1 Query Execution in Databases: An Introduction Zack Ives CSE 544 Spring 2000.
1 Database Query Execution Zack Ives CSE Principles of DBMS Ullman Chapter 6, Query Execution Spring 1999.
1 Relational Operators. 2 Outline Logical/physical operators Cost parameters and sorting One-pass algorithms Nested-loop joins Two-pass algorithms.
CSCE Database Systems Chapter 15: Query Execution 1.
Optimizing Query Execution Zachary G. Ives University of Pennsylvania CIS 650 – Implementing Data Management Systems September 18, 2008 Content on hashing.
©Silberschatz, Korth and Sudarshan13.1Database System Concepts Chapter 13: Query Processing Overview Measures of Query Cost Selection Operation Sorting.
Query Processing. Steps in Query Processing Validate and translate the query –Good syntax. –All referenced relations exist. –Translate the SQL to relational.
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.
RELATIONAL JOIN Advanced Data Structures. Equality Joins With One Join Column External Sorting 2 SELECT * FROM Reserves R1, Sailors S1 WHERE R1.sid=S1.sid.
Query Processing Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 12, 2015.
Chapter 12 Query Processing. Query Processing n Selection Operation n Sorting n Join Operation n Other Operations n Evaluation of Expressions 2.
CS4432: Database Systems II Query Processing- Part 3 1.
CS411 Database Systems Kazuhiro Minami 11: Query Execution.
CS4432: Database Systems II Query Processing- Part 2.
CSCE Database Systems Chapter 15: Query Execution 1.
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.
Lecture 3 - Query Processing (continued) Advanced Databases Masood Niazi Torshiz Islamic Azad university- Mashhad Branch
File Processing : Query Processing 2008, Spring Pusan National University Ki-Joune Li.
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.
CS 540 Database Management Systems
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.
Query Processing and Query Optimization Database System Implementation CSE 507 Some slides adapted from Silberschatz, Korth and Sudarshan Database System.
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.
CS4432: Database Systems II Query Processing- Part 1 1.
CS 540 Database Management Systems
CS 440 Database Management Systems
Database Management System
Evaluation of Relational Operations
Database Management Systems (CS 564)
File Processing : Query Processing
Query Optimization Overview
Database Query Execution
Indexing and Sorting Zachary G. Ives November 21, 2007
Query Optimization Overview
Lecture 2- Query Processing (continued)
Lecture 13: Query Execution
Database Internals Zachary Ives CSE 594 Spring 2002
Evaluation of Relational Operations: Other Techniques
B-Trees and Sorting Zachary G. Ives April 12, 2019
Sorting We may build an index on the relation, and then use the index to read the relation in sorted order. May lead to one disk block access for each.
Lecture 11: B+ Trees and Query Execution
Presentation transcript:

Sorting and Query Processing Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 29, 2005

2 Speeding Operations over Data  Three general data organization techniques:  Indexing  Sorting  Hashing

Technique II: Sorting Pass 1: Read a page, sort it, write it Can use a single page to do this! Pass 2, 3, …, etc.: Requires a minimum of 3 pages Main memory buffers INPUT 1 INPUT 2 OUTPUT Disk

Two-Way External Merge Sort Divide and conquer: sort into subfiles and merge Each pass: we read & write every page If N pages in the file, we need: d log 2 (N) e + 1 passes to sort the data, yielding a cost of: 2N d log 2 (N) e + 1 Input file 1-page runs 2-page runs 4-page runs 8-page runs PASS 0 PASS 1 PASS 2 PASS 3 9 3,46,29,48,75,63,12 3,4 5,62,64,97,8 1,32 2,3 4,6 4,7 8,9 1,3 5,62 2,3 4,4 6,7 8,9 1,2 3,5 6 1,2 2,3 3,4 4,5 6,6 7,8

General External Merge Sort  To sort a file with N pages using B buffer pages:  Pass 0: use B buffer pages. Produce d N / B e sorted runs of B pages each  Pass 2, …, etc.: merge B-1 runs B Main memory buffers INPUT 1 INPUT B-1 OUTPUT Disk INPUT 2...  How can we utilize more than 3 buffer pages?

Cost of External Merge Sort  Number of passes: 1+ d log B-1 d N / B ee  Cost = 2N * (# of passes)  With 5 buffer pages, to sort 108 page file:  Pass 0: d 108/5 e = 22 sorted runs of 5 pages each (last run is only 3 pages)  Pass 1: d 22/4 e = 6 sorted runs of 20 pages each (final run only uses 8 pages)  Pass 2: d 6/4 e = 2 sorted runs, 80 pages and 28 pages  Pass 3: Sorted file of 108 pages

7 Speeding Operations over Data Three general data organization techniques:  Indexing  Sorting  Hashing

8 Technique 3: Hashing A familiar idea, which we just saw for hash files:  Requires “good” hash function (may depend on data)  Distribute data across buckets  Often multiple items in same bucket (buckets might overflow) Hash indices can be built along the same lines as what we discussed  The difference: they may be unclustered as well as clustered  Types:  Static  Extendible (requires directory to buckets; can split)  Linear (two levels, rotate through + split; bad with skew)  We won’t get into detail because of time, but see text

9 Making Use of the Data + Indices: Query Execution  Query plans & exec strategies  Basic principles  Standard relational operators  Querying XML

10 Making Use of the Data + Indices: Query Execution  Query plans & exec strategies  Basic principles  Standard relational operators  Querying XML

11 Query Plans  Data-flow graph of relational algebra operators  Typically: determined by optimizer Select Client = “Atkins” Join PressRel.Symbol = Clients.Symbol Scan PressRel Scan Clients Join PressRel.Symbol = EastCoast.CoSymbol Project CoSymbol Scan EastCoast SELECT * FROM PressRel p, Clients C WHERE p.Symbol = c.Symbol AND c.Client = ‘Atkins’ AND c.Symbol IN (SELECT CoSymbol FROM EastCoast)

12 Iterator-Based Query Execution  Execution begins at root  open, next, close  Propagate calls to children May call multiple child nexts  Efficient scheduling & resource usage Can you think of alternatives and their benefits? Select Client = “Atkins” Join PressRel.Symbol = Clients.Symbol Scan PressRel Scan Clients Join PressRel.Symbol = EastCoast.CoSymbol Project CoSymbol Scan EastCoast

13 Execution Strategy Issues Granularity & parallelism:  Pipelining vs. blocking  Materialization Select Client = “Atkins” Join PressRel.Symbol = Clients.Symbol Scan PressRel Scan Clients Join PressRel.Symbol = EastCoast.CoSymbol Project CoSymbol Scan EastCoast

14 Basic Principles  Many DB operations require reading tuples, tuple vs. previous tuples, or tuples vs. tuples in another table  Techniques generally used:  Iteration: for/while loop comparing with all tuples on disk  Index: if comparison of attribute that’s indexed, look up matches in index & return those  Sort/merge: iteration against presorted data (interesting orders)  Hash: build hash table of the tuple list, probe the hash table  Must be able to support larger-than-memory data

15 Basic Operators  One-pass operators:  Scan  Select  Project  Multi-pass operators:  Join  Various implementations  Handling of larger-than-memory sources  Semi-join  Aggregation, union, etc.

16 1-Pass Operators: Scanning a Table  Sequential scan: read through blocks of table  Index scan: retrieve tuples in index order  May require 1 seek per tuple! When?  Cost in page reads – b(T) blocks, r(T) tuples  b(T) pages for sequential scan  Up to r(T) for index scan if unclustered index  Requires memory for one block

17 1-Pass Operators: Select(  )  Typically done while scanning a file  If unsorted & no index, check against predicate: Read tuple While tuple doesn’t meet predicate Read tuple Return tuple  Sorted data: can stop after particular value encountered  Indexed data: apply predicate to index, if possible  If predicate is:  conjunction: may use indexes and/or scanning loop above (may need to sort/hash to compute intersection)  disjunction: may use union of index results, or scanning loop

18 1-Pass Operators: Project (  )  Simple scanning method often used if no index: Read tuple While tuple exists Output specified attributes Read tuple  Duplicate removal may be necessary  Partition output into separate files by bucket, do duplicate removal on those  If have many duplicates, sorting may be better  If attributes belong to an index, don’t need to retrieve tuples!

19 Multi-pass Operators: Join ( ⋈ ) – Nested-Loops Join  Requires two nested loops: For each tuple in outer relation For each tuple in inner, compare If match on join attribute, output  Results have order of outer relation  Can do over indices  Very simple to implement, supports any joins predicates  Supports any join predicates  Cost: # comparisons = t(R) t(S) # disk accesses = b(R) + t(R) b(S) Join outerinner

20 Block Nested-Loops Join  Join a page (block) at a time from each table: For each page in outer relation For each page in inner, join both pages If match on join attribute, output  More efficient than previous approach:  Cost: # comparisons still = t(R) t(S) # disk accesses = b(R) + b(R) * b(S)

21 Index Nested-Loops Join For each tuple in outer relation For each match in inner’s index Retrieve inner tuple + output joined tuple  Cost: b(R) + t(R) * cost of matching in S  For each R tuple, costs of probing index are about:  1.2 for hash index, 2-4 for B+-tree and:  Clustered index: 1 I/O on average  Unclustered index: Up to 1 I/O per S tuple

22 Two-Pass Algorithms Sort-based Need to do a multiway sort first (or have an index) Approximately linear in practice, 2 b(T) for table T Hash-based Store one relation in a hash table

23 (Sort-)Merge Join  Requires data sorted by join attributes Merge and join sorted files, reading sequentially a block at a time  Maintain two file pointers  While tuple at R < tuple at S, advance R (and vice versa)  While tuples match, output all possible pairings  Preserves sorted order of “outer” relation  Very efficient for presorted data  Can be “hybridized” with NL Join for range joins  May require a sort before (adds cost + delay)  Cost: b(R) + b(S) plus sort costs, if necessary In practice, approximately linear, 3 (b(R) + b(S))

24 Hash-Based Joins  Allows partial pipelining of operations with equality comparisons  Sort-based operations block, but allow range and inequality comparisons  Hash joins usually done with static number of hash buckets  Generally have fairly long chains at each bucket  What happens when memory is too small?

25 Hash Join Read entire inner relation into hash table (join attributes as key) For each tuple from outer, look up in hash table & join  Very efficient for equality

26 Running out of Memory  Resolution: When hash tables full Split hash table into files along bucket boundaries Partition remaining data in same way Recursively join partitions with diff. hash fn!  Hybrid hash join: flush “lazily” a few buckets at a time  Cost: <= 3 * (b(R) + b(S))

27 Aggregation (  )  Need to store entire table, coalesce groups with matching GROUP BY attributes  Compute aggregate function over group:  If groups are sorted or indexed, can iterate:  Read tuples while attributes match, compute aggregate  At end of each group, output result  Hash approach:  Group together in hash table (leave space for agg values!)  Compute aggregates incrementally or at end  At end, return answers  Cost: b(t) pages. How much memory?

28 Other Operators  Duplicate removal very similar to grouping  All attributes must match  No aggregate  Union, difference, intersection:  Read table R, build hash/search tree  Read table S, add/discard tuples as required  Cost: b(R) + b(S)

29 SQL Operations In a whirlwind, you’ve seen most of relational operators:  Select, Project, Join  Group/aggregate  Union, Difference, Intersection  Others are used sometimes:  Various methods of “for all,” “not exists,” etc  Recursive queries/fixpoint operator  etc.

30 What about XQuery?  Major difference: bind variables to subtrees; treat each set of bindings as a tuple  Select, project, join, etc. on tuples of bindings  Plus we need some new operators:  XML construction:  Create element (add tags around data)  Add attribute(s) to element (similar to join)  Nest element under other element (similar to join)  Path expression evaluation – create the binding tuples