37
Chapter 9 of DBMS • First we look at a simple (strawman) approach (ISAM). • We will see why it is unsatisfactory. • This will motivate the B+Tree Read 9.1 to 9.5 • Glance at 9.6 and 9.8.2 • Homework (won’t be graded, but TA’s will be happy to answer questions) 9.1 and 9.2 (with more emphasis on the insertion questions, less on deletion) • For Tuesday glance at chapter 10. • On Thursday Nov 7 th , there will be no traditional class. Instead, I will be in my office, and the two TAs will be in the TA Lab, to answer questions during the

Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to

  • View
    228

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to

Chapter 9 of DBMS• First we look at a simple (strawman) approach (ISAM).• We will see why it is unsatisfactory.• This will motivate the B+Tree

• Read 9.1 to 9.5• Glance at 9.6 and 9.8.2

• Homework (won’t be graded, but TA’s will be happy to answer questions) 9.1 and 9.2 (with more emphasis on the insertion questions, less on deletion)

• For Tuesday glance at chapter 10.

• On Thursday Nov 7th, there will be no traditional class. Instead, I will be in my office, and the two TAs will be in the TA Lab, to answer questions during the normal class hours. (We will not have prepared remarks, we will simply answer questions. Participation is optional, the usefulness of the experience depends on you coming in with meaningful questions).

Page 2: Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to

Indexed Sequential Access MethodIndexed Sequential Access Method

DataPage 1

DataPage 2

DataPage 3 :: Data

Page N-1DataPage N

• If our large database is sorted, we can speed up search by doing binary search on the entire database.

• However this means we must do log(N) disk accesses…

• The idea of ISAM is to do a faster, approximate binary search in main memory, and use this information to do fewer disk accesses (usually only one).

Page 3: Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to

Indexed Sequential Access MethodIndexed Sequential Access Method

K P• An index entry is a <key,pointer> pair, where key is the value of the first key on the page, and pointer, points to the page.

• Example

Data Page 7

Maggie q 4

Manjula p 3

Marge d 5

Monty f 4

Maggie page 7

Page 4: Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to

Indexed Sequential Access MethodIndexed Sequential Access Method• An index file is a concatenation of index entries. Together with one extra pointer at the beginning.

• Example K1 P1 K2 P2 K3 P3K3 P0

Maggie page 2 Waylon page 3Maggie page 1

Page 5: Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to

Indexed Sequential Access MethodIndexed Sequential Access Method

7 P4K3 P3

Every record pointed to by this pointer has a value greater that or equal to 7

Every record pointed to by this pointer has a value less that 7

Lets look at an index file (this one is the smallest possible example)

Page 6: Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to

Indexed Sequential Access MethodIndexed Sequential Access Method

DataPage 1

DataPage 2

DataPage 3 :: Data

Page N-1DataPage N

125 16 19::p p p p p pIndex File

Data Files

• Instead of doing binary search on the data files, we can do binary search on the index, to find the largest value, which is equal to or less than the search key. We then use the pointer to go to disk to retrieve the relevant block from disk. • Example: we are searching for 8, we do a binary search to find 5, we retrieve page 2, and search it to find a match (if there is one).

• Find the largest entry less than the key, follow right child. (find 20)• Find the smallest entry greater than or equal to the key, follow left child. (find 2)

Page 7: Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to

Indexed Sequential Access MethodIndexed Sequential Access Method

DataPage 1

DataPage 2

DataPage 3 :: Data

Page N-1DataPage N

52 34 77::p p p p p pIndex File

Data Files

• How big should the index file be?• How about more pointers per page?• We could have two pointers to each page (on average, or exactly).• This does not help, because we have to retrieve a block at a time.

Page 8: Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to

Indexed Sequential Access MethodIndexed Sequential Access Method• How big should the index file be?• How about less pointers per page?• We could have a pointer for each two pages (on average, or exactly).• This might help, because it makes the index smaller. We can do a little trick of adding “sideways” pointers. Actually, these “sideways” pointer can be useful for another reason,

they can be helpful for range queries.

• However, to few pointers leads to chaining….

DataPage 1

DataPage 2

DataPage 3 :: Data

Page N-1DataPage N

125 16 19::p p p p p pIndex File

Data Files

Page 9: Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to

Indexed Sequential Access MethodIndexed Sequential Access Method

DataPage 1

DataPage 2

DataPage 3 :: Data

Page N-1DataPage N

125 16 19::p p p p p pIndex File

Data Files

• We have seen that too small or too large an index (in other words too few or too many pointers) can be a problem. But suppose the index does not fit in main memory?• The key observation is that the index itself is a sort of database, so lets build an index on the index!

21p

Page 10: Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to

30135 1814 16 35 43

DataPage 1

DataPage 2

DataPage 4

DataPage 3

DataPage 5

DataPage 6

DataPage 7

DataPage 9

DataPage 8

DataPage 10

• An index of indexes is a tree!

• We can use this structure to do fast equality search. Find 15, 0

• What about range search?• It looks like we have solved our fast indexing problem, but there is a catch, what happens if we have a deletion, an insertion?

Tree Based IndexingTree Based Indexing

17Define rootinternal nodeleaf

Page 11: Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to

30135 1814 16 35 43

DataPage 1

DataPage 2

DataPage 4

DataPage 3

DataPage 5

DataPage 6

DataPage 7

DataPage 9

DataPage 8

DataPage 10

• What happens if we have a deletion? (not much) • What happens if we have an insertion? (trouble!)

• Solution: Overflow Buckets • If we have enough overflow buckets, we might as well have no index at all

Tree Based IndexingTree Based Indexing

Over-flow 1

Suppose we add a bunch of 15 year olds to the database…

17

Page 12: Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to

BB++-Tree Index Files-Tree Index Files

• Disadvantage of indexed-sequential files: performance degrades as file grows, since many overflow blocks get created. Periodic reorganization of entire file is required.

• Advantage of B+-tree index files: automatically reorganizes itself with small, local, changes, in the face of insertions and deletions. Reorganization of entire file is not required to maintain performance.

• Disadvantage of B+-trees: extra insertion and deletion overhead, space overhead.

• Advantages of B+-trees outweigh disadvantages, and they are used extensively.

B+-tree indices are an alternative to indexed-sequential files.

Page 13: Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to

BB++-Tree Index Files (Cont.)-Tree Index Files (Cont.)

• All paths from root to leaf are of the same length• Let d be the order of the tree (user specified), and let

d = n/2• Each node that is not a root or a leaf has between [n/2]

and n children.• A leaf node has between [(n–1)/2] and n–1 values• Special cases:

– If the root is not a leaf, it has at least 2 children.– If the root is a leaf (that is, there are no other nodes in the

tree), it can have between 0 and (n–1) values.

A B+-tree is a rooted tree satisfying the following properties:

Page 14: Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to

BB++-Tree Node Structure-Tree Node Structure

• Typical node

– Ki are the search-key values – Pi are pointers to children (for non-leaf nodes) or pointers

to records (for leaf nodes).– The fan-out is the number of pointers

• The search-keys in a node are ordered

K1 < K2 < K3 < . . . < Kn–1

Page 15: Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to

Leaf Nodes in BLeaf Nodes in B++-Trees-Trees

• For i = 1, 2, . . ., n–1, pointer Pi points to a file record with search-key value Ki.

• If Li, Lj are leaf nodes and i < j, Li’s search-key values are less than Lj’s search-key values

• Pn points to next leaf node in search-key order

Properties of a leaf node:

Page 16: Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to

Non-Leaf Nodes in BNon-Leaf Nodes in B++-Trees-Trees• Non leaf nodes form a multi-level sparse index on the

leaf nodes. For a non-leaf node with m pointers:– All the search-keys in the subtree to which P1 points are less

than K1

– For 2 i n – 1, all the search-keys in the subtree to which Pi points have values greater than or equal to Ki–1 and less than Km–1

5

60.0000

or greater

60

59.99..

or less

4.9999..

or less

Page 17: Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to

Example of a BExample of a B++-tree-tree

B+-tree for account file (n = 3)

• Leaf nodes must have between 1 and 2 values ((n–1)/2 and n –1, with n = 3).

• Non-leaf nodes other than root must have between 2 and 3 children ((n/2 and n with n =3).

• Root must have at least 2 children.

Page 18: Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to

Example of BExample of B++-tree-tree

• Leaf nodes must have between 2 and 4 values ((n–1)/2 and n –1, with n = 5).

• Non-leaf nodes other than root must have between 3 and 5 children ((n/2 and n with n =5).

• Root must have at least 2 children.

B+-tree for account file (n = 5)

Page 19: Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to

Observations about BObservations about B++-trees-trees• Since the inter-node connections are done by

pointers, “logically” close blocks need not be “physically” close.

• The non-leaf levels of the B+-tree form a hierarchy of sparse indices.

• The B+-tree contains a relatively small number of levels (logarithmic in the size of the main file), thus searches can be conducted efficiently. (and a large base logarithm at that)

• Insertions and deletions to the main file can be handled efficiently, as the index can be restructured in logarithmic time (as we shall see).

Page 20: Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to

Queries on BQueries on B++-Trees-Trees• Find all records with a search-key value of k.

1. Start with the root node1. Examine the node for the smallest search-key value > k.

2. If such a value exists, assume it is Kj. Then follow Pi to the child node

3. Otherwise k Km–1, where there are m pointers in the node. Then follow Pm to the child node.

2. If the node reached by following the pointer above is not a leaf node, repeat the above procedure on the node, and follow the corresponding pointer.

3. Eventually reach a leaf node. If for some i, key Ki = k follow pointer Pi to the desired record. Else no record with search-key value k exists.

Page 21: Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to

2* 3*

Root

17

30

14* 16* 33* 34* 38* 39*

135

7*5* 8* 22* 24*

27

27* 29*

Entries <= 17 Entries > 17

What is the difference between 5 and 5* ?How come 30 is in the index, but there is no 30* in the file?

Find 28*, Find 0*, Find all records > 25 and < 31

Queries on BQueries on B++-Trees-Trees

Page 22: Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to

Queries on BQueries on B+-+-Trees (Cont.)Trees (Cont.)• In processing a query, a path is traversed in the tree from the

root to some leaf node.

• If there are K search-key values in the file, the path is no longer than logn/2(K).

• A node is generally the same size as a disk block, typically 4 kilobytes, and n is typically around 100 (40 bytes per index entry).

• With 1 million search key values and n = 100, at most log50(1,000,000) = 4 nodes are accessed in a lookup.

• Contrast this with a balanced binary free with 1 million search key values — around 20 nodes are accessed in a lookup– above difference is significant since every node access may need a disk

I/O, costing around 20 milliseconds!

Page 23: Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to

Updates on BUpdates on B++-Trees: Insertion-Trees: Insertion• Find the leaf node in which the search-key value

would appear• If the search-key value is already there in the leaf

node, record is added to file and if necessary a pointer is inserted into the bucket.

• If the search-key value is not there, then add the record to the main file and create a bucket if necessary. Then:– If there is room in the leaf node, insert (key-value,

pointer) pair in the leaf node– Otherwise, split the node (along with the new (key-value,

pointer) entry) as discussed in the next slide.

Page 24: Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to

2* 3* 19* 20*

1713

16*14* 24* 27* 40* 41*

24 30

5* 7* 22* 28* 45* 77*

Updates on BUpdates on B++-Trees: Insertion-Trees: Insertion

2* 3* 19* 20*

1713

16*14* 24* 27* 40* 41*

24 30

5* 7* 22* 28* 45* 77*

Insert 23

23*

This is the easy case!

Page 25: Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to

2* 3* 19* 20*

1713

16*14* 24* 27* 40* 41*

24 30

5* 7* 22* 28* 45* 77*

Updates on BUpdates on B++-Trees: Insertion-Trees: Insertion

Insert 8

2* 3* 19* 20*

1713

16*14* 24* 27* 40* 41*

24 30

22* 28* 45* 77*5* 7* 8*

5

Because the insertion will cause overfill, we split the leaf node into two nodes, we split the data into two nodes (and distribute the data evenly between them). “5” is special, since it discriminates between the two new siblings, so it is copied up. We now need to insert 5 into the parent node…

Page 26: Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to

Updates on BUpdates on B++-Trees: Insertion-Trees: Insertion

2* 3* 19* 20*

1713

16*14* 24* 27* 40* 41*

24 30

22* 28* 45* 77*5* 7* 8*

5

2* 3* 19* 20*

3024

16*14* 24* 27* 40* 41*22* 28* 45* 77*5* 7* 8*

135

17

We now need to insert 5 into the parent node…

Because the insertion will cause overfill, we split the node into two nodes, we split the data into two nodes. “17” is special, since it discriminates between the two new siblings, so it is pushed up.

Page 27: Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to

Updates on BUpdates on B++-Trees: Insertion-Trees: Insertion

2* 3* 19* 20*

3024

16*14* 24* 27* 40* 41*22* 28* 45* 77*5* 7* 8*

135

17

2* 3* 19* 20*

3024

16*14* 24* 27* 40* 41*22* 28* 45* 77*5* 7* 8*

135

17

The insertion of 8 has increased the height of the tree by one (this is rare).

Page 28: Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to

Updates on BUpdates on B++-Trees: Insertion (Cont.)-Trees: Insertion (Cont.)• Splitting a node:

– take the n(search-key value, pointer) pairs (including the one being inserted) in sorted order. Place the first n/2 in the original node, and the rest in a new node.

– let the new node be p, and let k be the least key value in p. Insert (k,p) in the parent of the node being split. If the parent is full, split it and propagate the split further up.

• The splitting of nodes proceeds upwards till a node that is not full is found. In the worst case the root node may be split increasing the height of the tree by 1.

Result of splitting node containing Brighton and Downtown on inserting Clearview

Page 29: Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to

Updates on BUpdates on B++-Trees: Insertion (Cont.)-Trees: Insertion (Cont.)

B+-Tree before and after insertion of “Clearview”

Page 30: Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to

Updates on BUpdates on B++-Trees: Deletion-Trees: Deletion• Find the record to be deleted, and remove it from the

main file and from the bucket (if present)• Remove (search-key value, pointer) from the leaf node

if there is no bucket or if the bucket has become empty• If the node has too few entries due to the removal, and

the entries in the node and a sibling fit into a single node, then – Insert all the search-key values in the two nodes into a single

node (the one on the left), and delete the other node.

– Delete the pair (Ki–1, Pi), where Pi is the pointer to the deleted node, from its parent, recursively using the above procedure.

Page 31: Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to

Updates on BUpdates on B++-Trees: Deletion-Trees: Deletion• Otherwise, if the node has too few entries due to the

removal, and the entries in the node and a sibling fit into a single node, then– Redistribute the pointers between the node and a sibling

such that both have more than the minimum number of entries.

– Update the corresponding search-key value in the parent of the node.

• The node deletions may cascade upwards till a node which has n/2 or more pointers is found. If the root node has only one pointer after deletion, it is deleted and the sole child becomes the root.

Page 32: Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to

Examples of BExamples of B++-Tree Deletion-Tree Deletion

• The removal of the leaf node containing “Downtown” did not result in its parent having too little pointers. So the cascaded deletions stopped with the deleted leaf node’s parent.

Before and after deleting “Downtown”

Page 33: Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to

Examples of BExamples of B++-Tree Deletion (Cont.)-Tree Deletion (Cont.)

• Node with “Perryridge” becomes underfull (actually empty, in this special case) and merged with its sibling.

• As a result “Perryridge” node’s parent became underfull, and was merged with its sibling (and an entry was deleted from their parent)

• Root node then had only one child, and was deleted and its child became the new root node

Deletion of “Perryridge” from result of previous example

Page 34: Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to

Example of BExample of B++-tree Deletion (Cont.)-tree Deletion (Cont.)

• Parent of leaf containing Perryridge became underfull, and borrowed a pointer from its left sibling

• Search-key value in the parent’s parent changes as a result

Before and after deletion of “Perryridge” from earlier example

Page 35: Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to

BB++-Tree File Optimization-Tree File Optimization• Good space utilization important since records use more space than pointers. • To improve space utilization, involve more sibling nodes in redistribution during splits and merges

– Involving 2 siblings in redistribution (to avoid split / merge where possible) results in each node having at least entries

3/2n

2* 3* 19* 20*

1713

16*14* 24* 27* 40* 41*

24 30

5* 7* 21* 28* 45* 77*22*

Insert 22

Page 36: Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to

Bulk LoadingBulk Loading

Page 37: Chapter 9 of DBMS First we look at a simple (strawman) approach (ISAM). We will see why it is unsatisfactory. This will motivate the B+Tree Read 9.1 to

Indices in SQLIndices in SQL

CREATE INDEX gpa_ranking ON Students

WITH STRUCTURE = BTREE KEY = gpa