Query Execution Zachary G. Ives University of Pennsylvania CIS 650 – Implementing Data Management Systems January 24, 2005 Content on hashing and sorting.

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

External Memory Hashing. Model of Computation Data stored on disk(s) Minimum transfer unit: a page = b bytes or B records (or block) N records -> N/B.
Query Execution, Concluded Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 18, 2003 Some slide content may.
Hash-Based Indexes Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY.
Hash-based Indexes CS 186, Spring 2006 Lecture 7 R &G Chapter 11 HASH, x. There is no definition for this word -- nobody knows what hash is. Ambrose Bierce,
1 Hash-Based Indexes Module 4, Lecture 3. 2 Introduction As for any index, 3 alternatives for data entries k* : – Data record with key value k – –Choice.
Hash-Based Indexes The slides for this text are organized into chapters. This lecture covers Chapter 10. Chapter 1: Introduction to Database Systems Chapter.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Hash-Based Indexes Chapter 11.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Hash-Based Indexes Chapter 11.
CPSC 404, Laks V.S. Lakshmanan1 Hash-Based Indexes Chapter 11 Ramakrishnan & Gehrke (Sections )
Access Methods for Advanced Database Applications.
Chapter 11 (3 rd Edition) Hash-Based Indexes Xuemin COMP9315: Database Systems Implementation.
Index tuning Hash Index. overview Introduction Hash-based indexes are best for equality selections. –Can efficiently support index nested joins –Cannot.
ICS 421 Spring 2010 Indexing (2) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 2/23/20101Lipyeow Lim.
Optimizing Query Execution Zachary G. Ives University of Pennsylvania CIS 650 – Implementing Data Management Systems January 26, 2005 Content on hashing.
1 Hash-Based Indexes Yanlei Diao UMass Amherst Feb 22, 2006 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
B+-tree and Hashing.
Spring 2003 ECE569 Lecture ECE 569 Database System Engineering Spring 2003 Yanyong Zhang
Indexing, Sorting, and Execution Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 18, 2004 Some slide content.
1 Hash-Based Indexes Chapter Introduction  Hash-based indexes are best for equality selections. Cannot support range searches.  Static and dynamic.
1 Hash-Based Indexes Chapter Introduction : Hash-based Indexes  Best for equality selections.  Cannot support range searches.  Static and dynamic.
Indexing and Sorting Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 22, 2005.
Spring 2004 ECE569 Lecture ECE 569 Database System Engineering Spring 2004 Yanyong Zhang
Indexing, Sorting, and Execution Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 11, 2003 Some slide content.
Sorting and Query Processing Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 29, 2005.
Query Execution Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 23, 2004.
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.
Optimizing Query Execution Zachary G. Ives University of Pennsylvania CIS 650 – Implementing Data Management Systems September 18, 2008 Content on hashing.
Hashing and Hash-Based Index. Selection Queries Yes! Hashing  static hashing  dynamic hashing B+-tree is perfect, but.... to answer a selection query.
Database Management Systems, R. Ramakrishnan and J. Gehrke 1 External Sorting Chapter 13.
1 Database Systems ( 資料庫系統 ) November 8, 2004 Lecture #9 By Hao-hua Chu ( 朱浩華 )
Query Processing Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 12, 2015.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Tree- and Hash-Structured Indexes Selected Sections of Chapters 10 & 11.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Hash-Based Indexes Chapter 11 Modified by Donghui Zhang Jan 30, 2006.
Introduction to Database, Fall 2004/Melikyan1 Hash-Based Indexes Chapter 10.
1.1 CS220 Database Systems Indexing: Hashing Slides courtesy G. Kollios Boston University via UC Berkeley.
Static Hashing (using overflow for collision managment e.g., h(key) mod M h key Primary bucket pages 1 0 M-1 Overflow pages(as separate link list) Overflow.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Indexed Sequential Access Method.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Hash-Based Indexes Chapter 10.
Query Processing CS 405G Introduction to Database Systems.
Relational Operator Evaluation. overview Projection Two steps –Remove unwanted attributes –Eliminate any duplicate tuples The expensive part is removing.
CS 540 Database Management Systems
B-Trees, Part 2 Hash-Based Indexes R&G Chapter 10 Lecture 10.
1 Database Systems ( 資料庫系統 ) November 28, 2005 Lecture #9.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Evaluation of Relational Operations Chapter 14, Part A (Joins)
GiST, Concluded, and Query Execution Zachary G. Ives University of Pennsylvania CIS 650 – Implementing Data Management Systems September 16, 2008 Content.
CS 540 Database Management Systems
COP Introduction to Database Structures
Hash-Based Indexes Chapter 11
Database Management Systems (CS 564)
Query Optimization Overview
Introduction to Database Systems
Database Query Execution
CS222: Principles of Data Management Notes #8 Static Hashing, Extendible Hashing, Linear Hashing Instructor: Chen Li.
Hash-Based Indexes R&G Chapter 10 Lecture 18
Hash-Based Indexes Chapter 10
Indexing and Sorting Zachary G. Ives November 21, 2007
CS222P: Principles of Data Management Notes #8 Static Hashing, Extendible Hashing, Linear Hashing Instructor: Chen Li.
Hashing.
Hash-Based Indexes Chapter 11
Index tuning Hash Index.
Lecture 2- Query Processing (continued)
Database Systems (資料庫系統)
LINEAR HASHING E0 261 Jayant Haritsa Computer Science and Automation
B-Trees and Sorting Zachary G. Ives April 12, 2019
Hash-Based Indexes Chapter 11
Chapter 11 Instructor: Xin Zhang
CS222/CS122C: Principles of Data Management UCI, Fall 2018 Notes #07 Static Hashing, Extendible Hashing, Linear Hashing Instructor: Chen Li.
Presentation transcript:

Query Execution Zachary G. Ives University of Pennsylvania CIS 650 – Implementing Data Management Systems January 24, 2005 Content on hashing and sorting courtesy Ramakrishnan & Gehrke

2 Today’s Trivia Question

3 Query Execution What are the goals?  Logical vs. physical plans – what are the differences?  Some considerations in building execution engines:  Efficiency – minimize copying, comparisons  Scheduling – make standard code-paths fast  Data layout – how to optimize cache behavior, buffer management, distributed execution, etc.

4 Execution System Architectures  Central vs. distributed vs. parallel vs. mediator  Data partitioning – vertical vs. horizontal  Monet model – binary relations  Distributed – data placement  One operation at a time – INGRES  Pipelined  Iterator-driven  Dataflow-driven  Hybrid approaches

5 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

6 Iterator-Based Query Execution  Execution begins at root  open, next, close  Propagate calls to children May call multiple child nexts  “Synchronous pipelining” Minimize copies  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

7 The Simplest Method Iteration over tables  Sequential scan  Nested loops join  What’s the cost? What tricks might we use to speed it up?  Optimizations:  Double-buffering  Overlap I/O and computation  Prefetch a page into a shadow block while CPU processes different block  Requires second buffer to prefetch into  Switch to that when the CPU is finished with the alternate buffer  Alternate the direction of reads in file scan

8 Speeding Operations over Data Three general data organization techniques:  Indexing  Associative lookup & synopses  Sorting  Hashing

9 Indices GiST and B+ Trees Alternatives for storage:  ; ;  Clustered vs. unclustered Bitmapped index – bit position for each value in the domain  Requires a domain with discrete values (not necessarily ordinal)  Booleans; enumerations; range- bounded integers  Low-update data  Efficient for AND, OR only expressions between different predicates

10 Usefulness of Indices Where are these structures most useful?  Sargable predicates  Covering indices In many cases, only help with part of the story  Filter part of the answer set, but we still need further computation  e.g., AND or OR of two predicates General rule of thumb:  Unclustered index only useful if selectivity is < 10-20%

Sorting – External Binary 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

14 Applicability of Sort Techniques  Join  Intersection  Aggregation  Duplicate removal as an instance of aggregation  XML nesting as an instance of aggregation

15 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  Maintain a “last in sequence” pointer  Preserves sorted order of “outer” relation  Cost: b(R) + b(S) plus sort costs, if necessary In practice, approximately linear, 3 (b(R) + b(S))

16 Hashing Several types of hashing:  Static hashing  Extensible hashing  Consistent hashing (used in P2P; we’ll see later)

Static Hashing  Fixed number of buckets (and pages); overflow when necessary  h(k) mod N = bucket to which data entry with key k belongs  Downside: long overflow chains h(key) mod N h key Primary bucket pages Overflow pages 2 0 N-1

Extendible Hashing If a bucket becomes full split in half  Use directory of pointers to buckets, double the directory, splitting just the bucket that overflowed  Directory much smaller than file, so doubling it is much cheaper  Only one page of data entries is split  Trick lies in how hash function is adjusted!

Example  Directory is array of size 4.  For r’s bucket, take last ‘global depth’ # bits of h(r); we denote r by h(r)  If h(r) = 5 = binary 101, it is in bucket pointed to by 01  Insert: If bucket is full, split it (allocate new page, re-distribute)  If necessary, double directory. (As we will see, splitting a bucket does not always require doubling; we can tell by comparing global depth with local depth for the split bucket.) 13* LOCAL DEPTH GLOBAL DEPTH DIRECTORY Bucket A Bucket B Bucket C Bucket D DATA PAGES 10* 1*21* 4*12*32* 16* 15*7*19* 5*

Insert h(r)=20 (Causes Doubling) 20* LOCAL DEPTH 2 2 DIRECTORY GLOBAL DEPTH Bucket A Bucket B Bucket C Bucket D Bucket A2 (`split image' of Bucket A) 1* 5*21*13* 32* 16* 10* 15*7*19* 4*12* 19* DIRECTORY Bucket A Bucket B Bucket C Bucket D Bucket A2 (‘split image' of Bucket A) 32* 1*5*21*13* 16* 10* 15* 7* 4* 20* 12* LOCAL DEPTH GLOBAL DEPTH

Points to Note 20 = binary  Last 2 bits (00)  r belongs in A or A2  Last 3 bits needed to tell which  Global depth of directory: Max # of bits needed to tell which bucket an entry belongs to  Local depth of a bucket: # of bits used to determine if an entry belongs to this bucket When does bucket split cause directory doubling?  Before insert, local depth of bucket = global depth  Insert causes local depth to become > global depth; directory is doubled by copying it over and `fixing’ pointer to split image page  (Use of least significant bits enables efficient doubling via copying of directory!)

Comments on Extendible Hashing If directory fits in memory, equality search answered with one disk access; else two  Directory grows in spurts, and, if the distribution of hash values is skewed, directory can grow large  Multiple entries with same hash value cause problems! Delete:  If removal of data entry makes bucket empty, can be merged with ‘split image’  If each directory element points to same bucket as its split image, can halve directory

23 Relevance of Hashing Techniques  Hash indices use extensible hashing  Uses of static hashing:  Aggregation  Intersection  Joins

24 Hash Join Read entire inner relation into hash table (join attributes as key) For each tuple from outer, look up in hash table & join  Not fully pipelined

25 Running out of Memory  Prevention: First partition the data by value into memory- sized groups Partition both relations in the same way, write to files Recursively join the partitions  Resolution: Similar, but do 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))

26 The Duality of Hash and Sort Different means of partitioning and merging data when comparisons are necessary:  Break on physical rule (mem size) in sorting  Merge on logical step, the merge  Break on logical rule (hash val) in hashing  Combine using physical step (concat)  When larger-than-memory sorting is necessary, multiple operators use the same key, we can make all operators work on the same in-memory portion of data at the same time  Can we do this with hashing? Hash teams (Graefe)

27

28 What If I Want to Distribute Query Processing?  Where do I put the data in the first place (or do I have a choice)?  How do we get data from point A -> point B?  What about delays?  What about “binding patterns”?  Looks kind of like an index join with a sargable predicate

29 Pipelined Hash Join Useful for Joining Web Sources  Two hash tables  As a tuple comes in, add to the appropriate side & join with opposite table  Fully pipelined, adaptive to source data rates  Can handle overflow as with hash join  Needs more memory

30 The Semi-Join/Dependent Join  Take attributes from left and feed to the right source as input/filter  Important in data integration  Simple method: for each tuple from left send to right source get data back, join  More complex:  Hash “cache” of attributes & mappings  Don’t send attribute already seen  Bloom joins (use bit-vectors to reduce traffic) Join A.x = B.y AB x

31 Wrap-Up Query execution is all about engineering for efficiency  O(1) and O(lg n) algorithms wherever possible  Avoid looking at or copying data wherever possible  Note that larger-than-memory is of paramount importance  Should that be so in today’s world? As we’ve seen it so far, it’s all about pipelining things through as fast as possible But may also need to consider other axes:  Adaptivity/flexibility – may sometimes need this  Information flow – to the optimizer, the runtime system

32 Upcoming Readings For Wednesday:  Read Chaudhuri survey as an overview  Read and review Selinger et al. paper For Monday:  Read EXODUS and Starburst papers  Write one review contrasting the two on the major issues