cs4604:introduconto#...

106
CS 4604: Introduc0on to Database Management Systems B. Aditya Prakash Lecture #8: Storing Data and Indexes

Upload: others

Post on 03-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

CS  4604:  Introduc0on  to  Database  Management  Systems  

B.  Aditya  Prakash  Lecture  #8:  Storing  Data  and  Indexes  

Page 2: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

STORING  DATA  

Prakash  2015   VT  CS  4604   2  

Page 3: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Prakash  2015   VT  CS  4604  

DBMS  Layers:  

Query Optimization and Execution

Relational Operators

Files and Access Methods

Buffer Management

Disk Space Management

DB

Queries  

TODAY  à  

3  

Page 4: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Prakash  2015   VT  CS  4604  

Leverage  OS  for  disk/file  management?  

§  Layers  of  abstracGon  are  good  …  but:  

4  

Page 5: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Prakash  2015   VT  CS  4604  

Leverage  OS  for  disk/file  management?  

§  Layers  of  abstracGon  are  good  …  but:  – Unfortunately,  OS  oMen  gets  in  the  way  of  DBMS  

5  

Page 6: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Prakash  2015   VT  CS  4604  

Leverage  OS  for  disk/file  management?  

§  DBMS  wants/needs  to  do  things  “its  own  way”  – Specialized  prefetching  – Control  over  buffer  replacement  policy  

•  LRU  not  always  best  (someGmes  worst!!)  – Control  over  thread/process  scheduling  

•  “Convoy  problem”    – Arises  when  OS  scheduling  conflicts  with  DBMS  locking  

– Control  over  flushing  data  to  disk  • WAL  protocol  requires  flushing  log  entries  to  disk  

6  

Page 7: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Prakash  2015   VT  CS  4604  

Disks  and  Files    

§  DBMS  stores  informaGon                                                                                        on  disks.  – but:  disks  are  (relaGvely)  VERY  slow!  

§ Major  implicaGons  for  DBMS  design!  

7  

Page 8: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Prakash  2015   VT  CS  4604  

Disks  and  Files    

§ Major  implicaGons  for  DBMS  design:  – READ:  disk  -­‐>  main  memory  (RAM).  – WRITE:  reverse  – Both  are  high-­‐cost  operaGons,    relaGve  to  in-­‐memory  operaGons,  so  must  be  planned  carefully!  

8  

Page 9: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Prakash  2015   VT  CS  4604  

Why  Not  Store  It  All  in  Main  Memory?  

9  

Page 10: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Prakash  2015   VT  CS  4604  

Why  Not  Store  It  All  in  Main  Memory?  

§  Costs  too  much.    – disk:  ~$1/Gb;  memory:  ~$100/Gb    – High-­‐end  Databases  today  in  the  10-­‐100  TB  range.  

– Approx  60%  of  the  cost  of  a  producGon  system  is  in  the  disks.  

§ Main  memory  is  vola9le.  §  Note:  some  specialized  systems  do  store  enGre  database  in  main  memory.      

10  

Page 11: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Prakash  2015   VT  CS  4604  

The  Storage  Hierarchy  Smaller, Faster

Bigger, Slower

11  

Page 12: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Prakash  2015   VT  CS  4604  

The  Storage  Hierarchy  

– Main memory (RAM) for currently used data.

– Disk for the main database (secondary storage).

– Tapes for archiving older versions of the data (tertiary storage).

Smaller, Faster

Bigger, Slower

Registers

L1 Cache

Main Memory

Magnetic Disk

Magnetic Tape

.  .  .  

12  

Page 13: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Prakash  2015   VT  CS  4604  

Jim  Gray’s  Storage  Latency  Analogy:      How  Far  Away  is  the  Data?  

Registers On Chip Cache On Board Cache

Memory

Disk

1 2

10

100

Tape

10 9

10 6

Boston

This Building

This Room My Head

10 min

1.5 hr

2 Years

1 min

Pluto

2,000 Years

The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again.

The image cannot be displayed. Your computer may not have

The image cannot be displayed. Your computer may not have enough

Andromeda

13  

Page 14: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Prakash  2015   VT  CS  4604  

Disks  §  Secondary  storage  device  of  choice.    § Main  advantage  over  tapes:    random  access  vs.  sequen9al.  

§  Data  is  stored  and  retrieved  in  units  called  disk  blocks  or  pages.  

§  Unlike  RAM,  Gme  to  retrieve  a  disk  page  varies  depending  upon  locaGon  on  disk.      –  relaGve  placement  of  pages  on  disk  is  important!  

14  

Page 15: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Prakash  2015   VT  CS  4604  

Anatomy  of  a  Disk    

Platters

Spindle

•  Sector •  Track •  Cylinder •  Platter •  Block size = multiple of sector size (which is fixed)

Disk head

Arm movement

Arm assembly

Tracks

Sector

#15  

Page 16: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Prakash  2015   VT  CS  4604  

Accessing  a  Disk  Page  

§  Time  to  access  (read/write)  a  disk  block:  –  .  –  .  –  .  

16  

Page 17: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Prakash  2015   VT  CS  4604  

Accessing  a  Disk  Page  

§  Time  to  access  (read/write)  a  disk  block:  – seek  9me:  moving  arms  to  posiGon  disk  head  on  track  

–  rota9onal  delay:  waiGng  for  block  to  rotate  under  head  

–  transfer  9me:  actually  moving  data  to/from  disk  surface  

17  

Page 18: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Prakash  2015   VT  CS  4604  

Accessing  a  Disk  Page  

§  RelaGve  Gmes?  – seek  9me:    –  rota9onal  delay:    –  transfer  9me:  

18  

Page 19: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Prakash  2015   VT  CS  4604  

Accessing  a  Disk  Page  

§  RelaGve  Gmes?  – seek  9me:  about  1  to  20msec    –  rota9onal  delay:  0  to  10msec    –  transfer  9me:  <  1msec  per  4KB  page  

Transfer

Seek

Rotate

transfer  

19  

Page 20: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Prakash  2015   VT  CS  4604  

Seek  0me  &  rota0onal  delay  dominate  

§  Key  to  lower  I/O  cost:                                                                                                                  reduce  seek/rotaGon  delays!                                                                                              

§  Also  note:  For  shared  disks,  much  Gme  spent  waiGng  in  queue  for  access  to  arm/controller  

Seek

Rotate

transfer  

20  

Page 21: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Prakash  2015   VT  CS  4604  

Arranging  Pages  on  Disk  

§  “Next” block  concept:      – blocks  on  same  track,  followed  by  – blocks  on  same  cylinder,  followed  by  – blocks  on  adjacent  cylinder  

§  Accesing  ‘next’  block  is  cheap  §  A  useful  opGmizaGon:  pre-­‐fetching  

– See  textbook  page  323  

21  

Page 22: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Prakash  2015   VT  CS  4604  

Rules  of  thumb…  

1. Memory  access  much  faster  than  disk  I/O                (~  1000x)  

§  “SequenGal”  I/O  faster  than  “random”  I/O                  (~  10x)  

22  

Page 23: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Prakash  2015   VT  CS  4604  

Disk  Arrays:  RAID  

§  Benefits:  – Higher  throughput  (via  data  “striping”)  – Longer  MTTF  (via  redundancy)  

Logical Physical

Just  FYI  

23  

Page 24: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Prakash  2015   VT  CS  4604  

Recall:  DBMS  Layers  

Query Optimization and Execution

Relational Operators

Files and Access Methods

Buffer Management

Disk Space Management

DB

Queries  

TODAY  à  

24  

Page 25: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Prakash  2015   VT  CS  4604  

Buffer  Management  in  a  DBMS  

DB  

MAIN  MEMORY  

DISK  

(copy  of  a)  disk  page  

free  frame  

Page  Requests  from  Higher  Levels  

buffer  pool  

choice  of  frame  dictated  by  replacement  policy  

Just  FYI  

25  

Page 26: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Prakash  2015   VT  CS  4604  

Files  

§  FILE:  A  collecGon  of  pages,  each  containing  a  collecGon  of  records.    

§  Must  support:  –  insert/delete/modify  record  –  read  a  parGcular  record  (specified  using  record  id)  –  scan  all  records  (possibly  with  some  condiGons  on  the  records  to  be  retrieved)  

26  

Page 27: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Prakash  2015   VT  CS  4604  

Alterna0ve  File  Organiza0ons  

Several  alternaGves  (w/  trade-­‐offs):  – Heap  files:    Suitable  when  typical  access  is  a  file  scan  retrieving  all  records.  

– Sorted  Files:  – Index  File  OrganizaGons:  

27  

Page 28: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Prakash  2015   VT  CS  4604  

Variable  length  records  

§  SLOTTED  PAGE  organizaGon  -­‐  popular.  

...  

page  header  

occupied  records  

28  

Page 29: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Prakash  2015   VT  CS  4604  

Conclusions-­‐-­‐-­‐Storing  

§ Memory  hierarchy  §  Disks:  (>1000x  slower)  -­‐  thus  

– pack  info  in  blocks  –  try  to  fetch  nearby  blocks  (sequenGally)  

§  Record  organizaGon:  Slosed  page  

29  

Page 30: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

TREE  INDEXES  

Prakash  2015   VT  CS  4604   30  

Page 31: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Declaring  Indexes  

§  No  standard!  §  Typical  syntax:  CREATE INDEX StudentsInd ON Students(ID);

CREATE INDEX CoursesInd ON Courses(Number, DeptName);

Prakash  2015   VT  CS  4604   31  

Page 32: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Types  of  Indexes  

§  Primary:  index  on  a  key  – Used  to  enforce  constraints  

§  Secondary:  index  on  non-­‐key  asribute  §  Clustering:  order  of  the  rows  in  the  data  pages  correspond  to  the  order  of  the  rows  in  the  index  – Only  one  clustered  index  can  exist  in  a  given  table  – Useful  for  range  predicates  

§  Non-­‐clustering:  physical  order  not  the  same  as  index  order  

Prakash  2015   VT  CS  4604   32  

Page 33: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Using  Indexes  (1):  Equality  Searches  

§  Given  a  value  v,  the  index  takes  us  to  only  those  tuples  that  have  v    in  the  asribute(s)  of  the  index.  

§  E.g.    (use  CourseInd  index)    SELECT Enrollment FROM Courses WHERE Number = “4604” and DeptName = “CS”

Prakash  2015   VT  CS  4604   33  

Page 34: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Using  Indexes  (1):  Equality  Searches  

§  Given  a  value  v,  the  index  takes  us  to  only  those  tuples  that  have  v    in  the  asribute(s)  of  the  index.  

§  Can  use  Hashes,  but  see  next

Prakash  2015   VT  CS  4604   34  

Page 35: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Using  Indexes  (2):  Range  Searches  

§  ``Find  all  students  with  gpa  >  3.0’’  §  may  be  slow,  even  on  sorted  file  §  Hashes  not  a  good  idea!    § What  to  do?  

Prakash  2015   VT  CS  4604  

Page 1 Page 2 Page N Page 3 Data File

35  

Page 36: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Range  Searches  

§  ``Find  all  students  with  gpa  >  3.0’’  §  may  be  slow,  even  on  sorted  file  §  SoluGon:    Create  an  `index’  file.  

Prakash  2015   VT  CS  4604  

Page 1 Page 2 Page N Page 3 Data File

k2 kN k1 Index File

36  

Page 37: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Range  Searches  

§ More  details:  §  if  index  file  is  small,  do  binary  search  there  §  Otherwise??  

Prakash  2015   VT  CS  4604  

Page 1 Page 2 Page N Page 3 Data File

k2 kN k1 Index File

37  

Page 38: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

B-­‐trees  

§  the  most  successful  family  of  index  schemes  (B-­‐trees,  B+-­‐trees,  B*-­‐trees)  

§  Can  be  used  for  primary/secondary,  clustering/non-­‐clustering  index.  

§  balanced  “n-­‐way”  search  trees  §  Original  Paper:  Rudolf  Bayer  and  McCreight,  E.  M.  OrganizaGon  and  Maintenance  of  Large  Ordered  Indexes.  Acta  InformaGca  1,  173-­‐189,  1972.  

Prakash  2015   VT  CS  4604   38  

Page 39: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

B-­‐trees  

§  Eg.,  B-­‐tree  of  order  d=1:  

Prakash  2015   VT  CS  4604  

1   3  

6  

7  

9  

13  

<6  

>6   <9  >9  

39  

Page 40: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

B  -­‐  tree  proper0es:  

§  each  node,  in  a  B-­‐tree  of  order  d:  – Key  order  – at  most  n=2d  keys  – at  least  d  keys  (except  root,  which  may  have  just  1  key)  

– all  leaves  at  the  same  level  –  if  number  of  pointers  is  k,  then  node  has  exactly  k-­‐1  keys  

–  (leaves  are  empty)  

Prakash  2015   VT  CS  4604  

v1   v2   …   vn-­‐1  

p1   pn  

40  

Page 41: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Proper0es  

§  “block  aware”  nodes:  each  node  is  a  disk  page  §  O(log  (N))  for  everything!  (ins/del/search)  §  typically,  if  d  =  50  -­‐  100,  then  2  -­‐  3  levels  §  uGlizaGon  >=  50%,  guaranteed;  on  average  69%  

Prakash  2015   VT  CS  4604   41  

Page 42: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Queries  

§  Algo  for  exact  match  query?  (eg.,  ssn=8?)  

Prakash  2015   VT  CS  4604  

1   3  

6  

7  

9  

13  

<6  

>6   <9  >9  

42  

Page 43: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

JAVA  anima0on  

§  hsp://slady.net/java/bt/  

Prakash  2015   VT  CS  4604   43  

Page 44: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Queries  

§  Algo  for  exact  match  query?  (eg.,  ssn=8?)  

Prakash  2015   VT  CS  4604  

1   3  

6  

7  

9  

13  

<6  

>6   <9  >9  

44  

Page 45: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Queries  

§  Algo  for  exact  match  query?  (eg.,  ssn=8?)  

Prakash  2015   VT  CS  4604  

1   3  

6  

7  

9  

13  

<6  

>6   <9  >9  

45  

Page 46: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Queries  

§  Algo  for  exact  match  query?  (eg.,  ssn=8?)  

Prakash  2015   VT  CS  4604  

1   3  

6  

7  

9  

13  

<6  

>6   <9  >9  

46  

Page 47: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Queries  

§  Algo  for  exact  match  query?  (eg.,  ssn=8?)  

Prakash  2015   VT  CS  4604  

1   3  

6  

7  

9  

13  

<6  

>6   <9  >9  

H  steps  (=  disk  accesses)  

47  

Page 48: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Queries  

§  what  about  range  queries?  (eg.,  5<salary<8)  §  Proximity/  nearest  neighbor  searches?  (eg.,  salary  ~  8  )  

Prakash  2015   VT  CS  4604   48  

Page 49: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Queries  

§  what  about  range  queries?  (eg.,  5<salary<8)  §  Proximity/  nearest  neighbor  searches?  (eg.,  salary  ~  8  )  

Prakash  2015   VT  CS  4604  

1   3  

6  

7  

9  

13  

<6  

>6   <9  >9  

49  

Page 50: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Queries  

§  what  about  range  queries?  (eg.,  5<salary<8)  §  Proximity/  nearest  neighbor  searches?  (eg.,  salary  ~  8  )  

Prakash  2015   VT  CS  4604  

1   3  

6  

7  

9  

13  

<6  

>6   <9  >9  

50  

Page 51: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Queries  

§  what  about  range  queries?  (eg.,  5<salary<8)  §  Proximity/  nearest  neighbor  searches?  (eg.,  salary  ~  8  )  

Prakash  2015   VT  CS  4604  

1   3  

6  

7  

9  

13  

<6  

>6   <9  >9  

51  

Page 52: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Queries  

§  what  about  range  queries?  (eg.,  5<salary<8)  §  Proximity/  nearest  neighbor  searches?  (eg.,  salary  ~  8  )  

Prakash  2015   VT  CS  4604  

1   3  

6  

7  

9  

13  

<6  

>6   <9  >9  

52  

Page 53: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Varia0ons  

§  How  could  we  do  even  beser  than  the  B-­‐trees  above?  

Prakash  2015   VT  CS  4604   53  

Page 54: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

B+  trees  -­‐  Mo0va0on  

§  B-­‐tree  –  print  keys  in  sorted  order:  

Prakash  2015   VT  CS  4604  

1   3  

6  

7  

9  

13  

<6  

>6   <9  >9  

54  

Page 55: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

B+  trees  -­‐  Mo0va0on  

§  B-­‐tree  needs  back-­‐tracking  –  how  to  avoid  it?  

Prakash  2015   VT  CS  4604  

1   3  

6  

7  

9  

13  

<6  

>6   <9  >9  

55  

Page 56: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

B+  trees  -­‐  Mo0va0on  

§  Stronger  reason:  for  clustering  index,  data  records  are  scasered:  

Prakash  2015   VT  CS  4604  

1   3  

6  

7  

9  

13  

<6  

>6   <9  >9  

56  

Page 57: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Solu0on:  B+  -­‐  trees    

§  facilitate  sequenGal  ops  §  They  string  all  leaf  nodes  together    §  AND  §  replicate  keys  from  non-­‐leaf  nodes,  to  make  sure  every  key  appears  at  the  leaf  level  

§  (vital,  for  clustering  index!)  

Prakash  2015   VT  CS  4604   57  

Page 58: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

B+  trees  

Prakash  2015   VT  CS  4604  

1   3  

6  

6  

9  

9  

<6  

>=6   <9  >=9  

7   13  

58  

Page 59: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

B+  trees  

Prakash  2015   VT  CS  4604  

1   3  

6  

6  

9  

9  

<6  

>=6   <9  >=9  

7   13  

Index  Pages  

Data  Pages  

59  

Page 60: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

B+  trees  

§ More  details:  next  (and  textbook)  §  In  short:  on  split  

– at  leaf  level:  COPY  middle  key  upstairs  – at  non-­‐leaf  level:  push  middle  key  upstairs  (as  in  plain  B-­‐tree)  

Prakash  2015   VT  CS  4604   60  

Page 61: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Example  B+  Tree  

§  Search  begins  at  root,  and  key  comparisons  direct  it  to  a  leaf    

§  Search  for  5*,  15*,  all  data  entries  >=  24*  ...  

Prakash  2015   VT  CS  4604  

Based on the search for 15*, we know it is not in the tree!

Root

17 24 30

2* 3* 5* 7* 14* 16* 19* 20* 22* 24* 27* 29* 33* 34* 38* 39*

13

61  

Page 62: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Inser0ng  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.  

§  parent  node  may  overflow  – but  then:  push  up  middle  key.  Splits  “grow”  tree;  root  split  increases  height.      

Prakash  2015   VT  CS  4604   62  

Page 63: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Example  B+  Tree  –  Inser0ng  30*  

Prakash  2015   VT  CS  4604  

Root

17 24

2* 3* 5* 7* 14* 16* 19* 20* 22* 24* 27* 29*

13

23*

63  

Page 64: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Example  B+  Tree  –  Inser0ng  30*  

Prakash  2015   VT  CS  4604  

Root

17 24

2* 3* 5* 7* 14* 16* 19* 20* 22* 24* 27* 29*

13

23* 30*  

64  

Page 65: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Example  B+  Tree  -­‐  Inser0ng  8*  

Prakash  2015   VT  CS  4604  

Root

17 24

2* 3* 5* 7* 14* 16* 19* 20* 22* 24* 27* 29*

13

23*

65  

Page 66: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Example  B+  Tree  -­‐  Inser0ng  8*  

Prakash  2015   VT  CS  4604  

Root

17 24

2* 3* 5* 7* 14* 16* 19* 20* 22* 24* 27* 29*

13

23*

No  Space  

66  

Page 67: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Prakash  2015   VT  CS  4604  

Example  B+  Tree  -­‐  Inser0ng  8*  Root

17 24

2* 3* 5* 7* 14* 16* 19* 20* 22* 24* 27* 29*

13

23*

2* 3* 5* 14* 16* 19* 20* 22* 24* 27* 29* 23* 7* 8*

13 17 24

5*

So  Split!  

67  

Page 68: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Prakash  2015   VT  CS  4604  

Example  B+  Tree  -­‐  Inser0ng  8*  Root

17 24

2* 3* 5* 7* 14* 16* 19* 20* 22* 24* 27* 29*

13

23*

2* 3* 5* 14* 16* 19* 20* 22* 24* 27* 29* 23* 7* 8*

13 17 24

5*

So  Split!  

And  then  push  middle  UP  

68  

Page 69: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Prakash  2015   VT  CS  4604  

Example  B+  Tree  -­‐  Inser0ng  8*  Root

17 24

2* 3* 5* 7* 14* 16* 19* 20* 22* 24* 27* 29*

13

23*

2* 3* 14* 16* 19* 20* 22* 24* 27* 29* 23* 7* 8*

5   13 17 24

5*

<5   >=5  

Final  State  

69  

Page 70: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Example  B+  Tree  -­‐  Inser0ng  21*  

Prakash  2015   VT  CS  4604  

2* 3*

Root

5

14* 16* 19* 20* 22* 24* 27* 29* 7* 5* 8*

13 17 24

23*

2* 3* 14* 16* 19* 20* 22* 24* 27* 29* 7* 5* 8* 23*

70  

Page 71: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Example  B+  Tree  -­‐  Inser0ng  21*  

Prakash  2015   VT  CS  4604  

2* 3*

Root

5

14* 16* 19* 20* 22* 24* 27* 29* 7* 5* 8*

13 17 24

23*

2* 3* 14* 16* 19* 20* 24* 27* 29* 7* 5* 8* 21* 22* 23*

17 21 24 13 5 Root  is  Full,  so  split  recursively  

71  

Page 72: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Example  B+  Tree:  Recursive  split  

Prakash  2015   VT  CS  4604  

•  Notice that root was also split, increasing height.

2* 3*

Root

17

21 24

14* 16* 19* 20* 21* 22* 23* 24* 27* 29*

13 5

7* 5* 8*

72  

Page 73: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Prakash  2015   VT  CS  4604  

Example:  Data  vs.  Index  Page  Split  

§  leaf:  ‘copy’  §  non-­‐leaf:  ‘push’  

§  why  not  ‘copy’  @  non-­‐leaves?  

2* 3* 5* 7* 8*

5

5 21 24

17

13

… 2* 3* 5* 7*

17 21 24 13

Data Page Split

Index Page Split

8*

5

#73  

Page 74: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Same  Inser0ng  21*:  The  Deferred  Split  

Prakash  2015   VT  CS  4604  

2* 3*

Root

5

14* 16* 19* 20* 22* 24* 27* 29* 7* 5* 8*

13 17 24

23*

Note  this  has  free  space.  So…  

74  

Page 75: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Inser0ng  21*:  The  Deferred  Split  

Prakash  2015   VT  CS  4604  

2* 3*

Root

5

14* 16* 19* 20* 22* 24* 27* 29* 7* 5* 8*

13 17 24

23*

LEND  keys  to  sibling,  through  PARENT!  

2* 3*

Root

5

14* 16* 19* 20* 21* 23* 24* 27* 7* 5* 8*

13 17 23

22* 29*

75  

Page 76: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Inser0ng  21*:  The  Deferred  Split  

Prakash  2015   VT  CS  4604  

2* 3*

Root

5

14* 16* 19* 20* 22* 24* 27* 29* 7* 5* 8*

13 17 24

23*

Shorter,  more  packed,  faster  tree  

2* 3*

Root

5

14* 16* 19* 20* 21* 23* 24* 27* 7* 5* 8*

13 17 23

22* 29*

76  

Page 77: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Inser0on  examples  for  you  to  try  

Prakash  2015   VT  CS  4604  

2* 3*

Root

30

14* 16* 21* 22* 23*

13 5

7* 5* 8*

20 … (not shown)

11*

Insert the following data entries (in order): 28*, 6*, 25*

77  

Page 78: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Answer…  

Prakash  2015   VT  CS  4604  

2* 3*

30

7* 8* 14* 16*

7 5

6* 5*

13 …

After inserting 28*, 6*

After inserting 25*

21* 22* 23* 28*

20

11*

78  

Page 79: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Answer…  

Prakash  2015   VT  CS  4604  

2* 3*

13

20 23

7* 8* 14* 16* 21* 22* 23* 25* 28*

7 5

6* 5*

30

11*

After inserting 25*

79  

Page 80: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Dele0ng  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  underflows  

•  Try  to  re-­‐distribute,  borrowing  from  sibling  (adjacent  node  with  same  parent  as  L).  

•  If  re-­‐distribuGon  fails,  merge  L  and  sibling.  –  update  parent  –  and  possibly  merge,  recursively  

Prakash  2015   VT  CS  4604   80  

Page 81: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Dele0on  from  B+Tree  

Prakash  2015   VT  CS  4604  

2* 3*

Root

17

21 24

14* 16* 19* 20* 21* 22* 23* 24* 27* 29*

13 5

7* 5* 8*

81  

Page 82: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Prakash  2015   VT  CS  4604  

Example:  Delete  19*  &  20*  

DeleGng  19*  is  easy:  

2* 3*

Root 17

24 30

14* 16* 19* 20* 22* 24* 27* 29* 33* 34* 38* 39*

13 5

7* 5* 8*

2* 3*

Root 17

30

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

13 5

7* 5* 8* 22* 24*

27

27* 29*

20* 22*

•  DeleGng  20*  -­‐>  re-­‐distribuGon  (noGce:  27  copied  up)  

1 2

3

82  

Page 83: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Prakash  2015   VT  CS  4604  

2* 3*

Root 17

30

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

13 5

7* 5* 8* 22* 24*

27

27* 29*

               ...  And  Then  Dele0ng  24*  

2* 3*

Root 17

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

13 5

7* 5* 8* 22* 27*

30

29*

•  Must  merge  leaves:  OPPOSITE  of  insert  

3

4

83  

Page 84: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Prakash  2015   VT  CS  4604  

2* 3*

Root 17

30

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

13 5

7* 5* 8* 22* 24*

27

27* 29*

               ...  And  Then  Dele0ng  24*  

2* 3*

Root 17

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

13 5

7* 5* 8* 22* 27*

30

29*

•  Must  merge  leaves:  OPPOSITE  of  insert  

…  but  are  we  done??  

3

4

84  

Page 85: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

 ...  Merge  Non-­‐Leaf  Nodes,  Shrink  Tree  

Prakash  2015   VT  CS  4604  

2* 3*

Root 17

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

13 5

7* 5* 8* 22* 27*

30

29*

4

2* 3* 7* 14* 16* 22* 27* 29* 33* 34* 38* 39* 5* 8*

Root 30 13 5 17

5

85  

Page 86: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Example  of  Non-­‐leaf  Re-­‐distribu0on  

§  Tree  is  shown  below  during  deleGon  of  24*.    §  Now,  we  can  re-­‐distribute  keys  

Prakash  2015   VT  CS  4604  

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*

86  

Page 87: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Aoer  Re-­‐distribu0on  

§  need  only  re-­‐distribute  ‘20’;  did  ‘17’,  too  §  why  would  we  want  to  re-­‐distribute  more  keys?  

Prakash  2015   VT  CS  4604  

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

87  

Page 88: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Main  observa0ons  for  dele0on  

§  If  a  key  value  appears  twice  (leaf  +  nonleaf),  the  above  algorithms  delete  it  from  the  leaf,  only    

§  why  not  non-­‐leaf,  too?  

Prakash  2015   VT  CS  4604   88  

Page 89: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Main  observa0ons  for  dele0on  

§  If  a  key  value  appears  twice  (leaf  +  nonleaf),  the  above  algorithms  delete  it  from  the  leaf,  only    

§  why  not  non-­‐leaf,  too?  §  ‘lazy  deleGons’  -­‐  in  fact,  some  vendors  just  mark  entries  as  deleted  (~  underflow),  – and  reorganize/compact  later  

Prakash  2015   VT  CS  4604   89  

Page 90: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Recap:  main  ideas  

§  on  overflow,  split  (and  ‘push’,  or  ‘copy’)  – or  consider  deferred  split  

§  on  underflow,  borrow  keys;  or  merge  – or  let  it  underflow...  

Prakash  2015   VT  CS  4604   90  

Page 91: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

B+  Trees  in  Prac0ce  

§  Typical  order:  100.    Typical  fill-­‐factor:  67%.  – average  fanout  =  2*100*0.67  =  134  

§  Typical  capaciGes:  – Height  4:  1334  =  312,900,721  entries  – Height  3:  1333  =        2,406,104  entries  

Prakash  2015   VT  CS  4604   91  

Page 92: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

B+  Trees  in  Prac0ce  

§  Can  oMen  keep  top  levels  in  buffer  pool:  – Level  1  =                      1  page  =          8  KB  – Level  2  =            134  pages  =          1  MB  – Level  3  =    17,956  pages  =  140  MB  

Prakash  2015   VT  CS  4604   92  

Page 93: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

B+  trees  with  duplicates  

§  Everything  so  far:  assumed  unique  key  values  §  How  to  extend  B+-­‐trees  for  duplicates?  

– Alt.  2:  <key,  rid>  – Alt.  3:  <key,  {rid  list}>  

§  2  approaches,  roughly  equivalent  

Prakash  2015   VT  CS  4604   93  

Page 94: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

B+  trees  with  duplicates  

§  approach#1:  repeat  the  key  values,  and  extend  B+  tree  algo’s  appropriately  -­‐  eg.  many  ‘14’s  

Prakash  2015   VT  CS  4604  

14 24

2* 3* 5* 7* 13* 14* 14* 14* 22* 24* 27* 29*

13

23* 14* 14*

94  

Page 95: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

B+  trees  with  duplicates  

§  approach#1:  subtle  problem  with  deleGon:  §  treat  rid  as  part  of  the  key,  thus  making  it  unique  

Prakash  2015   VT  CS  4604  

14 24

2* 3* 5* 7* 13* 14* 14* 14* 22* 24* 27* 29*

13

23* 14* 14*

95  

Page 96: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

B+  trees  with  duplicates  

§  approach#2:  store  each  key  value:  once  §  but  store  the  {rid  list}  as  variable-­‐length  field  (and  use  overflow  pages,  if  needed)  

Prakash  2015   VT  CS  4604  

14 24

2* 3* 5* 7* 13* 14* 22* 24* 27* 29*

13

23* {rid list}  

{rid list, cont’d}  96  

Page 97: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

B+trees  in  Prac0ce    

§  prefix  compression;    §  bulk-­‐loading;    §  ‘order’  

Prakash  2015   VT  CS  4604   97  

Page 98: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Prefix  Key  Compression  

§  Important  to  increase  fan-­‐out.    (Why?)  §  Key  values  in  index  entries  only  `direct  traffic’;  can  oMen  compress  them.  

Prakash  2015   VT  CS  4604  

Papadopoulos Pernikovskaya

98  

Page 99: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Prefix  Key  Compression  

§  Important  to  increase  fan-­‐out.    (Why?)  §  Key  values  in  index  entries  only  `direct  traffic’;  can  oMen  compress  them.  

Prakash  2015   VT  CS  4604  

Pap Per <room for more separators/keys>  

99  

Page 100: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Bulk  Loading  of  a  B+  Tree  

§  In  an  empty  tree,  insert  many  keys  § Why  not  one-­‐at-­‐a-­‐Gme?  

– Too  slow!  

Prakash  2015   VT  CS  4604   100  

Page 101: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Bulk  Loading  of  a  B+  Tree  

§  IniGalizaGon:    Sort  all  data  entries  §  scan  list;  whenever  enough  for  a  page,  pack  §  <repeat  for  upper  level>  

Prakash  2015   VT  CS  4604  

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

101  

Page 102: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Prakash  2015   VT  CS  4604  

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 35 23 12 6

10 20

3* 4* 6* 9* 10* 11* 12* 13* 20* 22* 23* 31* 35* 36* 38* 41* 44*

6

Root

10

12 23

20

35

38

not yet in B+ tree Data entry pages

Bulk  Loading  of  a  B+  Tree  

Page 103: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

A  Note  on  `Order’  

§  Order  (d)  concept  replaced  by  physical  space  criterion  in  pracGce  (`at  least  half-­‐full’).  

§  Why  do  we  need  it?  –  Index  pages  can  typically  hold  many  more  entries  than  leaf  pages.  

–  Variable  sized  records  and  search  keys  mean  different  nodes  will  contain  different  numbers  of  entries.  

–  Even  with  fixed  length  fields,  mulGple  records  with  the  same  search  key  value  (duplicates)  can  lead  to  variable-­‐sized  data  entries  (if  we  use  AlternaGve  (3)).  

Prakash  2015   VT  CS  4604   103  

Page 104: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

A  Note  on  `Order’  

§ Many  real  systems  are  even  sloppier  than  this:  they  allow  underflow,  and  only  reclaim  space  when  a  page  is  completely  empty.  

§  (what  are  the  benefits  of  such  ‘slopiness’?)  

Prakash  2015   VT  CS  4604   104  

Page 105: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Conclusions  

§  B+tree  is  the  prevailing  indexing  method    §  Excellent,  O(logN)  worst-­‐case  performance  for  ins/del/search;  (~3-­‐4  disk  accesses  in  pracGce)  

§  guaranteed  50%  space  uGlizaGon;  avg  69%  

Prakash  2015   VT  CS  4604   105  

Page 106: CS4604:Introduconto# Database#Management#Systems#courses.cs.vt.edu/~cs4604/Spring15/lectures/lecture-8.pdf · file’scan’retrieving’all’records.’ – Sorted’Files:’

Conclusions  

§  Can  be  used  for  any  type  of  index:  primary/secondary,  sparse  (clustering),  or  dense  (non-­‐clustering)  

§  Several  fine-­‐extensions  on  the  basic  algorithm  – deferred  split;  prefix  compression;  (underflows)  – bulk-­‐loading  – duplicate  handling  

Prakash  2015   VT  CS  4604   106