Lecture 8 Objectives Physical Database Design
Lecture 8 Objectives Physical Database Design
Lecture 8 Objectives Physical Database Design
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Ordered Files<br />
Find<br />
Say we wanted to find record with ordering key value K then we require<br />
a binary search<br />
The binary search starts by reading the middle block, M, calculated as:<br />
M = (1 + B) div 2 where B is the number of blocks<br />
Suppose:<br />
the smallest value in block M is called S and<br />
the largest value in block M is called L<br />
There are 3 possibilities:<br />
1. K < S – this means record K is somewhere in blocks 1 to M-1<br />
2. K < L – this means record K is somewhere in blocks M+1 to B<br />
3. Neither 1 or 2 in which case record K is in the current block, M<br />
In cases 1 and 2 the search continues at the next mid block<br />
For Ordered Files on average we will require ⎡log 2 B ⎤ block access<br />
Hash Files<br />
• Here one or more fields in a record is used to<br />
calculate the location of record for storage and<br />
retrieval<br />
• Unit of storage is a bucket<br />
– these contain blocks which store up to bfr records<br />
• e.g if bfr = 4 then you can store 4 records max per block<br />
• A hash function is used to calculate location of<br />
bucket<br />
– Enter record into next free space in block<br />
– If all the blocks are used up then create an overflow<br />
25<br />
Find cont.<br />
Ordered Files<br />
Given the following blocks, find record with key value, K=300<br />
block 1 block 2 block 3 block 4 block 5 block 6<br />
1 2 3 4 5 7 8 11 12 14 16 18 20 30 40 50 100 150 200 300 400<br />
B = 6 so read mid-block number i.e (1 +6) div 2 = block 3<br />
300 not in block 3 and K>18 so look between blocks 4 to 6<br />
block 4 block 5 block 6<br />
20 30 40 50 100 150 200 300 400<br />
Read new mid-block number (4+6) div 2 = block 5<br />
300 not in block 5 and K>200 so look at block 6 - found<br />
Correct block found with 3 block accesses i.e ⎡log2 6 ⎤ =3<br />
- quicker than a linear search which requires 6 file block access<br />
Hash Files<br />
Insert<br />
Assume we have 2 blocks per bucket with bfr = 4<br />
Also assume we have a hash function on K mod 5 – so could have:<br />
Bucket 1 Bucket 2<br />
1 6 11 16 21 26 31 36 22<br />
2 7 12 17<br />
Use overflow buckets when there is no room (collisions)<br />
e.g insert 41 and 46<br />
Bucket 1 Bucket 2<br />
1 6 11 16 21 26 31 36 22<br />
2 7 12 17<br />
Overflow Bucket<br />
bucket and place record in there 41 46<br />
27<br />
28<br />
26