I3306-chap2-TD2-EN - Fa23-24-Solution
I3306-chap2-TD2-EN - Fa23-24-Solution
Exercise 1:
Consider a disk with block size B=512 bytes. A block pointer is P=6 bytes long, and a record pointer
is P R =7 bytes long. A file has r=30,000 EMPLOYEE records of fixed-length. Each record has the
following fields: NAME (30 bytes), SSN (9 bytes), DEPARTMENTCODE (9 bytes), ADDRESS (40
bytes), PHONE (9 bytes), BIRTHDATE (8 bytes), SEX (1 byte), JOBCODE (4 bytes), SALARY (4
bytes, real number). An additional byte is used as a deletion marker.
(b) Calculate the blocking factor bfr and the number of file blocks b assuming an unspanned
organization.
Blocking factor bfr = floor(B/R) = floor(512/115) = 4 records per block
Number of blocks needed for file = ceiling(r/bfr) = ceiling(30000/4) = 7500
(c) Suppose the file is ordered by the key field SSN and we want to construct a primary index on
SSN. Calculate:
(i) the index blocking factor bfr i (which is also the index fan-out fo);
Index record size R i = (V SSN + P) = (9 + 6) = 15 bytes
Index blocking factor bfr i = fo = floor(B/R i ) = floor(512/15) = 34
(ii) the number of first-level index entries and the number of first-level index blocks;
Number of first-level index entries r 1 = number of file blocks b = 7500 entries
Number of first-level index blocks b 1 = ceiling(r 1 /bfr i ) = ceiling(7500/34)= 221 blocks
(v) the number of block accesses needed to search for and retrieve a record from the file--given its
SSN value--using the primary index.
Number of block accesses to search for a record = x + 1 = 3 + 1 = 4
(d) Suppose the file is not ordered by the key field SSN and we want to construct a secondary index
on SSN. Repeat the previous exercise (part c) for the secondary index and compare with the primary
index.
i. Index record size R i = (V SSN + P) = (9 + 6) = 15 bytes
Index blocking factor bfr i = (fan-out) fo = floor(B/R i ) = floor(512/15)= 34 index records per block
ii. Number of first-level index entries r 1 = number of file records r = 30000
Number of first-level index blocks b 1 = ceiling(r 1 /bfr i ) = ceiling(30000/34)= 883 blocks
(e) Suppose the file is ordered by the non-key field DEPARTMENTCODE and we want to construct
a clustering index on DEPARTMENTCODE that uses block anchors (every new value of
DEPARTMENTCODE starts at the beginning of a new block). Assume there are 1000 distinct values
of DEPARTMENTCODE, and that the EMPLOYEE records are evenly distributed among these
values. Calculate
(i) the index blocking factor bfr i (which is also the index fan-out fo);
Index record size R i = (V DEPARTMENTCODE + P) = (9 + 6) = 15 bytes
Index blocking factor bfr i = (fan-out) fo = floor(B/R i ) = floor(512/15)= 34 index records per block
(ii) the number of first-level index entries and the number of first-level index blocks;
Number of first-level index entries r 1= number of distinct DEPARTMENTCODE values= 1000
entries
Number of first-level index blocks b 1 = ceiling(r 1 /bfr i )= ceiling(1000/34) = 30 blocks
Exercise 2:
Insert the following key values:
3, 7, 9, 23, 45, 1, 5, 14, 25, 24, 13, 11, 8, 19, 4, 31, 35, 56
a. Into a B-tree.
b. Into a B+-tree.
Solution of Exercise 2:
a)
b)
Exercise 3:
Solution Exercise 3:
Add 2 :
Add 6 :
Add 12 :
Delete 4 :
Delete 5 :
Delete 7 :
Delete 3 :
Delete 6 :
Delete 14 :
Exercise 3:
Delete 5 : Remove the key value from the leaf node. It is also an internal node; remove it and
replace it by the next key value in the leaf node.
Delete 7 : We remove it from the leaf and the parent; then we merge with the left sibling. At the higher
level, we have an underflow. We borrow 23 from the right to replace the root and we borrow 13 from the
root.
Delete 23 : and delete it from the root. We replace it by the min of the leaf nodes at the right (24). We also
delete it at the higher level after the merge. When we delete 25, we could not borrow from the left sibling, then
we merge the left, the root and the right.
Exercise 4 :
The following table contains the information of employees in a company.
Using the index bitmap, define:
1- Males with first level income.
2- Employees (male or female) with a first level income.
3- Employees who do not have a 5th level income.
Solution of exercise 4:
1) m AND L1: 10010 AND 10100: 10000 (corresponds to the first record of the table).
2) m of f: 10010 OR 01101: 11111. Then 11111 AND L1 (10100): 10100 (first and third record).
3) NOT L5: NOT 00000 = 11111 (all records).