CS 5614: (Big) Data Management Systems. B. Aditya Prakash Lecture #5: Hashing and Sor5ng

Similar documents
Carnegie Mellon Univ. Dept. of Computer Science /615 DB Applications. Outline. (Static) Hashing. Faloutsos - Pavlo CMU SCS /615

Carnegie Mellon Univ. Dept. of Computer Science Database Applications. Why Sort? Why Sort? select... order by

Why Sort? Data requested in sorted order. Sor,ng is first step in bulk loading B+ tree index. e.g., find students in increasing GPA order

Database Applications (15-415)

External Sorting. Chapter 13. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

CS542. Algorithms on Secondary Storage Sorting Chapter 13. Professor E. Rundensteiner. Worcester Polytechnic Institute

Last Class. Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Why do we need to sort? Today's Class

Last Class Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications

CARNEGIE MELLON UNIVERSITY DEPT. OF COMPUTER SCIENCE DATABASE APPLICATIONS

External Sorting. Chapter 13. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

Symbol Table. Symbol table is used widely in many applications. dictionary is a kind of symbol table data dictionary is database management

Lecture 8 Index (B+-Tree and Hash)

Database Applications (15-415)

Module 3: Hashing Lecture 9: Static and Dynamic Hashing. The Lecture Contains: Static hashing. Hashing. Dynamic hashing. Extendible hashing.

Selection Queries. to answer a selection query (ssn=10) needs to traverse a full path.

5. Hashing. 5.1 General Idea. 5.2 Hash Function. 5.3 Separate Chaining. 5.4 Open Addressing. 5.5 Rehashing. 5.6 Extendible Hashing. 5.

Topics to Learn. Important concepts. Tree-based index. Hash-based index

Systems Infrastructure for Data Science. Web Science Group Uni Freiburg WS 2014/15

Hash-Based Indexing 1

Hash-Based Indexes. Chapter 11

CS143: Index. Book Chapters: (4 th ) , (5 th ) , , 12.10

Indexing: Overview & Hashing. CS 377: Database Systems

Data and File Structures Chapter 11. Hashing

Lecture 13. Lecture 13: B+ Tree

TUTORIAL ON INDEXING PART 2: HASH-BASED INDEXING

Kathleen Durant PhD Northeastern University CS Indexes

Chapter 6. Hash-Based Indexing. Efficient Support for Equality Search. Architecture and Implementation of Database Systems Summer 2014

Introduction to Data Management. Lecture 15 (More About Indexing)

Hashing Techniques. Material based on slides by George Bebis

Access Methods. Basic Concepts. Index Evaluation Metrics. search key pointer. record. value. Value

Hash-Based Indexes. Chapter 11. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

University of California, Berkeley. CS 186 Introduction to Databases, Spring 2014, Prof. Dan Olteanu MIDTERM

CS 5614: (Big) Data Management Systems. B. Aditya Prakash Lecture #4: Constraints, Storing and Indexes

Evaluation of Relational Operations

Module 5: Hash-Based Indexing

Module 5: Hashing. CS Data Structures and Data Management. Reza Dorrigiv, Daniel Roche. School of Computer Science, University of Waterloo

Data Organization B trees

External Sorting Implementing Relational Operators

Hashed-Based Indexing

THE B+ TREE INDEX. CS 564- Spring ACKs: Jignesh Patel, AnHai Doan

Database Applications (15-415)

CS 5614: (Big) Data Management Systems. B. Aditya Prakash Lecture #4: Constraints, Storing and Indexes

External Sorting. Chapter 13. Comp 521 Files and Databases Fall

Hashing. Given a search key, can we guess its location in the file? Goal: Method: hash keys into addresses

Introduction to Data Management. Lecture 21 (Indexing, cont.)

4 Hash-Based Indexing

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

Tree-Structured Indexes. Chapter 10

BBM371& Data*Management. Lecture 6: Hash Tables

RAID in Practice, Overview of Indexing

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

CS261 Data Structures. Maps (or Dic4onaries)

Indexing. Chapter 8, 10, 11. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

CSIT5300: Advanced Database Systems

CSc 120. Introduc/on to Computer Programming II. 15: Hashing

Find the block in which the tuple should be! If there is free space, insert it! Otherwise, must create overflow pages!

Topic HashTable and Table ADT

CSE 544 Principles of Database Management Systems. Magdalena Balazinska Winter 2009 Lecture 6 - Storage and Indexing

EXTERNAL SORTING. Sorting

NOTE: sorting using B-trees to be assigned for reading after we cover B-trees.

Hashing. Data organization in main memory or disk

Hash-Based Indexes. Chapter 11 Ramakrishnan & Gehrke (Sections ) CPSC 404, Laks V.S. Lakshmanan 1

Algorithms and Data Structures

CompSci 516: Database Systems

Advances in Data Management Principles of Database Systems - 2 A.Poulovassilis

Storage hierarchy. Textbook: chapters 11, 12, and 13

Spring 2017 EXTERNAL SORTING (CH. 13 IN THE COW BOOK) 2/7/17 CS 564: Database Management Systems; (c) Jignesh M. Patel,

Dtb Database Systems. Announcement

CS698F Advanced Data Management. Instructor: Medha Atre. Aug 11, 2017 CS698F Adv Data Mgmt 1

Chapter 17. Disk Storage, Basic File Structures, and Hashing. Records. Blocking

CMSC 341 Hashing. Based on slides from previous iterations of this course

Tree-Structured Indexes

Administrivia. Tree-Structured Indexes. Review. Today: B-Tree Indexes. A Note of Caution. Introduction

Lassonde School of Engineering Winter 2016 Term Course No: 4411 Database Management Systems

PS2 out today. Lab 2 out today. Lab 1 due today - how was it?

Tree-Structured Indexes

CS 350 Algorithms and Complexity

Tree-Structured Indexes

CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #3: SQL and Rela2onal Algebra- - - Part 1

Announcements. Reading Material. Recap. Today 9/17/17. Storage (contd. from Lecture 6)

Sorting a file in RAM. External Sorting. Why Sort? 2-Way Sort of N pages Requires Minimum of 3 Buffers Pass 0: Read a page, sort it, write it.

Hash Tables. Hash Tables

Fast Lookup: Hash tables

Problem. Indexing with B-trees. Indexing. Primary Key Indexing. B-trees: Example. B-trees. primary key indexing

Query Processing: The Basics. External Sorting

CSE373: Data Structures & Algorithms Lecture 17: Hash Collisions. Kevin Quinn Fall 2015

Introduction to Database Systems CSE 444, Winter 2011

Lecture 12. Lecture 12: The IO Model & External Sorting

Database design and implementation CMPSCI 645. Lecture 08: Storage and Indexing

Data Structure Lecture#22: Searching 3 (Chapter 9) U Kang Seoul National University

System Structure Revisited

COSC160: Data Structures Hashing Structures. Jeremy Bolton, PhD Assistant Teaching Professor

Module 4: Tree-Structured Indexing

CSE 373: Data Structure & Algorithms Comparison Sor:ng

Database Management System

key h(key) Hash Indexing Friday, April 09, 2004 Disadvantages of Sequential File Organization Must use an index and/or binary search to locate data

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

Lecture 11. Lecture 11: External Sorting

Remember. 376a. Database Design. Also. B + tree reminders. Algorithms for B + trees. Remember

CS 222/122C Fall 2017, Final Exam. Sample solutions

Transcription:

CS 5614: (Big) Data Management Systems B. Aditya Prakash Lecture #5: Hashing and Sor5ng

(Sta7c) Hashing Problem: find EMP record with ssn=123 What if disk space was free, and 5me was at premium? Prakash 2014 VT CS 5614 2

Hashing A: Brilliant idea: key- to- address transforma5on: #0 page 123; Smith; Main str #123 page #999,999,999 Prakash 2014 VT CS 5614 3

Hashing Since space is NOT free: use M, instead of 999,999,999 slots hash func5on: h(key) = slot- id #0 page 123; Smith; Main str #123 page #999,999,999 Prakash 2014 VT CS 5614 4

Hashing Typically: each hash bucket is a page, holding many records: #0 page 123; Smith; Main str #h(123) M Prakash 2014 VT CS 5614 5

Hashing No5ce: could have clustering, or non- clustering versions: #0 page 123; Smith; Main str. #h(123) M Prakash 2014 VT CS 5614 6

Hashing No5ce: could have clustering, or non- clustering versions: #0 page... EMP file #h(123)... 123 234; Johnson; Forbes ave 123; Smith; Main str. M Prakash 2014 VT CS 5614 7... 345; Tompson; Fi]h ave...

Design decisions 1) formula h() for hashing func5on 2) size of hash table M 3) collision resolu5on method Prakash 2014 VT CS 5614 8

Design decisions - func7ons Goal: uniform spread of keys over hash buckets Popular choices: Division hashing Mul5plica5on hashing Prakash 2014 VT CS 5614 9

Division hashing h(x) = (a*x+b) mod M eg., h(ssn) = (ssn) mod 1,000 gives the last three digits of ssn M: size of hash table - choose a prime number, defensively (why?) Prakash 2014 VT CS 5614 10

Division hashing eg., M=2; hash on driver- license number (dln), where last digit is gender (0/1 = M/ F) in an army unit with predominantly male soldiers Thus: avoid cases where M and keys have common divisors - prime M guards against that! Prakash 2014 VT CS 5614 11

Mul7plica7on hashing h(x) = [ frac-onal- part- of ( x * φ ) ] * M φ: golden ra5o ( 0.618... = ( sqrt(5)- 1)/2 ) in general, we need an irra5onal number advantage: M need not be a prime number but φ must be irra5onal Prakash 2014 VT CS 5614 12

Other hashing func7ons quadra5c hashing (bad)... Prakash 2014 VT CS 5614 13

Other hashing func7ons quadra5c hashing (bad)... conclusion: use division hashing Prakash 2014 VT CS 5614 14

Size of hash table eg., 50,000 employees, 10 employee- records / page Q: M=?? pages/buckets/slots Prakash 2014 VT CS 5614 15

Size of hash table eg., 50,000 employees, 10 employees/page Q: M=?? pages/buckets/slots A: u5liza5on ~ 90% and M: prime number Eg., in our case: M= closest prime to 50,000/10 / 0.9 = 5,555 Prakash 2014 VT CS 5614 16

Collision resolu7on Q: what is a collision? A:?? Prakash 2014 VT CS 5614 17

Collision resolu7on #0 page 123; Smith; Main str. #h(123) M Prakash 2014 VT CS 5614 18

Collision resolu7on Q: what is a collision? A:?? Q: why worry about collisions/overflows? (recall that buckets are ~90% full) A: birthday paradox Prakash 2014 VT CS 5614 19

Collision resolu7on open addressing linear probing (ie., put to next slot/bucket) re- hashing separate chaining (ie., put links to overflow pages) Prakash 2014 VT CS 5614 20

Collision resolu7on linear probing: #0 page 123; Smith; Main str. #h(123) M Prakash 2014 VT CS 5614 21

Collision resolu7on re- hashing #0 page h1() 123; Smith; Main str. #h(123) h2() M Prakash 2014 VT CS 5614 22

Collision resolu7on separate chaining 123; Smith; Main str. Prakash 2014 VT CS 5614 23

Design decisions - conclusions func5on: division hashing h(x) = ( a*x+b ) mod M size M: ~90% u5l.; prime number. collision resolu5on: separate chaining easier to implement (dele5ons!); no danger of becoming full Prakash 2014 VT CS 5614 24

Problem with sta7c hashing problem: overflow? problem: underflow? (underu5liza5on) Prakash 2014 VT CS 5614 25

Solu7on: Dynamic/extendible hashing idea: shrink / expand hash table on demand....dynamic hashing Details: how to grow gracefully, on overflow? Many solu5ons - One of them: extendible hashing [Fagin et al] Prakash 2014 VT CS 5614 26

Extendible hashing #0 page 123; Smith; Main str. #h(123) M Prakash 2014 VT CS 5614 27

Extendible hashing solu7on: split the bucket in two #0 page 123; Smith; Main str. #h(123) M Prakash 2014 VT CS 5614 28

Extendible hashing in detail: keep a directory, with ptrs to hash- buckets Q: how to divide contents of bucket in two? A: hash each key into a very long bit string; keep only as many bits as needed Eventually: Prakash 2014 VT CS 5614 29

Extendible hashing directory 00... 01... 10... 11... 0001... 0111... 10101... 10011... 10110... 1101... 101001... Prakash 2014 VT CS 5614 30

Extendible hashing directory 00... 01... 10... 11... 0001... 0111... 10101... 10011... 10110... 1101... 101001... Prakash 2014 VT CS 5614 31

Extendible hashing 00... 01... 10... 11... directory 0001... 0111... 10101... 10011... 10110... 101001... 1101... split on 3- rd bit Prakash 2014 VT CS 5614 32

Extendible hashing 00... 01... 10... 11... directory 0001... 0111... 10011... 1101... 10101... 101001... 10110... new page / bucket Prakash 2014 VT CS 5614 33

Extendible hashing 000... 001... 010... 011... 100... 101... 110... directory (doubled) 0001... 0111... 10011... 1101... 10101... 101001... 10110... new page / bucket 111... Prakash 2014 VT CS 5614 34

Extendible hashing BEFORE AFTER 00... 01... 10... 11... 0001... 0111... 10101... 10011... 10110... 101001... 1101... 0001... 0111... 10011... 1101... 10101... 101001... 10110... 000... 001... 010... 011... 100... 101... 110... 111... Prakash 2014 VT CS 5614 35

Extendible hashing Summary: directory doubles on demand or halves, on shrinking files needs local and global depth Prakash 2014 VT CS 5614 36

Linear hashing - overview Mo5va5on main idea search algo inser5on/split algo dele5on Prakash 2014 VT CS 5614 37

Linear hashing Mo5va5on: ext. hashing needs directory etc etc; which doubles (ouch!) Q: can we do something simpler, with smoother growth? Prakash 2014 VT CS 5614 38

Linear hashing Mo5va5on: ext. hashing needs directory etc etc; which doubles (ouch!) Q: can we do something simpler, with smoother growth? A: split buckets from le] to right, regardless of which one overflowed ( crazy, but it works well!) - Eg.: Prakash 2014 VT CS 5614 39

Linear hashing Ini5ally: h(x) = x mod N (N=4 here) Assume capacity: 3 records / bucket Insert key 17 bucket- id 0 1 2 3 4 8 5 9 13 6 7 11 Prakash 2014 VT CS 5614 40

Linear hashing Ini5ally: h(x) = x mod N (N=4 here) 17 overflow of bucket#1 bucket- id 0 1 2 3 4 8 5 9 13 6 7 11 Prakash 2014 VT CS 5614 41

Linear hashing Ini5ally: h(x) = x mod N (N=4 here) 17 overflow of bucket#1 Split #0, anyway!!! bucket- id 0 1 2 3 4 8 5 9 13 6 7 11 Prakash 2014 VT CS 5614 42

Linear hashing Ini5ally: h(x) = x mod N (N=4 here) 17 Split #0, anyway!!! Q: But, how? bucket- id 0 1 2 3 4 8 5 9 13 6 7 11 Prakash 2014 VT CS 5614 43

Linear hashing A: use two h.f.: h0(x) = x mod N h1(x) = x mod (2*N) 17 bucket- id 0 1 2 3 4 8 5 9 13 6 7 11 Prakash 2014 VT CS 5614 44

Linear hashing - a]er split: A: use two h.f.: h0(x) = x mod N h1(x) = x mod (2*N) bucket- id 0 1 2 3 4 8 5 9 13 6 7 11 4 17 Prakash 2014 VT CS 5614 45

Linear hashing - a]er split: A: use two h.f.: h0(x) = x mod N h1(x) = x mod (2*N) bucket- id 0 1 2 3 4 8 5 9 13 6 7 11 4 17 overflow Prakash 2014 VT CS 5614 46

Linear hashing - a]er split: A: use two h.f.: h0(x) = x mod N h1(x) = x mod (2*N) split ptr bucket- id 0 1 2 3 4 8 5 9 13 6 7 11 4 17 overflow Prakash 2014 VT CS 5614 47

Linear hashing - searching? h0(x) = x mod N the splieed ones) (for the un- split buckets) h1(x) = x mod (2*N) (for split ptr bucket- id 0 1 2 3 4 8 5 9 13 6 7 11 4 17 overflow Prakash 2014 VT CS 5614 48

Linear hashing - searching? Q1: find key 6? Q2: find key 4? Q3: key 8? split ptr bucket- id 0 1 2 3 4 8 5 9 13 6 7 11 4 17 overflow Prakash 2014 VT CS 5614 49

Linear hashing - searching? Algo to find key k : compute b= h0(k); if b<split- ptr, compute b=h1(k) search bucket b Prakash 2014 VT CS 5614 50

Linear hashing - inser7on? Algo: insert key k compute appropriate bucket b if the overflow criterion is true split the bucket of split- ptr split- ptr ++ (*) Prakash 2014 VT CS 5614 51

Linear hashing - inser7on? no5ce: overflow criterion is up to us!! Q: sugges5ons? Prakash 2014 VT CS 5614 52

Linear hashing - inser7on? no5ce: overflow criterion is up to us!! Q: sugges5ons? A1: space u5liza5on >= u- max Prakash 2014 VT CS 5614 53

Linear hashing - inser7on? no5ce: overflow criterion is up to us!! Q: sugges5ons? A1: space u5liza5on > u- max A2: avg length of ovf chains > max- len A3:... Prakash 2014 VT CS 5614 54

Linear hashing - inser7on? Algo: insert key k compute appropriate bucket b if the overflow criterion is true split the bucket of split- ptr split- ptr ++ (*) what if we reach the right edge?? Prakash 2014 VT CS 5614 55

Linear hashing - split now? h0(x) = x mod N the splieed ones) (for the un- split buckets) h1(x) = x mod (2*N) for split ptr 0 1 2 3 4 5 6 Prakash 2014 VT CS 5614 56

Linear hashing - split now? h0(x) = x mod N the splieed ones) (for the un- split buckets) h1(x) = x mod (2*N) (for split ptr 0 1 2 3 4 5 6 7 Prakash 2014 VT CS 5614 57

Linear hashing - split now? h0(x) = x mod N the splieed ones) (for the un- split buckets) h1(x) = x mod (2*N) (for split ptr 0 1 2 3 4 5 6 7 Prakash 2014 VT CS 5614 58

Linear hashing - split now? h0(x) = x mod N the splieed ones) (for the un- split buckets) h1(x) = x mod (2*N) (for split ptr 0 1 2 3 4 5 6 7 Prakash 2014 VT CS 5614 59

Linear hashing - split now? this state is called full expansion split ptr 0 1 2 3 4 5 6 7 Prakash 2014 VT CS 5614 60

Linear hashing - observa7ons In general, at any point of 5me, we have at most two h.f. ac5ve, of the form: h n (x) = x mod (N * 2 n ) h n+1 (x) = x mod (N * 2 n+1 ) (aher a full expansion, we have only one h.f.) Prakash 2014 VT CS 5614 61

Linear hashing - dele7on? reverse of inser5on: Prakash 2014 VT CS 5614 62

Linear hashing - dele7on? reverse of inser5on: if the underflow criterion is met contract! Prakash 2014 VT CS 5614 63

Linear hashing - how to contract? h0(x) = mod N (for the un- split buckets) h1(x) = mod (2*N) (for the splieed ones) split ptr 0 1 2 3 4 5 6 Prakash 2014 VT CS 5614 64

Linear hashing - how to contract? h0(x) = mod N (for the un- split buckets) h1(x) = mod (2*N) (for the splieed ones) split ptr 0 1 2 3 4 5 Prakash 2014 VT CS 5614 65

Hashing - pros? Prakash 2014 VT CS 5614 66

Hashing - pros? Speed, on exact match queries on the average Prakash 2014 VT CS 5614 67

B(+)- trees - pros? Prakash 2014 VT CS 5614 68

B(+)- trees - pros? Speed on search: exact match queries, worst case range queries nearest- neighbor queries Speed on inser5on + dele5on smooth growing and shrinking (no re- org) Prakash 2014 VT CS 5614 69

Conclusions B- trees and variants: in all DBMSs hash indices: in some (but hashing in useful for joins ) Prakash 2014 VT CS 5614 70

SORTING Prakash 2014 VT CS 5614 71

Why Sort? Prakash 2014 VT CS 5614 72

Why Sort? select... order by e.g., find students in increasing gpa order bulk loading B+ tree index. duplicate eliminalon (select distinct) select... group by Sort- merge join algorithm involves sor5ng. Prakash 2014 VT CS 5614 73

Outline two- way merge sort external merge sort fine- tunings B+ trees for sor5ng Prakash 2014 VT CS 5614 74

2- Way Sort: Requires 3 Buffers Pass 0: Read a page, sort it, write it. only one buffer page is used Pass 1, 2, 3,, etc.: requires 3 buffer pages merge pairs of runs into runs twice as long three buffer pages used. INPUT 1 INPUT 2 OUTPUT Disk Main memory buffers Disk Prakash 2014 VT CS 5614 75

Two- Way External Merge Sort Each pass we read + write each page in file. 3,4 6,2 9,4 8,7 5,6 3,1 2 3,4 2,6 4,9 7,8 5,6 1,3 2 2,3 4,6 2,3 4,4 6,7 8,9 4,7 8,9 1,3 5,6 2 1,2 3,5 6 Input file PASS 0 1-page runs PASS 1 2-page runs PASS 2 4-page runs PASS 3 Prakash 2014 VT CS 5614 9 76 1,2 2,3 3,4 4,5 6,6 7,8 8-page runs

Two- Way External Merge Sort Each pass we read + write each page in file. 3,4 6,2 9,4 8,7 5,6 3,1 2 3,4 2,6 4,9 7,8 5,6 1,3 2 2,3 4,6 2,3 4,4 6,7 8,9 4,7 8,9 1,3 5,6 2 1,2 3,5 6 Input file PASS 0 1-page runs PASS 1 2-page runs PASS 2 4-page runs PASS 3 Prakash 2014 VT CS 5614 9 77 1,2 2,3 3,4 4,5 6,6 7,8 8-page runs

Two- Way External Merge Sort Each pass we read + write each page in file. 3,4 6,2 9,4 8,7 5,6 3,1 2 3,4 2,6 4,9 7,8 5,6 1,3 2 2,3 4,6 2,3 4,4 6,7 8,9 4,7 8,9 1,3 5,6 2 1,2 3,5 6 Input file PASS 0 1-page runs PASS 1 2-page runs PASS 2 4-page runs PASS 3 Prakash 2014 VT CS 5614 9 78 1,2 2,3 3,4 4,5 6,6 7,8 8-page runs

Two- Way External Merge Sort Each pass we read + write each page in file. 3,4 6,2 9,4 8,7 5,6 3,1 2 3,4 2,6 4,9 7,8 5,6 1,3 2 2,3 4,6 2,3 4,4 6,7 8,9 4,7 8,9 1,3 5,6 2 1,2 3,5 6 Input file PASS 0 1-page runs PASS 1 2-page runs PASS 2 4-page runs PASS 3 Prakash 2014 VT CS 5614 9 79 1,2 2,3 3,4 4,5 6,6 7,8 8-page runs

Two- Way External Merge Sort Each pass we read + write each page in file. N pages in the file => = log + 2 N 1 So total cost is: ( log N + ) 2 N 1 2 Idea: Divide and conquer: sort subfiles and merge 3,4 6,2 9,4 8,7 5,6 3,1 2 3,4 2,6 4,9 7,8 5,6 1,3 2 2,3 4,6 Input file Prakash 2014 VT CS 5614 9 80 2,3 4,4 6,7 8,9 4,7 8,9 1,2 2,3 3,4 4,5 6,6 7,8 1,3 5,6 2 1,2 3,5 6 PASS 0 1-page runs PASS 1 2-page runs PASS 2 4-page runs PASS 3 8-page runs

External merge sort B > 3 buffers Q1: how to sort? Q2: cost? Prakash 2014 VT CS 5614 81

General External Merge Sort B>3 buffer pages. How to sort a file with N pages?......... Disk B Main memory buffers Disk Prakash 2014 VT CS 5614 82

General External Merge Sort Pass 0: use B buffer pages. Produce N / B sorted runs of B pages each. Pass 1, 2,, etc.: merge B- 1 runs. INPUT 1... INPUT 2... OUTPUT... Disk INPUT B-1 B Main memory buffers Disk Prakash 2014 VT CS 5614 83

Sor7ng create sorted runs of size B (how many?) merge them (how?) B...... Prakash 2014 VT CS 5614 84

Sor7ng create sorted runs of size B merge first B- 1 runs into a sorted run of (B- 1) *B,... B........ Prakash 2014 VT CS 5614 85

Sor7ng How many steps we need to do? i, where B*(B- 1)^i > N How many reads/writes per step? N+N B........ Prakash 2014 VT CS 5614 86

Cost of External Merge Sort Number of passes: Cost = 2N * (# of passes) 1 + 1 log B N / B Prakash 2014 VT CS 5614 87

Cost of External Merge Sort E.g., with 5 buffer pages, to sort 108 page file: Pass 0: 108 / 5 = 22 sorted runs of 5 pages each (last run is only 3 pages) Pass 1: 22 / 4 = 6 sorted runs of 20 pages each (last run is only 8 pages) Pass 2: 2 sorted runs, 80 pages and 28 pages Pass 3: Sorted file of 108 pages Formula check: log 4 22 = 3 + 1 à 4 passes Prakash 2014 VT CS 5614 88

Number of Passes of External Sort ( I/O cost is 2N 5mes number of passes) N B=3 B=5 B=9 B=17 B=129 B=257 100 7 4 3 2 1 1 1,000 10 5 4 3 2 2 10,000 13 7 5 4 2 2 100,000 17 9 6 5 3 3 1,000,000 20 10 7 5 3 3 10,000,000 23 12 8 6 4 3 100,000,000 26 14 9 7 4 4 1,000,000,000 30 15 10 8 5 4 Prakash 2014 VT CS 5614 89

Internal Sort Algorithm Quicksort is a fast way to sort in memory. Prakash 2014 VT CS 5614 90

Blocked I/O & double- buffering So far, we assumed random disk access Cost changes, if we consider that runs are wriyen (and read) sequen5ally What could we do to exploit it? Prakash 2014 VT CS 5614 91

Blocked I/O & double- buffering So far, we assumed random disk access Cost changes, if we consider that runs are wriyen (and read) sequen5ally What could we do to exploit it? A1: Blocked I/O (exchange a few r.d.a for several sequen5al ones) A2: double- buffering Prakash 2014 VT CS 5614 92

Double Buffering To reduce wait 5me for I/O request to complete, can prefetch into `shadow block. Poten5ally, more passes; in prac5ce, most files s5ll sorted in 2-3 passes. INPUT 1 INPUT 1' INPUT 2 INPUT 2' OUTPUT OUTPUT' Disk INPUT k b block size Disk INPUT k' Prakash 2014 VT CS 5614 93

Using B+ Trees for Sor7ng Scenario: Table to be sorted has B+ tree index on sor5ng column(s). Idea: Can retrieve records in order by traversing leaf pages. Is this a good idea? Cases to consider: B+ tree is clustered B+ tree is not clustered Prakash 2014 VT CS 5614 94

Using B+ Trees for Sor7ng Scenario: Table to be sorted has B+ tree index on sor5ng column(s). Idea: Can retrieve records in order by traversing leaf pages. Is this a good idea? Cases to consider: B+ tree is clustered Good idea! B+ tree is not clustered Could be a very bad idea! Prakash 2014 VT CS 5614 95

Clustered B+ Tree Used for Sor7ng Cost: root to the le]- most leaf, then retrieve all leaf pages (Alterna5ve 1) Index (Directs search) Data Entries ("Sequence set") Data Records Always beyer than external sor5ng! Prakash 2014 VT CS 5614 96

Unclustered B+ Tree Used for Sor7ng Alterna5ve (2) for data entries; each data entry contains rid of a data record. In general, one I/O per data record! Index (Directs search) Data Entries ("Sequence set") Data Records Prakash 2014 VT CS 5614 97

External Sor7ng vs. Unclustered Index N Sorting p=1 p=10 p=100 100 200 100 1,000 10,000 1,000 2,000 1,000 10,000 100,000 10,000 40,000 10,000 100,000 1,000,000 100,000 600,000 100,000 1,000,000 10,000,000 1,000,000 8,000,000 1,000,000 10,000,000 100,000,000 10,000,000 80,000,000 10,000,000 100,000,000 1,000,000,000 p: # of records per page B=1,000 and block size=32 for sor-ng p=100 is the more realis-c value. Prakash 2014 VT CS 5614 98

Summary External sor5ng is important External merge sort minimizes disk I/O cost: Pass 0: Produces sorted runs of size B (# buffer pages). Later passes: merge runs. Clustered B+ tree is good for sor5ng; unclustered tree is usually very bad. Prakash 2014 VT CS 5614 99