B-trees

COMP9315 23T1 ♢ B-trees ♢ [0/15]
❖ B-Trees

B-trees are multi-way search trees with the properties:

B-tree insertion and deletion methods Advantages of B-trees over general multi-way search trees:
COMP9315 23T1 ♢ B-trees ♢ [1/15]
❖ B-Trees (cont)

Example B-tree (depth=3, n=3)   (actually B+ tree)

[Diagram:Pics/file-struct/btree0.png]

(Note: in DBs, nodes are pages ⇒ large branching factor, e.g. n=500)

COMP9315 23T1 ♢ B-trees ♢ [2/15]
❖ B-Tree Depth

Depth depends on effective branching factor  (i.e. how full nodes are).

Simulation studies show typical B-tree nodes are 69% full.

Gives   load Li = 0.69 × ci   and   depth of tree ~ ceil( logLi r ).

Example: ci=128,    Li=88

Level #nodes #keys
root 1 88
1 89 7832
2 7921 697048
3 704969 62037272

Note: ci is generally larger than 128 for a real B-tree.
COMP9315 23T1 ♢ B-trees ♢ [3/15]
❖ Selection with B-Trees

For one queries:

Node find(k,tree) {
   return search(k, root_of(tree))
}
Node search(k, node) {
   // get the page of the node
   if (is_leaf(node)) return node
   keys = array of nk key values in node
   pages = array of nk+1 ptrs to child nodes
   if (k <= keys[0])
      return search(k, pages[0])
   else if (keys[i] < k <= keys[i+1])
      return search(k, pages[i+1])
   else if (k > keys[nk-1])
      return search(k, pages[nk])
}

COMP9315 23T1 ♢ B-trees ♢ [4/15]
❖ Selection with B-Trees (cont)

Simplified description of search ...

N = B-tree root node
while (N is not a leaf node) N = scanToFindChild(N,K)
tid = scanToFindEntry(N,K)
access tuple T using tid

Costone  =  (D + 1)r

[Diagram:Pics/file-struct/btree00.png]

COMP9315 23T1 ♢ B-trees ♢ [5/15]
❖ Selection with B-Trees (cont)

For range queries (assume sorted on index attribute):

search index to find leaf node for Lo
for each leaf node entry until Hi found
    add pageOf(tid) to Pages to be scanned
scan Pages looking for matching tuples

Costrange  =  (D + bi + bq)r

[Diagram:Pics/file-struct/btree00.png]

COMP9315 23T1 ♢ B-trees ♢ [6/15]
❖ Insertion into B-Trees

Overview of the method:

  1. find leaf node and position in node where new key belongs
  2. if node is not full, insert entry into appropriate position
  3. if node is full ...
    • promote middle element to parent
    • split node into two half-full nodes   (< middle, > middle)
    • insert new key into appropriate half-full node
  4. if parent full, split and promote upwards
  5. if reach root, and root is full, make new root upwards

Note: if duplicates not allowed and key exists, may stop after step 1.
COMP9315 23T1 ♢ B-trees ♢ [7/15]
❖ Example: B-tree Insertion

Starting from this tree:

[Diagram:Pics/file-struct/btree1.png]

insert the following keys in the given order   12 15 30 10

COMP9315 23T1 ♢ B-trees ♢ [8/15]
❖ Example: B-tree Insertion (cont)


[Diagram:Pics/file-struct/btree1a.png]

COMP9315 23T1 ♢ B-trees ♢ [9/15]
❖ Example: B-tree Insertion (cont)

[Diagram:Pics/file-struct/btree1b.png]

COMP9315 23T1 ♢ B-trees ♢ [10/15]
❖ B-Tree Insertion Cost

Insertion cost = CosttreeSearch + CosttreeInsert + CostdataInsert

Best case: write one page (most of time)

Costinsert  =  Dr + 1w + 1r + 1w

Common case: 3 node writes (rearrange 2 leaves + parent)

Costinsert  =  Dr + 3w + 1r + 1w
COMP9315 23T1 ♢ B-trees ♢ [11/15]
❖ B-Tree Insertion Cost (cont)

Worst case: propagate to root    Costinsert  =  Dr + D.3w + 1r + 1w

[Diagram:Pics/file-struct/btree1c.png]

COMP9315 23T1 ♢ B-trees ♢ [12/15]
❖ B-trees in PostgreSQL

PostgreSQL implements ≅ Lehman/Yao-style B-trees

B-tree implementation: backend/access/nbtree Notes:
COMP9315 23T1 ♢ B-trees ♢ [13/15]
❖ B-trees in PostgreSQL (cont)

Changes for PostgreSQL v12

To explore indexes in more detail:
COMP9315 23T1 ♢ B-trees ♢ [14/15]
❖ B-trees in PostgreSQL (cont)

Interface functions for B-trees

// build Btree index on relation
Datum btbuild(rel,index,...)
// insert index entry into Btree
Datum btinsert(rel,key,tupleid,index,...)
// start scan on Btree index
Datum btbeginscan(rel,key,scandesc,...)
// get next tuple in a scan
Datum btgettuple(scandesc,scandir,...)
// close down a scan
Datum btendscan(scandesc)

COMP9315 23T1 ♢ B-trees ♢ [15/15]


Produced: 15 Mar 2023