Presentation is loading. Please wait.

Presentation is loading. Please wait.

COP Introduction to Database Structures

Similar presentations


Presentation on theme: "COP Introduction to Database Structures"— Presentation transcript:

1 COP 3540 - Introduction to Database Structures
Indexing Techniques

2 Tree-Structured Index

3 Introduction As for any index, 3 alternatives for data entries k*:
Data record with key value k <k, rid of data record with search key value k> <k, list of rids of data records with search key k> Tree-structured indexing techniques support both range searches and equality searches. B+ tree: dynamic, adjusts gracefully under inserts and deletes.

4 Range Searches “Find all students with gpa > 3.0’’
If data is in sorted file, do binary search to find first such student, then scan to find others. Cost of binary search can be quite high. Simple idea: Create an ‘index’ file. Page 1 Page 2 Page N Page 3 k2 kN k1 Index File Data File Can do binary search on (smaller) index file!

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

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

7 B+ Trees in Practice Typical order: 100. Typical fill-factor: 67%.
average fanout = 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 = page = Kbytes Level 2 = pages = Mbyte Level 3 = 17,689 pages = 133 MBytes

8 Inserting a Data Entry into a B+ Tree
Find correct leaf L. Put data entry onto L. If L has enough space, done! 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. This can happen recursively To split index node, redistribute entries evenly, but push up middle key. (Contrast with leaf splits.) Splits “grow” tree; root split increases height. Tree growth: gets wider or one level taller at top.

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

10 Example B+ Tree After Inserting 8*
2* 3* Root 17 24 30 14* 16* 19* 20* 22* 24* 27* 29* 33* 34* 38* 39* 13 5 7* 5* 8* Notice that root was split, leading to increase in height.

11 Deleting a Data Entry from a B+ Tree
Start at root, find leaf L where entry belongs. Remove the entry. If L is at least half-full, done! 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. If merge occurred, must delete entry (pointing to L or sibling) from parent of L. Merge could propagate to root, decreasing height.

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

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

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

15 After Re-distribution
Intuitively, entries are re-distributed by ‘pushing through’ the splitting entry in the parent node. It 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 13 5 17 30 20 22

16 Summary (Contd.) Tree-structured indexes are ideal for range-searches, also good for equality searches. B+ tree is a dynamic structure. Inserts/deletes leave tree height-balanced; logF N cost. High fanout (F) means depth rarely more than 3 or 4. Almost always better than maintaining a sorted file. Typically, 67% occupancy on average.

17 Hash-Based Index

18 Introduction As for any index, 3 alternatives for data entries k*:
Data record with key value k <k, rid of data record with search key value k> <k, list of rids of data records with search key k> Choice orthogonal to the indexing technique Hash-based indexes are best for equality selections. Cannot support range searches. Static and dynamic hashing techniques exist.

19 Static Hashing # primary pages fixed, allocated sequentially, never de-allocated; overflow pages if needed. h(k) mod N = bucket to which data entry with key k belongs. (N = # of buckets) h(key) mod N h key Primary bucket pages Overflow pages 2 N-1

20 Linear Hashing Idea: Use a family of hash functions h0, h1, h2, ...
hi(key) = h(key) mod(2iN); N = initial # buckets h is some hash function (range is not 0 to N-1, must be somewhat larger) If N = 2d0, for some d0, hi consists of applying h and looking at the last di bits, where di = d0 + i. hi+1 doubles the range of hi (similar to directory doubling)

21 Linear Hashing (Contd.)
The bucket to be split is chosen in round robin fashion. Splitting proceeds in ‘rounds’. Current round number is Next. Buckets 0 to Next-1 have been split; Next to NR yet to be split.

22 Overview of LH File In the middle of a round. Level h
Buckets that existed at the beginning of this round: this is the range of Next Bucket to be split of other buckets) in this round search key value ) ( Buckets split in this round: If is in this range, must use Level+1 `split image' bucket. to decide if entry is in created (through splitting ‘split image' buckets:

23 Linear Hashing (Contd.)
Insert: Find bucket by applying hLevel or hLevel+1: If bucket to insert into is full: Add overflow page and insert data entry. (Maybe) Split Next bucket and increment Next. Since buckets are split round-robin, long overflow chains don’t develop!

24 On split, hLevel+1 is used to re-distribute entries.
Example of Linear Hashing. Insert 43* (i.e., , where d0 = 2) On split, hLevel+1 is used to re-distribute entries. h 1 (This info is for illustration only!) Level=0, N=4 00 01 10 11 000 001 010 011 (The actual contents of the linear hashed file) Next=0 PRIMARY PAGES Data entry r with h(r)=5 Primary bucket page 44* 36* 32* 25* 9* 5* 14* 18* 10* 30* 31* 35* 11* 7* h 1 Level=0 00 01 10 11 000 001 010 011 Next=1 PRIMARY PAGES 44* 36* 32* 25* 9* 5* 14* 18* 10* 30* 31* 35* 11* 7* OVERFLOW 43* 100

25 Example: End of a Round. Insert 50* (i.e., 110102)
h 1 37* 00 01 10 11 000 001 010 011 100 101 110 Next=0 Level=1 111 PRIMARY PAGES OVERFLOW 32* 9* 25* 66* 18* 10* 34* 35* 11* 44* 36* 5* 29* 43* 14* 30* 22* 31* 7* 50* h 1 22* 00 01 10 11 000 001 010 011 100 Next=3 101 110 Level=0 PRIMARY PAGES OVERFLOW 32* 9* 5* 14* 25* 66* 10* 18* 34* 35* 31* 7* 11* 43* 44* 36* 37* 29* 30*

26 Summary Hash-based indexes: best for equality searches, cannot support range searches. Static Hashing can lead to long overflow chains. Linear Hashing splits buckets round-robin, and using overflow pages. Overflow pages not likely to be long. Duplicates handled easily. Space utilization could be lower than Extendible Hashing, since splits not concentrated on ‘dense’ data areas. Can tune criterion for triggering splits to trade-off slightly longer chains for better space utilization. For hash-based indexes, a skewed data distribution is one in which the hash values of data entries are not uniformly distributed!


Download ppt "COP Introduction to Database Structures"

Similar presentations


Ads by Google