Docsity

Indexing-Database System Principles-Lecture Slides, Slides of Principles of Database Management

Toggle sidebar
bg1
1
CS 245 Notes 4 1
CS 245: Database System
Principles
Notes 4: Indexing
Hector Garcia-Molina
CS 245 Notes 4 2
Indexing & Hashing
value
Chapter 4
value
record
CS 245 Notes 4 3
Topics
• Conventional indexes
• B-trees
• Hashing schemes
CS 245 Notes 4 4
Sequential File
20
10
40
30
60
50
80
70
100
90
CS 245 Notes 4 5
Sequential File
20
10
40
30
60
50
80
70
100
90
Dense Index
10
20
30
40
50
60
70
80
90
100
110
120
CS 245 Notes 4 6
Sequential File
20
10
40
30
60
50
80
70
100
90
Sparse Index
10
30
50
70
90
110
130
150
170
190
210
230
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download Indexing-Database System Principles-Lecture Slides and more Slides Principles of Database Management in PDF only on Docsity!

CS 245 Notes 4 1

CS 245: Database System

Principles

Notes 4: Indexing

Hector Garcia-Molina

CS 245 Notes 4 2

Indexing & Hashing

value

Chapter 4

 value

record

CS 245 Notes 4 3

Topics

• Conventional indexes

• B-trees

• Hashing schemes

CS 245 Notes 4 4

Sequential File 2010 4030 6050 8070 10090

CS 245 Notes 4 5

Sequential File 2010 4030 6050 8070 10090

Dense Index (^1020) (^3040) (^5060) (^7080) 10090 (^110120) CS 245 Notes 4 6

Sequential File 2010 4030 6050 8070 10090

Sparse Index (^1030) (^5070) 11090 (^130150) (^170190) (^210230)

CS 245 Notes 4 7

Sequential File 2010 4030 6050 8070 10090

Sparse 2nd level (^1030) (^5070) 11090 (^130150) (^170190) (^210230)

(^1090) (^170250) (^330410) (^490570)

CS 245 Notes 4 8

  • Comment:

{FILE,INDEX} may be contiguous

or not (blocks chained)

CS 245 Notes 4 9

Question:

  • Can we build a dense, 2nd level indexfor a dense index?

CS 245 Notes 4 10

Notes on pointers:

(1) Block pointer (sparse index) can besmaller than record pointer

BP

RP

CS 245 Notes 4 11

(2) If file is contiguous, then we can omit

pointers (i.e., compute them)

Notes on pointers:

CS 245 Notes 4 12

K
K3K4K

R R R R

CS 245 Notes 4 19

(^1020) (^3040)

Dense index, better way?

Duplicate keys

CS 245 Notes 4 20

(^1010) (^2030)

Sparse index, one way?

Duplicate keys

CS 245 Notes 4 21

(^1010) (^2030)

Sparse index, one way?

Duplicate keys

careful if lookingfor 20 or 30!

CS 245 Notes 4 22

(^1020) (^3030)

Sparse index, another way?

Duplicate keys

  • place first new key from block

CS 245 Notes 4 23

(^1020) (^3030)

Sparse index, another way?

Duplicate keys

  • place first new key from block

shouldthis be

CS 245 Notes 4 24

Duplicate values, primary index

  • Index may point to first instance ofeach value only

File

Index

Summary

a

aa b



CS 245 Notes 4 25

Deletion from sparse index

2010 4030 6050 8070

(^1030) (^5070) 11090 (^130150) CS 245 Notes 4 26

Deletion from sparse index 2010 4030 6050 8070

(^1030) (^5070) 11090 (^130150)

  • delete record 40

CS 245 Notes 4 27

Deletion from sparse index

2010 4030 6050 8070

(^1030) (^5070) 11090 (^130150)

  • delete record 40

CS 245 Notes 4 28

Deletion from sparse index 2010 4030 6050 8070

(^1030) (^5070) 11090 (^130150)

  • delete record 30

CS 245 Notes 4 29

Deletion from sparse index

2010 4030 6050 8070

(^1030) (^5070) 11090 (^130150)

  • delete record 30 (^4040)

CS 245 Notes 4 30

Deletion from sparse index 2010 4030 6050 8070

(^1030) (^5070) 11090 (^130150)

  • delete records 30 & 40

CS 245 Notes 4 37

Insertion, sparse index case

(^201030) (^504060)

(^1030) (^4060)

CS 245 Notes 4 38

Insertion, sparse index case

(^201030) (^504060)

(^1030) (^4060)

  • insert record 34

CS 245 Notes 4 39

Insertion, sparse index case

(^201030) (^504060)

(^1030) (^4060)

  • insert record 34

34

  • our lucky day!we have free space where we need it! CS 245 Notes 4 40

Insertion, sparse index case

(^201030) (^504060)

(^1030) (^4060)

  • insert record 15

CS 245 Notes 4 41

Insertion, sparse index case

(^201030) (^504060)

(^1030) (^4060)

  • insert record 15 (^1520) 30

CS 245 Notes 4 42

Insertion, sparse index case

(^201030) (^504060)

(^1030) (^4060)

  • insert record 15 (^1520) 30
  • Illustrated: Immediate reorganization• Variation:
    • insert new block (chained file)– update index

CS 245 Notes 4 43

Insertion, sparse index case (^201030) (^504060)

(^1030) (^4060)

  • insert record 25

CS 245 Notes 4 44

Insertion, sparse index case (^201030) (^504060)

(^1030) (^4060)

  • insert record 25 25 overflow blocks(reorganize later...)

CS 245 Notes 4 45

Insertion, dense index case

  • Similar
  • Often more expensive...

CS 245 Notes 4 46

Secondary indexes (^) Sequence field 5030 7020 4080 (^10010) 6090

CS 245 Notes 4 47

Secondary indexes (^) Sequence field 5030 7020 4080 (^10010) 6090

  • Sparse index (^3020) 10080 (^90) ...

CS 245 Notes 4 48

Secondary indexes (^) Sequence field 5030 7020 4080 (^10010) 6090

  • Sparse index (^3020) 10080 (^90) ... does not make sense!

CS 245 Notes 4 55

Duplicate values & secondary indexes 1020 4020 4010 4010 4030

(^1010) (^1020) (^2030) (^4040) (^4040) ...

one option...

Problem:excess overhead!

  • disk space• search time

CS 245 Notes 4 56

Duplicate values & secondary indexes 1020 4020 4010 4010 4030

another option... 10

4030

20

CS 245 Notes 4 57

Duplicate values & secondary indexes 1020 4020 4010 4010 4030

another option... 10

4030

Problem:^20

variable sizerecords in

index!

CS 245 Notes 4 58

Duplicate values & secondary indexes 1020 4020 4010 4010 4030

(^1020) (^3040) (^5060) ...

 Another idea (suggested in class):Chain records with same key? 

CS 245 Notes 4 59

Duplicate values & secondary indexes 1020 4020 4010 4010 4030

(^1020) (^3040) (^5060) ...

 Another idea (suggested in class):Chain records with same key?Problems: 

  • Need to add fields to records• Need to follow chain to know records CS 245 Notes 4 60

Duplicate values & secondary indexes 1020 4020 4010 4010 4030

(^1020) (^3040) (^5060) ...

buckets

CS 245 Notes 4 61

Why “bucket” idea is useful

Indexes Records

Name: primary EMP (name,dept,floor,...)

Dept: secondary

Floor: secondary

CS 245 Notes 4 62

Query: Get employees in(Toy Dept)

^ (2nd floor)

Dept. index EMP Floor index Toy 2nd

CS 245 Notes 4 63

Query: Get employees in(Toy Dept)

^ (2nd floor)

Dept. index EMP Floor index Toy 2nd

 Intersect toy bucket and 2nd Floorbucket to get set of matching EMP’s

CS 245 Notes 4 64

This idea used intext information retrieval

Documents

...the cat isfat ... ...was rainingcats and dogs... ...Fido thedog ...

CS 245 Notes 4 65

This idea used in text information retrieval

Documents

...the cat isfat ... ...was rainingcats and dogs... ...Fido thedog ... Inverted lists

cat dog

CS 245 Notes 4 66

IR QUERIES

  • Find articles with “cat” and “dog”
  • Find articles with “cat” or “dog”
  • Find articles with “cat” and not “dog”

CS 245 Notes 4 73

  • Tricks to weigh scores + normalize

e.g.: Match on common word not asuseful as match on rare words...

CS 245 Notes 4 74

  • How to process V.S. Queries?

w1 w2 w3 w4 w5 w6 …

Q = < 0 0 0 1 1 0 … >

CS 245 Notes 4 75

  • Try Stanford Libraries
  • Try Google, Yahoo, ...

CS 245 Notes 4 76

Summary so far

  • Conventional index– Basic Ideas: sparse, dense, multi-level…
    • Duplicate Keys– Deletion/Insertion
    • Secondary indexes– Buckets of Postings List

CS 245 Notes 4 77

Conventional indexes

Advantage:

  • Simple- Index is sequential file

good for scans

Disadvantage:- Inserts expensive, and/or

  • Lose sequentiality & balance CS 245 Notes 4 78

Example Index (sequential)

continuous free space

CS 245 Notes 4 79

Example Index (sequential)

continuous free space

(not sequential)overflow area

CS 245 Notes 4 80

Outline:

  • Conventional indexes
  • B-Trees  NEXT
  • Hashing schemes

CS 245 Notes 4 81

  • NEXT: Another type of index– Give up on sequentiality of index
    • Try to get “balance”

CS 245 Notes 4 82

Root

B+Tree Example n=

100 (^30120150180) (^35113035100101110120130150156179180200)

CS 245 Notes 4 83

Sample non-leaf

to keys< 57 to keys 57  k<81 to keys 81 k<95 to keys 95

CS 245 Notes 4 84

Sample leaf node: From non-leaf node 57 81 95 to next leafin sequence

To recordwith key 57To recordwith key 81To recordwith key 85

CS 245 Notes 4 91

Insert into B+tree

(a) simple case– space available in leaf

(b) leaf overflow

(c) non-leaf overflow

(d) new root

CS 245 Notes 4 92

(a) Insert key = 32 n=

CS 245 Notes 4 93

(a) Insert key = 32 n=

CS 245 Notes 4 94

(a) Insert key = 7 n=

CS 245 Notes 4 95

(a) Insert key = 7 n=

CS 245 Notes 4 96

(a) Insert key = 7 n=

CS 245 Notes 4 97

(c) Insert key = 160 n=

CS 245 Notes 4 98

(c) Insert key = 160 n=

CS 245 Notes 4 99

(c) Insert key = 160 n=

CS 245 Notes 4 100

(c) Insert key = 160 n=

CS 245 Notes 4 101

(d) New root, insert 45 n=

CS 245 Notes 4 102

(d) New root, insert 45 n=

CS 245 Notes 4 109

(c) Redistribute keys– Delete 50

n=

CS 245 Notes 4 110

(d) Non-leaf coalese– Delete 37 n= 25

CS 245 Notes 4 111

(d) Non-leaf coalese– Delete 37 n=

CS 245 Notes 4 112

(d) Non-leaf coalese– Delete 37 n=

CS 245 Notes 4 113

(d) Non-leaf coalese– Delete 37 n=

new root

CS 245 Notes 4 114

B+tree deletions in practice

  • Often, coalescing is not implemented– Too hard and not worth it!

CS 245 Notes 4 115

Comparison: B-trees vs. staticindexed sequential file

Ref #1: Held & Stonebraker

“B-Trees Re-examined”

CACM, Feb. 1978

CS 245 Notes 4 116

Ref # 1 claims:

  • Concurrency control harder in B-Trees
    • B-tree consumes more space

For their comparison:

block = 512 byteskey = pointer = 4 bytes

4 data records per block

CS 245 Notes 4 117

Example: 1 block static index

127 keys (127+1)4 = 512 Bytes-> pointers in index implicit! up to 127 blocks

k1k k

k k k

1 datablock

CS 245 Notes 4 118

Example: 1 block B-tree

63 keys 63x(4+4)+8 = 512 Bytes-> pointers needed in B-tree up to 63 blocks because index isnot contiguous blocks

k1k k63...

k k k

1 datablock

  • next

CS 245 Notes 4 119

Size comparison Ref. #

Static Index B-tree

# datablocks height # datablocks height

2 -> 127128 -> 16,129 23 2 -> 6364 -> 3968 (^23) 16,130 -> 2,048,383 4 3969 -> 250,047250,048 -> 15,752,961 (^45)

CS 245 Notes 4 120

Ref. #1 analysis claims

  • For an 8,000 block file,after 32,000 inserts

after 16,000 lookups

 Static index saves enough accessesto allow for reorganization