Database Applications (15-415)

Similar documents
Database Applications (15-415)

Database Applications (15-415)

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

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

Database Applications (15-415)

Hash-Based Indexing 1

Hashed-Based Indexing

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

Database Applications (15-415)

Database Applications (15-415)

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

Database Applications (15-415)

Hash-Based Indexes. Chapter 11

CSIT5300: Advanced Database Systems

EXTERNAL SORTING. Sorting

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

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

Database Applications (15-415)

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

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

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

Lecture 8 Index (B+-Tree and Hash)

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

Database Applications (15-415)

Database Applications (15-415)

External Sorting Implementing Relational Operators

Lecture 11. Lecture 11: External Sorting

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

Kathleen Durant PhD Northeastern University CS Indexes

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

Dtb Database Systems. Announcement

Advanced Database Systems

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

Final Exam Review. Kathleen Durant CS 3200 Northeastern University Lecture 22

Module 5: Hash-Based Indexing

4 Hash-Based Indexing

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

External Sorting Sorting Tables Larger Than Main Memory

Evaluation of Relational Operations

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

Principles of Data Management. Lecture #5 (Tree-Based Index Structures)

Introduction to Indexing 2. Acknowledgements: Eamonn Keogh and Chotirat Ann Ratanamahatana

Chapter 12: Query Processing. Chapter 12: Query Processing

Query Processing. Debapriyo Majumdar Indian Sta4s4cal Ins4tute Kolkata DBMS PGDBA 2016

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

Query Processing & Optimization

TUTORIAL ON INDEXING PART 2: HASH-BASED INDEXING

CompSci 516: Database Systems

Why Is This Important? Overview of Storage and Indexing. Components of a Disk. Data on External Storage. Accessing a Disk Page. Records on a Disk Page

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

Database Applications (15-415)

Spring 2013 CS 122C & CS 222 Midterm Exam (and Comprehensive Exam, Part I) (Max. Points: 100)

Query Processing. Introduction to Databases CompSci 316 Fall 2017

Chapter 12: Indexing and Hashing. Basic Concepts

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.

Database Applications (15-415)

Chapter 12: Indexing and Hashing

Tree-Structured Indexes. Chapter 10

Database Applications (15-415)

Intro to DB CHAPTER 12 INDEXING & HASHING

Midterm Review CS634. Slides based on Database Management Systems 3 rd ed, Ramakrishnan and Gehrke

Question 1. Part (a) [2 marks] what are different types of data independence? explain each in one line. Part (b) CSC 443 Term Test Solutions Fall 2017

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

Today's Class. Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Example Database. Query Plan Example

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

Physical Disk Structure. Physical Data Organization and Indexing. Pages and Blocks. Access Path. I/O Time to Access a Page. Disks.

Chapter 12: Query Processing

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

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

Hash-Based Indexes. Chapter 11. Comp 521 Files and Databases Spring

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

Tree-Structured Indexes

CSC 261/461 Database Systems Lecture 19

Hash-Based Indexes. Chapter 11. Comp 521 Files and Databases Fall

Data Management for Data Science

Chapter 11: Indexing and Hashing

RAID in Practice, Overview of Indexing

CS 350 Algorithms and Complexity

CSC 261/461 Database Systems Lecture 17. Fall 2017

Chapter 13: Query Processing

Tree-Structured Indexes

Announcements (March 1) Query Processing: A Systems View. Physical (execution) plan. Announcements (March 3) Physical plan execution

6.830 Lecture 8 10/2/2017. Lab 2 -- Due Weds. Project meeting sign ups. Recap column stores & paper. Join Processing:

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

Chapter 12: Indexing and Hashing (Cnt(

Tree-Structured Indexes

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

C has been and will always remain on top for performancecritical

Database System Concepts, 6 th Ed. Silberschatz, Korth and Sudarshan See for conditions on re-use

! A relational algebra expression may have many equivalent. ! Cost is generally measured as total elapsed time for

Chapter 13: Query Processing Basic Steps in Query Processing

Introduction to Data Management. Lecture #13 (Indexing)

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

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

Storage and Indexing

Tree-Structured Indexes

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

Query Processing: A Systems View. Announcements (March 1) Physical (execution) plan. CPS 216 Advanced Database Systems

COSC-4411(M) Midterm #1

Transcription:

Database Applications (15-415) DBMS Internals- Part V Lecture 15, March 15, 2015 Mohammad Hammoud

Today Last Session: DBMS Internals- Part IV Tree-based (i.e., B+ Tree) and Hash-based (i.e., Extendible Hashing) indexes Today s Session: DBMS Internals- Part V Hash-based indexes (Cont d) and External Sorting Announcements: Project 2 is due today by midnight. Student demos will be conducted on Tuesday/Thursday PS3 is now posted and it is due on March 26 by midnight Project 3 will be posted by Thursday

DBMS Layers Queries Query Optimization and Execution Continue Transaction Manager Lock Manager Relational Operators Files and Access Methods Buffer Management Disk Space Management Recovery Manager DB

Outline Linear Hashing Why Sorting? In-Memory vs. External Sorting A Simple 2-Way External Merge Sorting General External Merge Sorting Optimizations: Replacement Sorting, Blocked I/O and Double Buffering

Linear Hashing Another way of adapting gracefully to insertions and deletions (i.e., pursuing dynamic hashing) is to use Linear Hashing (LH) In contrast to Extendible Hashing, LH Does not require a directory Deals naturally with collisions Offers a lot of flexibility w.r.t the timing of bucket split (allowing trading off greater overflow chains for higher average space utilization)

How Linear Hashing Works? LH uses a family of hash functions h 0, h 1, h 2,... h i (key) = h(key) mod(2 i N); N = initial # buckets h is some hash function (range is not 0 to N-1) If N = 2 d0, for some d0, h i consists of applying h and looking at the last di bits, where di = d0 + i h i+1 doubles the range of h i (similar to directory doubling)

How Linear Hashing Works? (Cont d) LH uses overflow pages, and chooses buckets to split in a round-robin fashion Splitting proceeds in rounds Next Buckets split in this round A round ends when all N R (for round R) initial buckets are split Buckets 0 to Next-1 have been split; Next to N R yet to be split Current round number is referred to as Level Buckets that existed at the beginning of this round: this is the range of h Level split image buckets created in this round

Linear Hashing: Searching For Entries To find a bucket for data entry r, find h Level (r): If h Level (r) in range `Next to N R, r belongs there Else, r could belong to bucket h Level (r) or bucket h Level (r) + N R ; must apply h Level+1 (r) to find out h h Example: search for 5* 0 1 Level=0, N=4 PRIMARY Next=0 PAGES Level = 0 h0 5* = 101 01 000 001 00 01 32*44* 36* 9* 25* 5* Data entry r with h(r)=5 010 10 14* 18*10*30* Primary bucket page 011 11 31*35* 7* 11*

Linear Hashing: Inserting Entries Find bucket as in search If the bucket to insert the data entry into is full: Add an overflow page and insert data entry (Maybe) Split Next bucket and increment Next Some points to Keep in mind: Unlike Extendible Hashing, when an insert triggers a split, the bucket into which the data entry is inserted is not necessarily the bucket that is split As in Static Hashing, an overflow page is added to store the newly inserted data entry However, since the bucket to split is chosen in a round-robin fashion, eventually all buckets will be split

Linear Hashing: Inserting Entries Example: insert 43* Level = 0 h0 43* = 101011 11 h 1 h 0 Level=0, N=4 PRIMARY Next=0 PAGES 000 00 32*44* 36* 001 01 9* 25* 5* 010 10 14* 18*10*30* Add an overflow page and insert data entry 011 11 31*35* 7* 11*

Linear Hashing: Inserting Entries Example: insert 43* Level = 0 h0 43* = 101011 11 h 1 h 0 Level=0, N=4 PRIMARY Next=0 PAGES OVERFLOW PAGES 000 00 32*44* 36* 001 01 9* 25* 5* 010 10 14* 18*10*30* Split Next bucket and increment Next 011 11 31*35* 7* 11* 43*

Linear Hashing: Inserting Entries Example: insert 43* Level = 0 h0 43* = 101011 11 h 1 h 0 Level=0, N=4 PRIMARY Next=0 PAGES OVERFLOW PAGES 000 00 32* 001 01 9* 25* 5* 010 10 14* 18*10*30* Almost there 011 11 31*35* 7* 11* 43* 100 00 44* 36*

Linear Hashing: Inserting Entries Example: insert 43* Level = 0 h0 43* = 101011 11 h 1 h 0 Level=0, N=4 PRIMARY PAGES OVERFLOW PAGES 000 00 32* Next=1 001 01 9* 25* 5* 010 10 14* 18*10*30* FINAL STATE! 011 11 31*35* 7* 11* 43* 100 00 44* 36*

Linear Hashing: Inserting Entries Another Example: insert 50* Level = 0 h0 50* = 110010 10 h 1 000 h0 00 Level=0, N= 4 PRIMARY PAGES 32* OVERFLOW PAGES 001 01 9* 25* 010 011 10 11 66* 18* 10* 34* Next=3 31* 35* 7* 11* 43* 100 00 44*36* Add an overflow page and insert data entry 101 110 01 10 5* 37*29* 14* 30* 22*

Linear Hashing: Inserting Entries Another Example: insert 50* Level = 0 h0 50* = 110010 10 h 1 000 h0 00 Level=0, N= 4 PRIMARY PAGES 32* OVERFLOW PAGES 001 01 9* 25* 010 011 10 11 66* 18* 10* 34* 50* Next=3 31* 35* 7* 11* 43* 100 00 44*36* Split Next bucket and increment Next 101 110 01 10 5* 37*29* 14* 30* 22*

Linear Hashing: Inserting Entries Another Example: insert 50* Level = 0 h0 50* = 110010 10 h 1 000 001 Level=0 h0 00 01 PRIMARY PAGES 32* 9* 25* OVERFLOW PAGES 010 10 66* 18* 10* 34* 50* 011 11 Next=3 43* 35* 11* 100 00 44* 36* Almost there 101 11 5* 37* 29* 110 10 14* 30* 22* 111 11 31*7*

Linear Hashing: Inserting Entries Another Example: insert 50* Level = 0 h0 50* = 110010 10 h 1 000 001 Level=0 h0 00 01 Next=0 PRIMARY PAGES 32* 9* 25* OVERFLOW PAGES 010 10 66* 18* 10* 34* 50* 011 11 43* 35* 11* 100 00 44* 36* Almost there 101 11 5* 37* 29* 110 10 14* 30* 22* 111 11 31*7*

Linear Hashing: Inserting Entries Another Example: insert 50* Level = 0 h0 50* = 110010 10 h 1 000 001 Level=1 h0 00 01 Next=0 PRIMARY PAGES 32* 9* 25* OVERFLOW PAGES 010 10 66* 18* 10* 34* 50* 011 11 43* 35* 11* 100 00 44* 36* FINAL STATE! 101 11 5* 37* 29* 110 10 14* 30* 22* 111 11 31*7*

Linear Hashing: Deleting Entries Deletion is essentially the inverse of insertion If the last bucket in the file is empty, it can be removed and Next can be decremented If Next is zero and the last bucket becomes empty Next is made to point to bucket M/2-1 (where M is the current number of buckets) Level is decremented The empty bucket is removed The insertion examples can be worked out backwards as examples of deletions!

DBMS Layers Queries Query Optimization and Execution But, before we will discuss Sorting Transaction Manager Lock Manager Relational Operators Files and Access Methods Buffer Management Disk Space Management Recovery Manager DB

Outline Linear Hashing Why Sorting? In-Memory vs. External Sorting A Simple 2-Way External Merge Sorting General External Merge Sorting Optimizations: Replacement Sorting, Blocked I/O and Double Buffering

When Does A DBMS Sort Data? Users may want answers in some order SELECT FROM student ORDER BY name SELECT S.rating, MIN (S.age) FROM Sailors S GROUP BY S.rating Bulk loading a B+ tree index involves sorting Sorting is useful in eliminating duplicates records The Sort-Merge Join algorithm involves sorting (next session!)

Outline Linear Hashing Why Sorting? In-Memory vs. External Sorting A Simple 2-Way External Merge Sorting General External Merge Sorting Optimizations: Replacement Sorting, Blocked I/O and Double Buffering

In-Memory vs. External Sorting Assume we want to sort 60GB of data on a machine with only 8GB of RAM In-Memory Sort (e.g., Quicksort)? Yes, but data do not fit in memory What about relying on virtual memory? In this case, external sorting is needed In-memory sorting is orthogonal to external sorting!

Outline Linear Hashing Why Sorting? In-Memory vs. External Sorting A Simple 2-Way External Merge Sorting General External Merge Sorting Optimizations: Replacement Sorting, Blocked I/O and Double Buffering

A Simple Two-Way Merge Sort IDEA: Sort sub-files that can fit in memory and merge Let us refer to each sorted sub-file as a run Algorithm: Pass 1: Read a page into memory, sort it, write it 1-page runs are produced Passes 2, 3, etc.,: Merge pairs (hence, 2-way) of runs to produce longer runs until only one run is left

A Simple Two-Way Merge Sort Algorithm: Pass 1: Read a page into memory, sort it, write it How many buffer pages are needed? Passes 2, 3, etc.,: Merge pairs (hence, 2-way) of runs to produce longer runs until only one run is left How many buffer pages are needed? ONE THREE INPUT 1 INPUT 2 OUTPUT Disk Main memory buffers Disk

2-Way Merge Sort: An Example PASS 0 PASS 1 PASS 2 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 4,7 8,9 1,3 5,6 2 Input File 1-Page Runs 2-Page Runs 2,3 PASS 3 4,4 6,7 8,9 1,2 3,5 6 4-Page Runs 1,2 2,3 3,4 4,5 6,6 7,8 9 8-Page Runs

2-Way Merge Sort: I/O Cost Analysis If the number of pages in the input file is 2 k How many runs are produced in pass 0 and of what size? 2 k 1-page runs How many runs are produced in pass 1 and of what size? 2 k-1 2-page runs How many runs are produced in pass 2 and of what size? 2 k-2 4-page runs How many runs are produced in pass k and of what size? 2 k-k 2 k -page runs (or 1 run of size 2 k ) For N number of pages, how many passes are incurred? log 2 N 1 How many pages do we read and write in each pass? 2N What is the overall cost? 1) 2N ( log2 N

2-Way Merge Sort: An Example PASS 0 PASS 1 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 Input File 1-Page Runs log 2 8 1 PASS 2 2,3 4,6 4,7 8,9 1,3 5,6 2 2-Page Runs = 4 passes 2,3 PASS 3 4,4 6,7 8,9 1,2 3,5 6 4-Page Runs Formula Check: log 1) 2N ( 2 N = (2 8) (3 + 1) = 64 I/Os Correct! 1,2 2,3 3,4 4,5 6,6 7,8 9 8-Page Runs

Outline Linear Hashing Why Sorting? In-Memory vs. External Sorting A Simple 2-Way External Merge Sorting General External Merge Sorting Optimizations: Replacement Sorting, Blocked I/O and Double Buffering

B-Way Merge Sort How can we sort a file with N pages using B buffer pages? Pass 0: use B buffer pages This will produce N B sorted B-page runs Pass 2,, etc.: merge B-1 runs / INPUT 1... INPUT 2...... OUTPUT Disk INPUT B-1 B Main memory buffers Disk

B-Way Merge Sort: I/O Cost Analysis I/O cost = 2N Number of passes Number of passes = 1 1 log B N / B Assume the previous example (i.e., 8 pages), but using 5 buffer pages (instead of 2) I/O cost = 32 (as opposed to 64) Therefore, increasing the number of buffer pages minimizes the number of passes and accordingly the I/O cost!

Number of Passes of B-Way Sort 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 High Fan-in during merging is crucial! How else can we minimize I/O cost?

Outline Linear Hashing Why Sorting? In-Memory vs. External Sorting A Simple 2-Way External Merge Sorting General External Merge Sorting Optimizations: Replacement Sorting, Blocked I/O and Double Buffering

Replacement Sort With a more aggressive implementation of B-way sort, we can write out runs of ~2 B internally sorted pages This is referred to as replacement sort 2 12 4 8 10 3 5 INPUT CURRENT SET OUTPUT IDEA: Pick the tuple in the current set with the smallest value that is greater than the largest value in the output buffer and append it to the output buffer

Replacement Sort With a more aggressive implementation of B-way sort, we can write out runs of ~2 B internally sorted pages This is referred to as replacement sort 2 12 4 8 10 3 5 INPUT CURRENT SET OUTPUT When do we terminate the current run and start a new one?

Blocked I/O and Double Buffering So far, we assumed random disk accesses Would cost change if we assume that reads and writes are done sequentially? Yes How can we incorporate this fact into our cost model? Use bigger units (this is referred to as Blocked I/O) Mask I/O delays through pre-fetching (this is referred to as double buffering)

Blocked I/O Normally, we go with B buffers of size (say) 1 page INPUT 1... INPUT 2...... OUTPUT Disk INPUT 5 Disk

Blocked I/O Normally, we go with B buffers of size (say) 1 page INSTEAD: let us go with B/b buffers, of size b pages INPUT 1 OUTPUT... INPUT 2... Disk 3 Main memory buffers Disk

Blocked I/O Normally, we go with B buffers of size (say) 1 page INSTEAD: let us go with B/b buffers, of size b pages What is the main advantage? Fewer random accesses (as some of the page will be arranged sequentially!) What is the main disadvantage? Smaller fan-in and accordingly larger number of passes!

Double Buffering Normally, when, say INPUT1 is exhausted We issue a read request and We wait INPUT 1... INPUT 2...... OUTPUT Disk INPUT B-1 B Main memory buffers Disk

Double Buffering INSTEAD: pre-fetch INPUT1 into a `shadow block When INPUT1 is exhausted, issue a read BUT, also proceed with INPUT1 Thus, the CPU can never go idle! INPUT 1 INPUT 1' INPUT 2 OUTPUT INPUT 2' OUTPUT' Disk INPUT k INPUT k' b block size Disk B main memory buffers, k-way merge

Next Class Queries Transaction Manager Lock Manager Query Optimization and Execution Relational Operators Files and Access Methods Buffer Management Disk Space Management Continue Recovery Manager DB