HW 3 Sol
HW 3 Sol
Homework 3
TAs:
Yang Chen, Kun Li, Yang Peng
yang, kli, [email protected] l.edu
Name:
UFID:
Email Address:
Signature
i
COP5725, Fall 2013 Homework 3 Page 1 of 7
00110 2
i=3
01010 2
000
01011 i=3
001
000
n=5
010 10011 3 r=7
001
011
00110 10110
100 010
10100 3 01010 11010
101 10110 01011
011
110 10011
11010 2 10100
111 100
(3) [3 points] Describe a scenario when extensible hash tables are preferred over linear
hash tables. Explain your answer.
Solution: Two possibilities:
• When the insertions are very frequent. This is because linear hash table is reorganized every
time a new bucket is added. A new bucket is added frequently.
• When the values of the keys that the index is built on uniformly distributed keys. (i.e., the
hash table is filled uniformly).
34 54
10 23 29 30 34 40 46 49 54 59 70 75
(5) [3 points] Show the result B+ tree after inserting values 80, 24, 42.
Solution:
42
24 34 54 70
10 23 24 29 30 34 40 42 46 49 54 59 70 75 80
(6) [3 points] Based on the B+ tree in (5), show the result B+ tree after deleting values
10, 40.
Solution:
29 42 54 70
23 24 29 30 34 42 46 49 54 59 70 75 80
(7) [8 points] Fill in the cost table below for “Alternative 1” ISAM and B+ tree indices.
Assume each index takes P pages on disk, has height H, and fanout F at each internal
node. Assume there are R tuples in the relation, and B tuples fit on a leaf (or overflow)
page. In each case, assume infinite buffer pool size, but the buffer pool starts out empty.
For each page that gets dirty, add 1 to your I/O cost since it will eventually have to
be flushed to disk. For ISAM, assume that a leaf node maintains only a pointer to
the beginning of an overflow list. Given the constraints of a B+ Tree/ISAM, assume
whatever data you want in the tree for each case below.
Solution:
COP5725, Fall 2013 Homework 3 Page 3 of 7
Suppose that the following indexes, all using Alternative (2) for data entries, exist: a hash
index on eid, a B+ tree index on sal, a hash index on age, and a clustered B+ tree index on
(age, sal). Each Employees record is 100 bytes long, and you can assume that each index
data entry is 20 bytes long. The Employees relation contains 10,000 pages.
(1) Consider each of the following selection conditions and, assuming that the reduction factor
(RF) for each term that matches an index is 0.1, compute the cost of the most selective access
path for retrieving all Employees tuples that satisfy the condition (in terms of the number
of I/O’s):
COP5725, Fall 2013 Homework 3 Page 5 of 7
i. [4 points] age=25.
Solution: The clustered B+ tree index would be the best option here, with a cost of 2 (lookup)
+ 10000 × 0.1 (data pages) + 10000 × 0.2 (index pages) ×0.1 = 1202. Although the hash index
has a less lookup time, the potential number of record lookups (10000 × 0.1 × 20 tuples per
page = 20000) renders the clustered index more efficient.
ii. [4 points] sal>200 AND age>30 AND title=’CFO’.
Solution: Here an age condition is present, so the clustered B+ tree index on (age, sal)
can be used. The cost is 2 + 10, 000 × 0.2 × 0.1 (all index pages needs to be fetched satisfying
age>30) + 10, 000 × 0.1 × 0.1 (data pages) = 302.
(2) [5 points] Identify a query plan that a decent query optimizer would choose.
Solution:
πD.dname, F.budget
π F.did, F.budget
E D
(3) Suppose that the following additional information is available:
• Unclustered B+ tree indexes exist on Emp.did, Emp.sal, Dept.did, and Finance.did
(each leaf page contains up to 200 entries).
• The systems statistics indicate that employee salaries range from 10,000 to 60,000, employees
enjoy 200 different hobbies.
• The company owns two floors in the building.
• There are a total of 50,000 employees and 5,000 departments (each with corresponding
financial information) in the database.
• The DBMS used by the company has just one join method available, namely index nested
loops.
COP5725, Fall 2013 Homework 3 Page 6 of 7
i. [3 points] For each of the query’s base relations, estimate the number of tuples that would
be initially selected from that relation if all of the non-join predicates on that relation were
applied to it before any join processing begins.
Solution:
1000 1
• Emp: 50000 × 50000 × 200 = 5.
1
• Dept: 5000 × 2 = 2500.
• Finance: 5000.
ii. [8 points] Under the System R approach, determine a join order that has the least estimated
cost. Compute the cost of your plan (in terms of the number of disk I/O’s).
Solution: ((D ./ E) ./ F ).
• First, we use the fact that there is a B-tree index on salary to retrieve the tuples from
E such that E.salary >= 59000. We estimate that (50000/50) = 1000 such tuples
selected out, with a cost of 1 tree traversal (say 3 I/O’s to get to the leaf) + the cost of
scanning the leaf pages (1000/200 + 1 - 1 = 5) + the cost of retrieving the 1000 tuples
(since the index is unclustered each tuple is potentially 1 disk I/O) = 3 + 5 + 1000 =
1008. Of these 1000 retrieved tuples, do an on-the fly select out only those that have
hobby = "yodelling", we estimate there will be (1000/200) = 5 such tuples.
• Pipeline these 5 tuples from E one at a time to D. By using the B+ tree index on D.did
and the fact the D.did is a key, we can find the matching tuples for the join by searching
the D.did B+ tree and retrieving at most 1 matching tuple per tuple from E.
The cost of E ./ D is hence total cost of index nested loop. 5×(tree traversal of D.did
Btree + record retrieval) = 5 × (3 + 1) = 20.
• Now select out the d5/2e = 3 tuples that have D.floor = 1 on the fly and pipeline it to
the next level F. (This is done after E ./ D is done). Use the B+ tree index on F.did
and the fact that F.did is a key to retrieve at most 1 tuple for each of the 3 pipelined
tuples. This cost is at most 3 × (3 + 1) = 12.
• Ignoring the cost of writing out the final result, we get a total cost of 1008 + 20 + 12 =
1040.
Say our scheduler performs exclusive locking only (i.e., no shared locks). For each of the
following three instances of transactions T1 and T2 annotated with lock and unlock actions,
say whether the annotated transactions:
a) T1 : l1 (B); l1 (C); w1 (C); l1 (A); r1 (A); w1 (A); r1 (B); w1 (B); Commit; u1 (A); u1 (C); u1 (B);
T2 : l2 (B); r2 (B); w2 (B); l2 (A); r2 (A); w2 (A); Commit; u2 (A); u2 (B);
b) T1 : l1 (C); l1 (A); r1 (A); w1 (C); w1 (A); l1 (B); r1 (B); w1 (B); u1 (A); u1 (C); u1 (B); Commit;
T2 : l2 (B); r2 (B); w2 (B); l2 (A); r2 (A); w2 (A); Commit; u2 (A); u2 (B);
c) T1 : l1 (C); w1 (C); l1 (A); r1 (A); w1 (A); l1 (B); r1 (B); w1 (B); Commit; u1 (A); u1 (C); u1 (B);
T2 : l2 (B); r2 (B); w2 (B); l2 (A); r2 (A); w2 (A); Commit; u2 (A); u2 (B);