Professional Documents
Culture Documents
Physical Design Notes
Physical Design Notes
Chen’s material and Fundamentals of Database Systems, Elmasri, Navathe, 7th edition
PHYSICAL DESIGN
Introduction
Storage hierarchy:
Primary storage: includes storage media that can be operated on directly by
the computer CPU, comes from RAM
Secondary storage – hard-disk drives
Tertiary storage - optical disks and tape
Records
Contain fields which have values of a particular type (i.e., rows of a table)
Stored in disk blocks
Example:
Assume # of records: r = 30,000 records in the EMPLOYEE file
Each record size: s = 150 bytes Block size: B = 512 bytes
bfr = ? bfrf = floor(B/s) = floor(512/150)= 3 records/block
# of blocks needed to store the file? # of file blocks: fb = ceiling(r/bfrf)= ceiling(30000/3)= 10,000 blocks
Average linear search: how many block accesses? fb/2 = 10000/2 = 5000
Average binary search: how many block accesses? ceiling(log2fb) = ceiling(log210000) = 14
Page
Page Header Row 1 Row 2
Row 2 Row 3
Row 3 Row 4
Row 5 Free Space
Free Space
Free Space
Row Header – Row Data with Offsets
Identification information
Internal row ID
Schema information
Row data length
Column count (maximum relative column with valid not NULL
data values)
Row Data
Row Header Value Offsets Column 1 Column 2 Column 3
Column 4 Column 5
Column 5 Column 6
Column 8
Row Header - Column Offset Array
it
does not know about the nature of your data
better than you!
Common indexes
single-level indexes: Primary Index, Clustering Index, Secondary Index
multileveled indexes: B-tree, B+-tree, etc.
Single-Level Indexes
Types
Primary Index: on a key field (field values ordered, distinct)
one index entry for each record block (block anchor –first record’s key)
Clustering Index: on ordered non-key field (field values ordered,
duplicate)
one index entry for each distinct value of the field: the first block that
contains records with that field value.
Secondary Index: on non-ordered field (field values non-ordered,,
distinct / duplicate))
used when some other primary access already existed
Primary Index
on the Key Field,
(ordered, distinct)
A Clustering
Index Example:
non-key, (ordered,
duplicate)
Index field
(Not ordered, distinct value)
Index file:
<ordered value, Pointer>
Index file:
<ordered value, Pointer>
Pointer
For distinct: to the record (dense index)
For duplicated: to the block that containing record pointers (sparse index)
Efficiency: # of block accesses
Assume a file with # of records r = 30,000 with record size s = 150 bytes and block
size B = 512 bytes
Multi-level indexes!
We can repeat the process, creating a third, fourth, ..., top level until all
entries of the top level fit in one disk block
Such a multi-level index is a form of search tree
However, insertion and deletion of new index entries is a problem because
every level of the index is an ordered file.
A Two-Level
Primary Index
Dynamic multi-level index using B trees
Dynamic multi-level index:
B-tree & B+-tree
• B tree
– Is a generalization of 2-3 trees
– All leaves are at the same depth balanced tree: of order M
– Root: either a leaf or has between 2 and M children (1 and M-1 keys)
– Other node is kept between half-full and completely full
• # of tree pointers: between M/2 and M
• # of key values: between M/2 -1 and M -1 (always 1 fewer than # of tree
pointers in its same node)
– Each node corresponds to a disk block
• Big O: search using B tree vs. AVL?
B+ tree properties
• Root: if tree has <=L items, root is a leaf, otherwise between 2 and M
children
• Leaf node order L or Pleaf (all leaves have the same depth)
– # of data pointers: Pleaf/2 ~ Pleaf
– # of key values: Pleaf/2 ~ Pleaf (“real” data)
– Tree pointers? 1 tree pointer to next leaf
Example
• So how do we pick M and L (Pint and Pleaf)?
o Depends on the application, in our example, it would be based on
the disk-block size
28
Back to disk application example
• What makes B trees appropriate for such an application?
29
Example of B+-tree insertion
• Insertion sequence: 8, 5, 1, 7, 3, 12, 9, 6
– For B+-tree, assume Pint = 3 and Pleaf = 2
– Always insert to leaf node, all real data stay at leaf
– Split rule: “promote” the middle “virtual” value
• Left: leaf node <= upper level key; internal node < upper level key
• Right: >
• Practice:
– Insertion sequence 12, 9, 6, 7, 1, 5, 8, 3?
– How about 1, 5, 7, 3, 8, 12, 9, 6? (resulting tree same as the B+ tree
for 8, 5, 1, 7, 3, 12, 9, 6)
Example of
an Insertion in
a B+tree
B+ tree for 12, 9, 6, 7, 1, 5, 8, 3
B+ tree efficiency example
B+ tree
Calculate Pint & Pleaf
(Pint * Pt) + (Pint - 1)*K <= B Pleaf * (Pd + K) + Pt <= B
16 Pleaf + 6 <= 512
6 Pint + 9(Pint - 1) <= 512 Pleaf = 31
Pint = 34
Application Example (disk)
Consider the application of B-trees for directories. Suppose that you are given a B-tree with
parameters (Pint) M=11 and (Pleaf) L=8. Determine the approximate size of a disk block on
the machine where this implementation would be used, assuming you also have the following
information:
– Key Size =8 Bytes
– Pointer Size = 4 Bytes
– Data Size = 16 Bytes per record (including the key)
Give a numeric answer showing all the steps taken to arrive to your answer (Compute the
possible sizes of both internal and leaf nodes). In addition, give a short explanation using the
parameters given and the equations.
The larger size is 128 (which turns out to be a power of 2), hence the disk block is 128 bytes
35