CS525: Advanced Database Organization Notes 4: Indexing and Hashing - - PowerPoint PPT Presentation

cs525 advanced database organization
SMART_READER_LITE
LIVE PREVIEW

CS525: Advanced Database Organization Notes 4: Indexing and Hashing - - PowerPoint PPT Presentation

CS525: Advanced Database Organization Notes 4: Indexing and Hashing Part II: B + -Trees Yousef M. Elmehdwi Department of Computer Science Illinois Institute of Technology yelmehdwi@iit.edu September 11 th , 2018 Slides: adapted from a course


slide-1
SLIDE 1

CS525: Advanced Database Organization

Notes 4: Indexing and Hashing Part II: B+-Trees

Yousef M. Elmehdwi Department of Computer Science Illinois Institute of Technology yelmehdwi@iit.edu

September 11th, 2018

Slides: adapted from a course taught by Hector Garcia-Molina, Stanford, &Principles of Database Management

1 / 73

slide-2
SLIDE 2

Outline

Conventional indexes

Basic Ideas: sparse, dense, multi-level . . . Duplicate Keys Deletion/Insertion Secondary indexes

B+-Trees Hashing schemes

2 / 73

slide-3
SLIDE 3

Multilevel Indexes Revisited

Creating index-to-an-index results in multilevel indexes Multilevel indexes are very useful for speeding up data access if the lowest-level index itself becomes too large to be searched efficiently. Give up “sequentiality” of index Predictable performance under updates Achieve always balance of “tree” Automate restructuring under updates We’ll actually study B+trees

3 / 73

slide-4
SLIDE 4

Multilevel Indexes Revisited

Creating index-to-an-index results in multilevel indexes

4 / 73

slide-5
SLIDE 5

Multilevel Indexes Revisited

Multilevel indexes useful for speeding up data access if lowest level index becomes too large Index can be considered as a sequential file and building an index-to-the-index improves access Higher-level index is, again, a sequential file to which index can be built and so on Lowest level index entries may contain pointers to disk blocks or records Higher-level index contains as many entries as there are blocks in the immediately lower level index Index entry consists of search key value and reference to corresponding block in lower level index Index levels can be added until highest-level index fits within single disk block First-level index, second-level index, third-level index etc.

5 / 73

slide-6
SLIDE 6

Multilevel Indexes Revisited

With binary search on single index, search interval, consisting of disk blocks, is reduced by 2 with every iteration Approximately log2(NBLKI) random block access (rba) to search index consisting of NBLKI blocks

  • ne additional rba needed to actual data file

With multilevel index, search interval is reduced by BFI with every index level (BFI = blocking factor of index)

BFI denotes how many index entries fit within single disk block also called fan-out of index

6 / 73

slide-7
SLIDE 7

Multilevel Indexes Revisited

Multilevel index can be considered as search tree, with each index level representing level in tree, each index block representing a node and each access to the index resulting in navigation towards a subtree in the tree Multilevel indexes may speed up data retrieval, but large multilevel indexes require a lot of maintenance in case of updates

7 / 73

slide-8
SLIDE 8

Another type of index

Give up “sequentiality” of index Predictable performance under updates Achieve always balance of “tree” Automate restructuring under updates We’ll actually study B+trees

8 / 73

slide-9
SLIDE 9

B+tree Structure

B+tree: a dynamic multi-level index B+tree organizes its blocks into a tree. An example of a balanced search tree: every root-to-leaf path has same length Each node (vertex) in the tree is a block, which contains search keys and pointers Parameter n, which is largest value so that n + 1 pointers and n keys fit in one block

Example (1)

If block size is 4096 bytes, keys be integers (4 bytes each), and pointers be 8 bytes each, then n = 340.

9 / 73

slide-10
SLIDE 10

Definitions related to a tree in general

Each node is stored in one block on disk Root node: the node at the “top” (or bottom depending how you draw the tree) of the tree Internal/intermediate node: a node that has one or more child node(s) Leaf node: a node that does not have any children nodes

10 / 73

slide-11
SLIDE 11

Main Challenge in B+Trees1

To ensure good search cost, the tree should be kept of minimum and uniform height. Keep the tree:

full (packed), and balanced (almost uniform height).

This can be difficult – in face of insertions and deletions. Solution: Keep the tree “semi-full” (i.e., each tree node half-full)

Makes it easy to keep the tree balanced and semi-optimal.

1Himanshu Gupta, Stony Brook University, CSE 532 11 / 73

slide-12
SLIDE 12

B+tree Example: n = 3

12 / 73

slide-13
SLIDE 13

Example B+tree nodes with n = 3

Each internal node is stored in one block on disk and contains at most n keys and (n+1) pointers

13 / 73

slide-14
SLIDE 14

Sample non-leaf

14 / 73

slide-15
SLIDE 15

Sample leaf node

The last pointer points to the next leaf node (a disk block) in the B+-tree

15 / 73

slide-16
SLIDE 16

n = 3

Each internal node is stored in one block on disk and contains at most n keys and (n+1) pointers

16 / 73

slide-17
SLIDE 17

Size of nodes :    n + 1 pointers (fixed) n keys

17 / 73

slide-18
SLIDE 18

Don’t want nodes to be too empty

Use a “Fill Factor” to control the growth and the shrinkage. A 50% fill factor would be the minimum for B+tree Use at least (to ensure a balanced tree)

Non-leaf: n+1

2

  • pointers (to nodes)

Except root: required at least 2 be used

Leaf: n+1

2

  • pointers (to data)

18 / 73

slide-19
SLIDE 19

Number of pointers/keys for B+tree

Max ptrs Max keys Min ptrs→ data Min keys Non-leaf (non-root) n + 1 n n+1

2

  • n+1

2

  • − 1

Leaf (non-root) n + 1 n n+1

2

  • n+1

2

  • Root

n + 1 n 2∗ 1

∗When there is only one record in the B+tree, min pointers in the root is 1 (the

  • ther pointers are null)

19 / 73

slide-20
SLIDE 20

B+tree rules: tree of order n

1) All leaves at same lowest level (balanced tree) 2) Pointers in leaves point to records except for “sequence pointer”

20 / 73

slide-21
SLIDE 21

Insert into B+tree

a) simple case

space available in leaf

b) leaf overflow c) non-leaf overflow d) new root

21 / 73

slide-22
SLIDE 22

a) Insert key = 32, n = 3

22 / 73

slide-23
SLIDE 23

a) Insert key = 32, n = 3

23 / 73

slide-24
SLIDE 24

b) Insert key = 7, n = 3

24 / 73

slide-25
SLIDE 25

b) Insert key = 7, n = 3

25 / 73

slide-26
SLIDE 26

b) Insert key = 7, n = 3

26 / 73

slide-27
SLIDE 27

c) Insert key = 160, n = 3

27 / 73

slide-28
SLIDE 28

c) Insert key = 160, n = 3

28 / 73

slide-29
SLIDE 29

c) Insert key = 160, n = 3

29 / 73

slide-30
SLIDE 30

c) Insert key = 160, n = 3

30 / 73

slide-31
SLIDE 31

d) New root, insert 45, n = 3

31 / 73

slide-32
SLIDE 32

d) New root, insert 45, n = 3

32 / 73

slide-33
SLIDE 33

d) New root, insert 45, n = 3

33 / 73

slide-34
SLIDE 34

d) New root, insert 45, n = 3

34 / 73

slide-35
SLIDE 35

Insertion Algorithm

Insert Record with key k Search leaf node for k

Leaf node has at least one space

Insert into leaf

Leaf is full

Split leaf into two nodes (new leaf) Insert new leaf’s smallest key into parent

35 / 73

slide-36
SLIDE 36

Insertion Algorithm (cont.)

Non-leaf node is full

Split parent Insert median key into parent

Root is full

Split root Create new root with two pointers and single key

B+-trees grow at the root

36 / 73

slide-37
SLIDE 37

Deletion from B+tree

a) Simple case b) Coalesce with neighbor (sibling) c) Re-distribute keys d) Cases b) or c) at non-leaf

37 / 73

slide-38
SLIDE 38

a) Delete key = 11, n = 3

38 / 73

slide-39
SLIDE 39

a) Delete key = 11, n = 3

39 / 73

slide-40
SLIDE 40

b) Coalesce with sibling: Delete 50, n = 4

40 / 73

slide-41
SLIDE 41

b) Coalesce with sibling: Delete 50, n = 4

41 / 73

slide-42
SLIDE 42

b) Coalesce with sibling: Delete 50, n = 4

42 / 73

slide-43
SLIDE 43

b) Coalesce with sibling: Delete 50, n = 4

43 / 73

slide-44
SLIDE 44

b) Coalesce with sibling: Delete 50, n = 4

44 / 73

slide-45
SLIDE 45

b) Coalesce with sibling: Delete 50, n = 4

45 / 73

slide-46
SLIDE 46

c) Redistribute keys: Delete 50, n = 4

46 / 73

slide-47
SLIDE 47

c) Redistribute keys: Delete 50, n = 4

47 / 73

slide-48
SLIDE 48

c) Redistribute keys: Delete 50, n = 4

48 / 73

slide-49
SLIDE 49

c) Redistribute keys: Delete 50, n = 4

49 / 73

slide-50
SLIDE 50

c) Redistribute keys: Delete 50, n = 4

50 / 73

slide-51
SLIDE 51

c) Redistribute keys: Delete 50, n = 4

51 / 73

slide-52
SLIDE 52

d) Non-leaf Coalesce: Delete 37, n = 4

52 / 73

slide-53
SLIDE 53

d) Non-leaf Coalesce: Delete 37, n = 4

53 / 73

slide-54
SLIDE 54

d) Non-leaf Coalesce: Delete 37, n = 4

54 / 73

slide-55
SLIDE 55

d) Non-leaf Coalesce: Delete 37, n = 4

55 / 73

slide-56
SLIDE 56

Deletion Algorithm

Delete record with key k Search leaf node for k

Leaf has more than min entries

Remove from leaf

Leaf has min entries

Try to borrow from sibling

One direct sibling has more min entries

Move entry from sibling and adapt key in parent

56 / 73

slide-57
SLIDE 57

Deletion Algorithm (cont.)

Both direct siblings have min entries

Merge with one sibling Remove node or sibling from parent

  • ¿recursive deletion

Root has two children that get merged

Merged node becomes new root

57 / 73

slide-58
SLIDE 58

B+tree deletions in practice

Often, coalescing is not implemented

Too hard and not worth it! Assumption: nodes will fill up in time again

58 / 73

slide-59
SLIDE 59

Splitting or Merging nodes

When splitting or merging nodes follow these conventions:

Leaf Split: In case a leaf node needs to be split during insertion and n is even, the left node should get the extra key. E.g, if n = 2 and we insert a key 4 into a node [1,5], then the resulting nodes should be [1,4] and [5]. For odd values of n we can always evenly split the keys between the two nodes. In both cases the value inserted into the parent is the smallest value of the right node. Non-Leaf Split: In case a non-leaf node needs to be split and n is

  • dd, we cannot split the node evenly (one of the new nodes will have
  • ne more key). In this case the “middle” value inserted into the parent

should be taken from the right node. E.g., if n = 3 and we have to split a non-leaf node [1,3,4,5], the resulting nodes would be [1,3] and [5]. The value inserted into the parent would be 4 Node Underflow: In case of a node underflow you should first try to redistribute values from a sibling and only if this fails merge the node with one of its siblings. Both approaches should prefer the left sibling. E.g., if we can borrow values from both the left and right sibling, you should borrow from the left one.

59 / 73

slide-60
SLIDE 60

Comparison: B+tree vs. static indexed sequential file

Ref #1: Held & Stonebraker, “B-Trees Re-examined”, CACM, Feb. 1978 Ref #2: M. Stonebraker, “Retrospection on a database system”, TODS, June 1980

60 / 73

slide-61
SLIDE 61

Comparison: B+tree vs. static indexed sequential file

B+tree Consumes more space, so lookup slower Each insert/delete potentially restructures Build-in restructuring Predictable performance indexed seq. file Less space, so lookup faster Inserts managed by overflow area Requires temporary restructuring Unpredictable performance

61 / 73

slide-62
SLIDE 62

Variation on B+tree: B-tree (no +)

Idea:

Avoid duplicate keys Have record pointers in non-leaf nodes

62 / 73

slide-63
SLIDE 63

Variation on B+tree: B-tree (no +)

63 / 73

slide-64
SLIDE 64

B-tree example: n = 2

The leaf nodes in a B-tree is not linked i.e., the last record pointer in a leaf node is not used

64 / 73

slide-65
SLIDE 65

B-tree example: n = 2

65 / 73

slide-66
SLIDE 66

B-tree example: n = 2

B-trees have faster lookup than B+trees in B-tree, non-leaf & leaf different sizes in B-tree, deletion more complicated ⇒ B+trees preferred

66 / 73

slide-67
SLIDE 67

But, note:

If blocks are fixed size (due to disk and buffering restrictions) Then lookup for B+tree is actually better

67 / 73

slide-68
SLIDE 68

Example

Consider a DBMS that has the following characteristics:

Pointers 4 bytes Keys 4 bytes Blocks 100 bytes

Compute the maximum number of records we can index with:

a) 2-level B-tree b) 2-level B+tree

68 / 73

slide-69
SLIDE 69

B-tree

Find largest integer value of n such that 4n + 4(2n + 1) < 100 n = 8 Root has 8 keys + 8 record pointers +9 children pointers = 8 × 4 + 8 × 4 + 9 × 4 = 100bytes Each of 9 children: 12 records pointers (+12 keys) = 12 × (4 + 4) + 4 = 100 bytes 2-level B-tree, maximum # of records = 12 × 9 + 8 = 116

69 / 73

slide-70
SLIDE 70

B+tree

Find largest integer value of n such that 4n + 4(n + 1) < 100 n = 12 Root has 12 keys + 13 children pointers = 12 × 4 + 13 × 4 = 100bytes Each of 13 children: 12 records pointers (+12 keys) = 12 × (4 + 4) + 4 = 100 bytes 2-level B+tree, maximum # of records = 13 × 12 = 156

70 / 73

slide-71
SLIDE 71

So,

71 / 73

slide-72
SLIDE 72

Reading

Chapter 4: Index Structures (uploaded on course Blackboard)

72 / 73

slide-73
SLIDE 73

Next

Hashing schemes

73 / 73