Database Management Systems (CS 564)

Slides:



Advertisements
Similar presentations
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Tree-Structured Indexes Chapter 9.
Advertisements

ICS 421 Spring 2010 Indexing (1) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 02/18/20101Lipyeow Lim.
CS4432: Database Systems II
B+-tree and Hashing.
Tree-Structured Indexes. Introduction v As for any index, 3 alternatives for data entries k* : À Data record with key value k Á Â v Choice is orthogonal.
1 Tree-Structured Indexes Yanlei Diao UMass Amherst Feb 20, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
1 Tree-Structured Indexes Chapter Introduction  As for any index, 3 alternatives for data entries k* :  Data record with key value k   Choice.
CS4432: Database Systems II
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Tree-Structured Indexes Chapter 9.
Tree-Structured Indexes. Range Searches ``Find all students with gpa > 3.0’’ –If data is in sorted file, do binary search to find first such student,
Storage and Indexing February 26 th, 2003 Lecture 19.
Introduction to Database Systems1 B+-Trees Storage Technology: Topic 5.
 B+ Tree Definition  B+ Tree Properties  B+ Tree Searching  B+ Tree Insertion  B+ Tree Deletion.
Database Management 8. course. Query types Equality query – Each field has to be equal to a constant Range query – Not all the fields have to be equal.
Tree-Structured Indexes Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein for some slides.
1 Indexing. 2 Motivation Sells(bar,beer,price )Bars(bar,addr ) Joe’sBud2.50Joe’sMaple St. Joe’sMiller2.75Sue’sRiver Rd. Sue’sBud2.50 Sue’sCoors3.00 Query:
B+ Tree Index tuning--. overview B + -Tree Scalability Typical order: 100. Typical fill-factor: 67%. –average fanout = 133 Typical capacities (root at.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 B+-Tree Index Chapter 10 Modified by Donghui Zhang Nov 9, 2005.
Storage and Indexing. How do we store efficiently large amounts of data? The appropriate storage depends on what kind of accesses we expect to have to.
Indexing. 421: Database Systems - Index Structures 2 Cost Model for Data Access q Data should be stored such that it can be accessed fast q Evaluation.
Data on External Storage – File Organization and Indexing – Cluster Indexes - Primary and Secondary Indexes – Index data Structures – Hash Based Indexing.
1 Tree-Structured Indexes Chapter Introduction  As for any index, 3 alternatives for data entries k* :  Data record with key value k   Choice.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Tree-Structured Indexes Content based on Chapter 10 Database Management Systems, (3 rd.
Tree-Structured Indexes Chapter 10
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Tree-Structured Indexes Chapter 10.
Database Applications (15-415) DBMS Internals- Part III Lecture 13, March 06, 2016 Mohammad Hammoud.
Tree-Structured Indexes. Introduction As for any index, 3 alternatives for data entries k*: – Data record with key value k –  Choice is orthogonal to.
Multilevel Indexing and B+ Trees
Multilevel Indexing and B+ Trees
CS522 Advanced database Systems
CS 540 Database Management Systems
CS522 Advanced database Systems
Tree-Structured Indexes
Tree-Structured Indexes
COP Introduction to Database Structures
Indexing ? Why ? Need to locate the actual records on disk without having to read the entire table into memory.
Database Management Systems (CS 564)
CS522 Advanced database Systems
Extra: B+ Trees CS1: Java Programming Colorado State University
Database Management Systems (CS 564)
Database Management Systems (CS 564)
Database Management Systems (CS 564)
Database Applications (15-415) DBMS Internals- Part III Lecture 15, March 11, 2018 Mohammad Hammoud.
Lecture 12 Lecture 12: Indexing.
B+-Trees and Static Hashing
Tree-Structured Indexes
CS222/CS122C: Principles of Data Management Notes #07 B+ Trees
Introduction to Database Systems Tree Based Indexing: B+-tree
Tree-Structured Indexes
Tree-Structured Indexes
Lecture 19: Data Storage and Indexes
B+Trees The slides for this text are organized into chapters. This lecture covers Chapter 9. Chapter 1: Introduction to Database Systems Chapter 2: The.
Tree-Structured Indexes
Database Management Systems (CS 564)
Tree-Structured Indexes
Indexing 1.
Database Systems (資料庫系統)
Storage and Indexing.
Database Systems (資料庫系統)
General External Merge Sort
Introduction to Database Systems CSE 444 Lectures 19: Data Storage and Indexes May 16, 2008.
Tree-Structured Indexes
Tree-Structured Indexes
Indexing February 28th, 2003 Lecture 20.
Tree-Structured Indexes
Tree-Structured Indexes
Lecture 20: Indexes Monday, February 27, 2006.
Tree-Structured Indexes
CS222/CS122C: Principles of Data Management UCI, Fall 2018 Notes #06 B+ trees Instructor: Chen Li.
CS222P: Principles of Data Management UCI, Fall Notes #06 B+ trees
Presentation transcript:

Database Management Systems (CS 564) Fall 2017 Lecture 16

Indexing: Faster Access to Data for a Price Use responsibly! CS 564 (Fall'17)

Recap Architecture of a typical DBMS Memory hierarchy Disk SSD CPU cache, main memory, SSD, disk, tape Disk Anatomy, accessing the disk (seek time, rotational delay, data transfer time) SSD Buffer management Buffer replacement policies, sequential flooding File organization (a.k.a. files of (pages of) records) Unordered (heap) files– using linked lists or page directories Page organization (fixed-length vs variable-length records) Column stores CS 564 (Fall'17)

Detailed DBMS Architecture Web Forms Application Front Ends SQL Interface SQL Commands Query Evaluation Engine Plan Executor Parser Operator Evaluator Optimizer Transaction Manager Recovery Manager File and Access Methods DBMS Concurrency Control Buffer Manager Storage Manager Lock Manager Disk Space Manager Index Files Database System Catalog Data Files CS 564 (Fall'17)

Magnetic Hard Disk Drive (HDD) Memory Hierarchy Volatile Persistent 1-10 CPU Cache Main Memory Price 102-103 Access Speed Access Cycles Capacity Flash Storage 105-106 Magnetic Hard Disk Drive (HDD) 107-108 Tape CS 564 (Fall'17)

Revisiting File Organization So far, we’ve seen heap files Unsorted Fast to scan all the records in a file Fast to access a record with its ID (rid) However, answering many queries using heap files only would be very slow CS 564 (Fall'17)

Revisiting File Organization (Cont.) Example Using a heap file, on average half of the pages in the file need to be checked to similar queries We can do better! SELECT Name FROM Student WHERE SID=‘23564’; CS 564 (Fall'17)

Alternative File/Data Organization Can speed up answering various queries by better organizing data in files Alternatives Sorted files Using indexes B+tree Hash index CS 564 (Fall'17)

Revisiting File Organization (Cont.) Another example Way to efficiently evaluate these queries Store the table as a heap file, scan the file Store the table as a sorted file, binary search the file Store the table as a heap file, build an index, and search using the index Store the table in an index file. The entire tuple is stored in the index! CREATE TABLE Tweet ( uniqueMsgID INTEGER, -- unique message id tstamp TIMESTAMP, -- when was the tweet posted uid INTEGER, -- unique id of the user msg VARCHAR (140), -- the actual message zip INTEGER -- zipcode when posted ); Q1: SELECT * FROM Tweet WHERE zip BETWEEN 53000 AND 54999 Q2: SELECT * FROM Tweet WHERE uid = 145; CS 564 (Fall'17)

Sorted Files Suppose the records of Tweet are stored in a sorted file, they are sorted by the value of their zip attribute, and we can access the ith page of the file in one disk I/O Let B: number of data pages R: number of records per page D: average time to read or write a disk page C: average time to process a record (e.g. evaluate zip BETWEEN 53000 AND 54999) Q1: SELECT * FROM Tweet WHERE zip BETWEEN 53000 AND 54999 How long would it take (on average) to read in the first tuple in the result set of Q1? D log2B + C log2R (Why?) CS 564 (Fall'17)

Basics of Indexes Data structure that organizes records on secondary storage to optimize certain kinds of retrieval operations Speed up retrieving all records that satisfy search conditions on the search key fields Any subset of the fields of a relation can be the search key Can have multiple indexes with different search key fields A search key is not the same as the primary key An index contains a collection of data entries (each entry with enough info to locate the data records) CS 564 (Fall'17)

Data Entry A data entry k* with search key value k can look like one of the following An actual data record ⟨k, rid⟩ ⟨k, rid-list⟩ Data file and index file can be the same; i.e. the whole table can be organized as an index Data file and index file are different; i.e. data entries are stored in a separate file (of its own structure) and are used to access data records in the data file CS 564 (Fall'17)

Index Types Two main types of indexes Hash indexes: good for equality search (e.g. Q2) Tree-based indexes: good for both range search (e.g. Q1) and equality search (e.g. Q2) Generally, a hash index is faster than a tree-based index for equality search (why?) Refresher: a hash function H projects a value from a set with many (or even an infinite number of) members to a value from a set with a fixed number of (fewer) members Hash functions are not reversible Example: 𝐻 𝑥 = 10𝑥 𝑚𝑜𝑑 1 CS 564 (Fall'17)

Example Rating(User, Movie, Stars) Hash index with 𝑘=𝐻(𝑡)=𝑡.Stars Simulate answering: SELECT * FROM Rating WHERE Stars = 3.0; Rating(User, Movie, Stars) Index (logical view) Data file <3.0, [1600, 1900, 2400]> <3.5, [1728]> <4.0, [2028, 2528]> 1600 Joe Blade Runner 3.0 1728 Joe Pulp Fiction 3.5 1900 Venkat 12 Angry Men 3.0 2028 Venkat Pulp Fiction 4.0 2400 Xin Blade Runner 3.0 2528 Xin Se7en 4.0 CS 564 (Fall'17)

Example (Cont.) Rating(User, Movie, Stars) Hash index with 𝑘=𝐻(𝑡)= 𝑡.Stars Simulate answering: SELECT * FROM Rating WHERE Stars = 3.0; Rating(User, Movie, Stars) Index file Data file 1 2 3 4 5 1600 Joe Blade Runner 3.0 1728 Joe Pulp Fiction 3.5 3.0 1600 3.0 1900 3.0 2400 3.5 1728 1900 Venkat 12 Angry Men 3.0 2028 Venkat Pulp Fiction 4.0 A bucket 4.0 2028 4.0 2528 2400 Xin Blade Runner 3.0 2528 Xin Se7en 4.0 CS 564 (Fall'17)

Example (Cont.) Rating(User, Movie, Stars) Data/index file Hash index with 𝑘=𝐻(𝑡)= 𝑡.Stars Rating(User, Movie, Stars) Simulate answering: SELECT * FROM Rating WHERE Stars = 3.0; Data/index file 1 2 3 4 5 Joe Blade Runner 3.0 Venkat 12 Angry Men 3.0 Xin Blade Runner 3.0 Joe Pulp Fiction 3.5 Venkat Pulp Fiction 4.0 Xin Se7en 4.0 CS 564 (Fall'17)

(Ubiquitous) B+tree Height-balanced (dynamic) tree structure Insert/delete at logF N cost F = fan-out, N = #leaf pages Each node contains d ≤ m ≤ 2d entries (except for root where 1 ≤ m ≤ 2d) i.e. minimum 50% occupancy d is called the order of the tree Supports equality and range searches efficiently Each node corresponds to a disk page Index entries In all the non-leaf nodes (search key value, pid) Think about page and record organization Non-leaf nodes Leaf nodes Root node Data entries Exist only in the leaf nodes (search key value, rid) or (search key value, record) Are sorted according to the search key CS 564 (Fall'17)

Example Index file Data file Height = 1 13 17 24 30 2 3 5 7 14 16 19 20 22 24 27 29 33 34 38 39 Data file Page 1 Page 2 Page 3 Page 4 For now, assume there are no duplicate keys; i.e. the search key is a candidate key CS 564 (Fall'17)

Example (Cont.) Index file 13 17 24 30 Height = 1 Data file 2* 3* 5* 7* 14* 16* 19* 20* 22* 24* 27* 29* 33* 34* 38* 39* Search Starting from root Examine index entries in non-leaf nodes Traverse down the tree in the same manner until a leaf node is reached Non-leaf nodes can be searched using a binary or a linear search Search for 5* 15* all data entries >=24* CS 564 (Fall'17)

B+tree Node/Page Formats Index entry P1 K1 P2 K2 … Pm Km Pm+1 Non-leaf Pages Pointer to a page with SK values < K1 Pointer to a page with K1 ≤ SK values < K2 Pointer to a page with Km-1 ≤ SK values < Km Pointer to a page with SK values ≥ Km Data entry P0 R1 K1 R2 K2 … Rn Kn Pn+1 Leaf Pages Pointer to previous leaf page Pointer to next leaf page Record 1 Record 2 Record n CS 564 (Fall'17)

B+trees in Practice Typical order = 100 Typical fill factor = 67% i.e. each (non-root) node contains between 100 and 200 entries Typical fill factor = 67% i.e. average fan-out = 133 Typical capacities Height = 4 1334 = 312,900,700 records Height = 3 1333 = 2,352,637 records Can often hold top levels in buffer pool Level 1 = 1 page = 8KB Level 2 = 133 pages = 1 MB Level 3 = 17,689 pages = 133 MB CS 564 (Fall'17)

Operations on B+trees A B+tree supports the following operations Equality search e.g. find all records with search key value equal to v Range search e.g. find all records with search key value between v and v’ Insert e.g. insert a record with search key value v Delete e.g. delete a record with search key value v Bulk loading e.g. insert a collection of records CS 564 (Fall'17)

Equality Search: Example Find the record with search key value 38 13 17 24 30 2* 3* 5* 7* 14* 16* 19* 20* 22* 24* 27* 29* 33* 34* 38* 39* CS 564 (Fall'17)

Range Search: Example Find all records with search key values ≥ 15 and < 35 13 17 24 30 2* 3* 5* 7* 14* 16* 19* 20* 22* 24* 27* 29* 33* 34* 38* 39* CS 564 (Fall'17)

Insertion: Example Insert 8* 13 17 24 30 2* 3* 5* 7* 14* 16* 19* 20* 22* 24* 27* 29* 33* 34* 38* 39* CS 564 (Fall'17)

Insertion: Example (Cont.) 13 17 24 30 2* 3* 5* 7* 14* 16* 19* 20* 22* 24* 27* 29* 33* 34* 38* 39* Split! CS 564 (Fall'17)

Insertion: Example (Cont.) Split! Copy up 5 13 17 24 30 2* 3* 14* 16* 19* 20* 22* 24* 27* 29* 33* 34* 38* 39* 5* 7* 8* CS 564 (Fall'17)

Insertion: Example (Cont.) Push up 17 5 13 24 30 2* 3* 5* 7* 8* 14* 16* 19* 20* 22* 24* 27* 29* 33* 34* 38* 39* CS 564 (Fall'17)

Insertion: Example (Cont.) New root node! 17 5 13 24 30 2* 3* 5* 7* 8* 14* 16* 19* 20* 22* 24* 27* 29* 33* 34* 38* 39* CS 564 (Fall'17)

Insertion Algorithm Insert record r with value v Find the correct leaf node L; i.e. the leaf with the correct search key range Insert data entry in L If L has space, DONE! Else, split L (into L and a new node L2) Redistribute keys evenly, copy up middle key Insert index entry pointing to L2 into parent of L This can propagate recursively to other nodes To split a non-leaf node, redistribute entries evenly, but push up the middle key Splits “grow” tree; root split increases height Tree gets wider or one level taller at top CS 564 (Fall'17)

Insertion: Example (Cont.) 17 5 13 24 30 2* 3* 5* 7* 8* 14* 16* 19* 20* 22* 24* 27* 29* 33* 34* 38* 39* Root was split and height increased by 1 Could avoid split by redistributing entries with a sibling Sibling: nodes immediately to left or right with same parent CS 564 (Fall'17)

Deletion: Example Delete 22* 17 5 13 24 30 2* 3* 5* 7* 8* 14* 16* 19* 20* 22* 24* 27* 29* 33* 34* 38* 39* 17 5 13 24 30 2* 3* 5* 7* 8* 14* 16* 19* 20* 24* 27* 29* 33* 34* 38* 39* CS 564 (Fall'17)

Deletion: Example (Cont.) Delete 20* 17 5 13 24 30 2* 3* 5* 7* 8* 14* 16* 19* 20* 24* 27* 29* 33* 34* 38* 39* 17 Copied up 27 5 13 27 30 2* 3* 5* 7* 8* 14* 16* 19* 24* 27* 29* 33* 34* 38* 39* CS 564 (Fall'17)

Deletion: Example (Cont.) And then delete 24* ... 17 5 13 27 30 2* 3* 5* 7* 8* 14* 16* 19* 24* 27* 29* 33* 34* 38* 39* 17 Must merge at this level too! 5 13 30 2* 3* 5* 7* 8* 14* 16* 19* 27* 29* 33* 34* 38* 39* Must merge CS 564 (Fall'17)

Deletion: Example (Cont.) Redistribute at the leaf level Pulled down 17 5 13 17 30 2* 3* 5* 7* 8* 14* 16* 19* 27* 29* 33* 34* 38* 39* CS 564 (Fall'17)

Deletion: Example (Cont.) Can redistribute at the non-leaf nodes too Example: a b+tree during deletion of 24* Redistribute entry from left child of root to right child 22 5 13 17 20 30 2* 3* 5* 7* 8* 14* 16* 17* 18* 20* 21* 22* 27* 29* 33* 34* 38* 39* CS 564 (Fall'17)

Deletion: Example (Cont.) Example: a b+tree during deletion of 24* Let’s move 20* 20 5 13 17 22 30 2* 3* 5* 7* 8* 14* 16* 17* 18* 20* 21* 22* 27* 29* 33* 34* 38* 39* CS 564 (Fall'17)

Deletion: Example (Cont.) Example: a b+tree during deletion of 24* Can move 17* as well 17 5 13 20 22 30 2* 3* 5* 7* 8* 14* 16* 17* 18* 20* 21* 22* 27* 29* 33* 34* 38* 39* CS 564 (Fall'17)

Deletion Algorithm Find the correct leaf node L where entry belongs Remove the entry If L is at least half-full, DONE! If L has only d-1 entries Try to redistribute, borrowing from sibling If redistribution fails, merge L and sibling If a merge occurred, delete an entry from parent of L Merge could propagate to root, decreasing height CS 564 (Fall'17)

Deletion Algorithm (Cont.) Try redistribution with all siblings first, then merge. Why? Good chance that redistribution is possible (large fan-out) Only need to propagate changes to the immediate parent node Reduces likelihood of split on subsequent insertions (files typically grow, not shrink) Since pages would have more space on them CS 564 (Fall'17)

Duplicates What if the search key is not unique? Solution 1: all entries with a given key value reside on a single data file page Use overflow pages if run out of space Solution 2: allow duplicate search key values in data entries Modify search: find the left-most data entry with the desired key value, keep reading till hit a new key (might need to hit more than one leaf page) Use rid to get a unique (composite) key for positioning the data entry in the leaf page Solution 3: use list of rids instead of a single rid in the leaf level Single data entry could still span multiple pages CS 564 (Fall'17)

B+tree Order Concept of b+tree order (d) replaced by physical space criterion in practice (at least half-full) Index (i.e. non-leaf) pages can typically hold many more entries than leaf pages Leaf pages could have actual data records Variable sized records and search keys mean different nodes will contain different numbers of entries Even with fixed length fields, multiple records with the same search key value (duplicates) can lead to variable- sized data entries (e.g. list of rids). CS 564 (Fall'17)

Review Exercise Assume a file which has 950 thousands (that is, 950000) records Say we index this file using a B+ tree In this particular B+ tree, the average page has an occupancy of 100 pointers (i.e. the tree's average branching factor is 100) Assume further that the amount of memory set aside for storing the index is 150 blocks, and that all 950000 records of the above file reside on disk No duplicate search key exists Given a search key K, compute the minimal number of disk I/O needed to retrieve the record with that search key CS 564 (Fall'17)

Hash-based Indexes Next Up Questions? CS 564 (Fall'17)