Files & Indexing. Files of Records uPage or block is OK when doing I/O, but higher levels of DBMS operate on records, and files of records. uFILE : A.

Slides:



Advertisements
Similar presentations
B+-Trees and Hashing Techniques for Storage and Index Structures
Advertisements

Database Management Systems, R. Ramakrishnan and J. Gehrke1 Tree-Structured Indexes Chapter 9.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18 Indexing Structures for Files.
1 Tree-Structured Indexes Module 4, Lecture 4. 2 Introduction As for any index, 3 alternatives for data entries k* : 1. Data record with key value k 2.
B+-trees. Model of Computation Data stored on disk(s) Minimum transfer unit: a page = b bytes or B records (or block) N records -> N/B = n pages I/O complexity:
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
CS CS4432: Database Systems II Basic indexing.
B+-tree and Hashing.
Tree-Structured Indexes Lecture 5 R & G Chapter 9 “If I had eight hours to chop down a tree, I'd spend six sharpening my ax.” Abraham Lincoln.
Tree-Structured Indexes Lecture 17 R & G Chapter 9 “If I had eight hours to chop down a tree, I'd spend six sharpening my ax.” Abraham Lincoln.
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.
1 Indexing Structures for Files. 2 Basic Concepts  Indexing mechanisms used to speed up access to desired data without having to scan entire.
Tree-Structured Indexes Lecture 5 R & G Chapter 10 “If I had eight hours to chop down a tree, I'd spend six sharpening my ax.” Abraham Lincoln.
1 B+ Trees. 2 Tree-Structured Indices v Tree-structured indexing techniques support both range searches and equality searches. v ISAM : static structure;
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.
Adapted from Mike Franklin
1.1 CS220 Database Systems Tree Based Indexing: B+-tree Slides courtesy G. Kollios Boston University via UC Berkeley.
Tree-Structured Indexes Chapter Indexes Sometimes, we want to retrieve records by specifying the values in one or more fields, e.g., –Find all students.
Tree-Structured Indexes Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein for some slides.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Tree-Structured Indexes.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Tree- and Hash-Structured Indexes Selected Sections of Chapters 10 & 11.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Tree-Structured Indexes Chapter 9.
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.
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.
I/O Cost Model, Tree Indexes CS634 Lecture 5, Feb 12, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
Tree-Structured Indexes Chapter 10
CS 405G: Introduction to Database Systems 12. Index.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Tree-Structured Indexes Chapter 10.
Tree-Structured Indexes R & G Chapter 10 “If I had eight hours to chop down a tree, I'd spend six sharpening my ax.” Abraham Lincoln.
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.
Tree-Structured Indexes
Tree-Structured Indexes
COP Introduction to Database Structures
B+-Trees and Static Hashing
Tree-Structured Indexes
CS222/CS122C: Principles of Data Management Notes #07 B+ Trees
Tree-Structured Indexes
Tree-Structured 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
Adapted from Mike Franklin
Tree-Structured Indexes
Indexing 1.
Database Systems (資料庫系統)
Storage and Indexing.
Database Systems (資料庫系統)
General External Merge Sort
Tree-Structured Indexes
Tree-Structured Indexes
Indexing February 28th, 2003 Lecture 20.
Tree-Structured Indexes
Tree-Structured Indexes
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:

Files & Indexing

Files of Records uPage or block is OK when doing I/O, but higher levels of DBMS operate on records, and files of records. uFILE : A collection of pages, each containing a collection of records. Must support: w insert/delete/modify records w read a particular record (specified using record id) w scan records (possibly with some conditions on the records to be retrieved)

Alternative File Organizations Many alternatives exist, tradeoffs for each: wHeap files: Suitable when typical access is file scan of all records. wSorted Files: Best for retrieval in search key order Also good for search based on search key w Indexes: Organize records via trees or hashing. Like sorted files, speed up searches for search key fields Updates are much faster than in sorted files.

Unordered (Heap) Files uSimplest file structure contains records in no particular order. uAs file grows and shrinks, disk pages are allocated and de-allocated. uTo support record level operations, we must: w keep track of the pages in a file w keep track of free space on pages w keep track of the records on a page uThere are many alternatives for keeping track of this.

Heap File Implemented as a List uThe header page id and Heap file name must be stored someplace. uProblem: Most pages might be on free space list (holes) Header Page Data Page Data Page Data Page Data Page Data Page Data Page Pages with Free Space Full Pages

Heap File Using a Page Directory uThe entry for a page can include the number of free bytes on the page. uThe directory is a collection of pages; linked list implementation is just one alternative. w Much smaller than linked list of all HF pages! Data Page 1 Data Page 2 Data Page N Header Page DIRECTORY

Indexes uSometimes need to retrieve records by the values in one or more fields, e.g., wFind all students in the “CS” department wFind all students with a gpa > 3 uAn index on a file is a: wDisk-based data structure wSpeeds up selections on the search key fields for the index. wAny subset of the fields of a relation can be index search key wSearch key is not the same as (candidate) key (e.g. doesn’t have to be unique). uAn index wContains a collection of index and data entries wSupports efficient retrieval of all records with a given search key value k.

Given condition(s) on attribute(s) find qualified records Attr = value Condition may also be uAttr>value uAttr>=value  value Qualified records value Goal of Indexing

9 First Question About Indexes uWhat kinds of selections do they support? wSelections of form field constant wEquality selections (op is =) wRange selections (op is one of, =, BETWEEN) wMore exotic selections: 2-dimensional ranges (“east of Troy and west of Schenectady and North of Albany and South of Watervliet”) –Or n-dimensional 2-dimensional distances (“within 2 miles of Sage Hall”) –Or n-dimensional Ranking queries (“10 italian restaurants closest to Troy”) Regular expression matches, genome string matches, etc.

Alternatives for Data Entry k* in Index uThree alternatives:  Actual data record (with key value k)  uChoice is orthogonal to the indexing technique. wtechniques: B+ trees, hash-tables, R trees, … wTypically, index contains auxiliary information that directs searches to the desired data entries uCan have multiple (different) indexes per file. wE.g. file sorted by age, with a hash index on salary and a B+tree index on name.

Basic Indexing Methods uIndexed Sequential File uB-Tree uHash Index

Indexed Sequential File uSearch key (  primary key) uPrimary index (on Sequencing field) wThe index on the attribute (a.k.a. search key) that determines the sequencing of the table uSecondary index wIndex on any other attribute uDense index (all Search Key values in) uSparse index uMulti-level index

Sequential File Tuples are sorted by their primary key Block

Sequential File Dense Index Index file needs much fewer blocks than the data file, hence easier to fit in memory For a given key K, only log 2 n, out of n, index blocks need to be accessed

Sequential File Sparse Index Typically, only one key per data block Find the index record with largest value that is less or equal to the value we are looking

Sequential File Sparse 2nd level Treat the index as a file and build an index on it Two levels are usually sufficient More than three levels are rare {FILE,INDEX} may be contiguous or not

Deletion from sparse index

Deletion from sparse index – delete record If the deleted entry does not appear in the index do nothing

Deletion from sparse index – delete record If the deleted entry appears in the index replace it with the next search-key value

Deletion from sparse index – delete records 30 & If the next search key value has its own index entry, then delete the entry

Deletion from dense index

Deletion from dense index – delete record Deletion from dense primary index file is handled in the same way with deletion from a sequential file

Insertion, sparse index case

Insertion, sparse index case – insert record our lucky day! we have free space where we need it!

Insertion, sparse index case – insert record Illustrated: Immediate reorganization Variation: – insert new block (chained file) – update index

Insertion, sparse index case – insert record overflow blocks (reorganize later...) How often do we reorganize and how expensive is it? B-Trees offer convincing answers

Index (sequential) continuous free space overflow area (not sequential) Insertion Example

Conventional Indexes uAdvantage: wSimple algorithms wIndex is sequential file good for scans uDisadvantage: wInserts expensive, and/or wEventually sequentiality is lost because of overflows reorganizations are needed

B + -Tree Index

B+ Tree Indexes  Leaf pages contain data entries, and are chained (prev & next)  Non-leaf pages contain index entries and direct searches: P 0 K 1 P 1 K 2 P 2 K m P m index entry Non-leaf Pages Leaf

Example B+ Tree uFind 28*? 29*? All > 15* and < 30* uInsert/delete: Find data entry in leaf, then change it. Need to adjust parent sometimes. wAnd change sometimes bubbles up the tree 2*3* Root *16* 33*34* 38* 39* 135 7*5*8*22*24* 27 27*29* Entries < 17Entries >= 17

B+ Tree: Most Widely Used Index uInsert/delete at log F N cost; keep tree height- balanced. (F = fanout, N = # leaf pages) uMinimum 50% occupancy (except for root). Each node contains d <= m <= 2d entries. The parameter d is called the order of the tree. uSupports equality and range-searches efficiently. Index Entries Data Entries ("Sequence set") (Direct search)

Example B+ Tree uSearch begins at root, and key comparisons direct it to a leaf. uSearch for 5*, 15*, all data entries >= 24*... * Based on the search for 15*, we know it is not in the tree! Root * 3*5* 7*14*16* 19*20*22*24*27* 29*33*34* 38* 39* 13

Inserting into a B+ Tree uFind correct leaf L. uPut data entry onto L. w If L has enough space, done! w Else, must split L (into L and a new node L2) Redistribute entries evenly, copy up middle key. Insert index entry pointing to L2 into parent of L. uThis can happen recursively w To split index node, redistribute entries evenly, but push up middle key. (Contrast with leaf splits.) uSplits “grow” tree; root split increases height. w Tree growth: gets wider or one level taller at top.

Inserting 8* into Example B+ Tree uObserve how minimum occupancy is guaranteed in both leaf and index page splits. uNote difference between copy- up and push- up; be sure you understand the reasons for this. 2* 3*5* 7* 8* 5 Entry to be inserted in parent node. (Note that 5 is continues to appear in the leaf.) s copied up and appears once in the index. Contrast Entry to be inserted in parent node. (Note that 17 is pushed up and only this with a leaf split.)

Example B+ Tree After Inserting 8*  Notice that root was split, leading to increase in height.  In this example, we can avoid split by re-distributing entries; however, this is usually not done in practice. 2*3* Root *16* 19*20*22*24*27* 29*33*34* 38* 39* 135 7*5*8*

Deleting from a B+ Tree uStart at root, find leaf L where entry belongs. uRemove the entry. w If L is at least half-full, done! w If L has only d-1 entries, Try to re-distribute, borrowing from sibling (adjacent node with same parent as L). If re-distribution fails, merge L and sibling. uIf merge occurred, must delete entry (pointing to L or sibling) from parent of L. uMerge could propagate to root, decreasing height.

Example Tree After (Inserting 8*, Then) Deleting 19* and 20*... uDeleting 19* is easy. uDeleting 20* is done with re- distribution. Notice how middle key is copied up. 2*3* Root *16* 33*34* 38* 39* 135 7*5*8*22*24* 27 27*29*

... And Then Deleting 24* uMust merge. uObserve `toss’ of index entry (on right), and `pull down’ of index entry (below) *27* 29*33*34* 38* 39* 2* 3* 7* 14*16* 22* 27* 29* 33*34* 38* 39* 5*8* Root

Non-leaf Re-distribution uTree is shown below during deletion of 24*. (What could be a possible initial tree?) uIn contrast to previous example, can re- distribute entry from left child of root to right child. Root *16* 17*18* 20*33*34* 38* 39* 22*27*29*21* 7*5*8* 3*2*

After Re-distribution uIntuitively, entries are re-distributed by `pushing through’ the splitting entry in the parent node. uIt suffices to re-distribute index entry with key 20; we’ve re-distributed 17 as well for illustration. 14*16* 33*34* 38* 39* 22*27*29* 17*18* 20*21* 7*5*8* 2*3* Root

Bulk Loading of a B+ Tree uIf we have a large collection of records, and we want to create a B+ tree on some field, doing so by repeatedly inserting records is very slow. uBulk Loading can be done much more efficiently. uInitialization: Sort all data entries, insert pointer to first (leaf) page in a new (root) page. 3* 4* 6*9*10*11*12*13* 20*22* 23*31* 35* 36*38*41*44* Sorted pages of data entries; not yet in B+ tree Root

Bulk Loading (Contd.) uIndex entries for leaf pages always entered into right- most index page just above leaf level. When this fills up, it splits. (Split may go up right-most path to the root.) uMuch faster than repeated inserts, especially when one considers locking! 3* 4* 6*9*10*11*12*13* 20*22* 23*31* 35* 36*38*41*44* Root Data entry pages not yet in B+ tree * 4* 6*9*10*11*12*13* 20*22* 23*31* 35* 36*38*41*44* 6 Root not yet in B+ tree Data entry pages