File Organization and Indexing: Structure of Disks
File Organization and Indexing: Structure of Disks
Sreenivasa Kumar
2nd option is preferred as RDBMS requires complete control over when a block in
Structure of Disks
Speed:
7000 to
Platters 10000 rpm
Read/write head
track
} sector
Disk
several platters stacked on a rotating spindle
Data transfer:
Blocks on the same cylinder - roughly close to each other - access time-wise
• the number of records, the length of each field are available in file header
• special separator symbols are used to indicate the field boundaries and
record boundaries
• the number of records, the separator symbols used are recorded in the
file header
• Usual case
Record
1 2 2 3 3
1
File blocks
Linked allocation
Mixed allocation
Example: Student file - organized to have student’s records sorted in increasing order of
Goal: To ensure that operations performed frequently on the file execute fast
are needed.
Heap file:
Records are appended to the file as they are inserted Simple organization
Insertion - Read the last file block; append the record write back the block - easy
Heap files are often used only along with other access structures.
The field whose values are used for sorting the records in the data file
Data file whose records are arranged such that the values of the ordering field
(Recall that the address of the nth file block can be obtained from the file header)
• periodically file is reorganized - auxiliary file and main file are merged
• locating record
• Carried out first on auxiliary file and then the main file.
Deleting a record:
Hashed files
Very useful file organization, if quick access to the data record is needed given
Hashing field: The attribute on which quick access is needed and on which
hashing is performed
consecutive blocks)
Hash function h: maps the values from the domain of the hashing attribute to
bucket numbers
Overflow
1 chain
M-1 Overflow
buckets
Main buckets
Insertion: for the given record R, apply h on the value of hashing attribute to get
the bucket number r.If there is space in bucket r, place R there else place R in the
Overflow
1 chain
M-1 Overflow
buckets
Main buckets
bucket/overflow chain if possible bring a record from the overflow chain into the
bucket.
access
buckets)
Disadvantage with static hashing actual records in the file – much less than C
• wastage of disk space actual records in the file – much more than C
two schemes
• Extendible hashing
• Linear hashing
Apply hashing function to the value of the hashing field of R to get the bucket
number r
Convert r into its binary representation to get the bit sequence Take the leading
k bits.
Extendible Hashing
Local depth
The # of 2
higher order Global depth d=3 The number of bits in the
bits used in common prefix of bit
the directory 000 sequences corresponding to
3 the records in the bucket
001
010 3
011
All records with 2-bit
100 Sequence ’10’
2
101
110
111 3
Locating a record
Match the d-bit sequence with an entry in the directory and go to the
b0 b0
full 1
00 b3
00 b1
2 01
01
10 b1
10
b2 11
11
2 d=2
b2
d=2
all local
depth = 2
Bucket b0 is split
b0 Full : All records whose 2-bit sequence is ‘01’ are sent to a new bucket b3. Others
b0 2
b0
000
001 b3
2
b3 010
00
01 011
b1 3
10 b1 100
full
11 101
110 b4 3
d=2 b2
111
b2
all local d=3 2
depth = 2
b1 full : b1 is split, directory is doubled, all records with 3-bit sequence 101 sent to b4.
Others in b1.
In general, if the local depth of the bucket to be split is equal to the global depth,
directory is doubled.
Linear Hashing
Does not require a separate directory structure
M – the initial no. of buckets (Assume that the hashing field is an integer)
Insertion (1/3)
0
Overflow
buckets
1
.
.
M-1
M Split image
of bucket 0
Initially the structure has M main buckets and a few overflow buckets
To insert a record with hash field value k, place the record in bucket ho(k)
When the first overflow in any bucket occurs Say, overflow occurred in bucket s
Insertion (2/3)
On first overflow, irrespective of where it occurs, bucket 0 is split On subsequent
ho h1 hi
h1 h2 hi+1
.
.
.
M-1
M+1
Split
images
M+2
.
.
Insertion (3/3)
Say the hash functions in use are hi, hi+1
Compute hi(k)
if hi(k) < N, original bucket is already split place the record in bucket hi+1(k)
Index Structures
Index: A disk data structure – enables efficient retrieval of a record given the
Primary Index:
Clustering Index:
Secondary Index:
Primary Index
101 0
104
.
.
.
.
101 121 1
121 123
.
129 .
.
. .
.
. 129
. 2
130
.
.
240 .
.
.
.
.
.
240 b
244
.
.
.
.
Ordering key
(RollNo) Data
file
Index Entry:
Index file: ordered file (sorted on OKF) size-no. Of blocks in the data file
m – value of OKF for the first record in the middle block k of index file
corresponding block pointer and get the data file block and search for the data record
with OKF value x else do binary search on blocks k+1……bi of index file
An Example
Data file:
Index file
9500
entries
49 entries
.
.
. .
.
.
.
. .
.
Second level
index
1 block First level
index data file
49 blocks 9500 blocks
Multilevel Index –
Successive levels of indices are built till the last level has one block
height – no. of levels block accesses: height + 1 (no binary search required)
No of block accesses required with multi-level primary index: 3 without any index: 14
Use the index to locate the record with OKF value x1 and read succeeding records till
Insertion: Data file – keep 25% of space in each block free - to take care of future
insertion – index doesn't get changed or use overflow chains for blocks that overflow
Deletion: Handle using deletion markers so that index doesn’t get affected
Clustering Index
Built on ordered files where ordering field is not a key Index attribute: ordering
field (OF)
Index entry:
Distinct value Vi address of the first block
of the OF hat has a record with OF value Vi
Secondary Index
Built on any non-ordering field (NOF) of a data file
value of the NOF Vi pointer to the record with Vi as the NOF value
(1)
value of the NOF Vi pointer(s) to the record(s) with Vi as the NOF value
(2)
value of the NOF Vi pointer to a block that has pointer(s) to the record(s)with Vi as the NOF value
Index entry:
value of the NOF Vi pointer to the record with Vi as the NOF value
Index file: ordered file (sorted on NOF values) No. of entries – same as the no. of
Index file blocking factor Bfi = ⌊B/(V+Pr)⌋ (B – block size, V – length of the NOF,
An Example
Data file:
b = ⌈90000/40⌉ = 2250
Index file:
Insertions:
Deletions:
B+- trees
Balanced search trees
Makes sure that blocks are between half used to completely full
Order
Order (m) of an Internal Node
it.
• order of a leaf node is the maximum number of record pointers a leaf can
Internal Node
An internal node structure of a B+- tree of order m:
m
It contains atleast 2 pointers, except it is the root node
m/2 ≤j≤m
P1 K1 P2 K2 … Ki-1 Pi Ki … Kj-1 Pj …
Example
2 5 12 -
K1 Pr1 K2 Pr2 … K j Pj … P
Order calculation
Block size B, Size of Indexing field V
(m*p) + ((m-1) * v) ≤ B
As there can be mleaf record pointers and keys with one block pointer mleaf can
be calculated by solving
m * P + ((m-1) *V) ≤ B
14m ≤ 520
m ≤ 37
mleaf (7 + 8) + 6 ≤ 512
15mleaf ≤ 506
mleaf ≤ 33
3 7
2 - 4 - 9 -
1 2 3 - 4 - 6 7 8 9 12 15 ^
Example of Insertions
m = 3 mleaf = 2
Insert 20, 11
1 Insert 14
11 20 ^
(m leaf + 1)
at j = =2
2
14 is replicated to upper level
2 Insert 25
14 - ^
Inserted at
leaf level
11 14 20 - ^
3
14 - ^
Insert 30
overflow split at
11 14 20 25 ^ 25.25 is moved
4
14 25
11 14 20 25 30
5 14 - .
12 - ^ 25 - ^
11 12 14 - 20 25 30 -
m
Internal node split at j = split at 14 and 14 is moved up
2
Insert 22
14 - ^ 6
12 - ^ 22 25
11 12 14 20 22 25 30 -
Insert 23, 24
14 24 7
12 - ^ 22 25
11 12 14 20 22 23 24 25 30
Delete the entry if it is present in Internal node and replace withthe entry to its
if not possible – Merge the node with left and right sibling
Example
14 24
12 22 25
11 12 14 20 22 23 24 25 30
Delete 20
Removed entry from leaf here
14 24
12 22 25
11 12 14 22 23 24 25 30
Delete 22
14 24
12 23 25
11 12 14 23 24 25 30
Entry 22 is removed from leaf and internal node Entries from right sibling are
distributed to left
Delete 24
14
12 23 25
11 12 14 23 25 30
Delete 14
12
11 23 25
11 12 23 25 30
Delete 12
23 25
11 23 25 30
Level drop has occurred
3) Height of the tree is less as only keys are used for indexing
Insert 45, 22
Local depth
Global 0
depth 0 45
22
Insert 12 1
1 22
0 12
1
1
45
Insert 11
1
1 22
0 12
1
1
45
11
Delete 15
Overflow occurs.
1
22
12
2 2
00 45
01
10
11 2
11
15
Delete 10
2
12
2
45
2
00
01 2
10 10
11 12
2
11
Overflows
Insert 12, 11
N Insert 0 12 h0 = xmod 2
14 N h1 = xmod 4
0 12 14
11
B0 overflows
Bucket pointed by
N is split 1 11
Mod functions are
changed
Insert 13
0 12 h0 = xmod 2
N
0 12 h1 = xmod 4
Insert 9
14 N
1 11 9
B1 overflows 13
1 11 B0 is split using by
and split image
is created
2 14
Insert 10
0 12
0 12
Insert 18
1 9
N 13
1 11 9 Overflow at B3
h2 is applied here 13 Split B1 2 14 18
h0 = mod 4 10
h1 = mod 8
2 14 3 11
10