Final Exam
Final Exam
Final Exam
FINAL EXAM
• This is a closed book examination but you are allowed one 8.5” x 11” sheet of notes (double
sided).
• You should answer as many questions as possible. You should read all of the questions before
starting the exam, as some of the questions are substantially more time-consuming than others.
• Write all of your answers in the designated space provided with each question. We will be grading
only answers written in the designated space.
• You must put your CS 186 class account at the top of each page before you start the exam.
GOOD LUCK!
CS186 class account:
For each of the following statements below, each correct tick is worth 1 point,
each wrong tick is worth -1 point, and no tick is worth 0 points.
Tick the correct TRUE/FALSE boxes for each of the following statements. TRUE FALSE
6. Given B buffer page frames, External Merge Sort can sort a file of size
B(B − 1)5 in 6 passes.
8. For a given query and cardinality estimates, the space of query plans
inspected by the greedy approach is a strict subset of the space of query
plans inspected by the dynamic programming approach (both approaches
were introduced in class).
2
CS186 class account:
14. There are cases where aggregations can be performed by index-only plans.
15. If we do not know the query workload and plenty of disk space is available,
then it is always a good idea to create more indexes.
16. A clustered B+-tree index is preferable over a hash index if the query
workload frequently contains range selections.
3
CS186 class account:
1. Write down an SQL query that returns, for each student present in the database, the student id
S and the number of courses s/he is enrolled in. (Hint: this may mean that for some values of S
the count is 0!) (5 points)
2. Write down an SQL query that returns all pairs of students (S1 , S2 ) such that S1 has taken (at
least) all the courses that S2 has taken. (5 points)
4
CS186 class account:
1. We want to evaluate the three-way natural join on these relations using the Leapfrog Triejoin
algorithm with the join order (A,B,D,C). Draw the trie view of each relation. Write down the first
four tuples in the result in the order they are produced. Indicate on your diagram the position of
each trie-iterator when each of the first four tuples is emitted. (8 points)
5
CS186 class account:
6
CS186 class account:
2. Briefly describe an optimization that can be made to the basic block nested-loops join algorithm.
Make sure to include when the optimization is useful and describe why the optimized algorithm
can be more efficient. (6 points)
7
CS186 class account:
Consider the three relations R(A,B), S(B,C) and T(C,D), where all attributes are integer attributes
and each such integer attribute takes 5 bytes. Thus, each of the input tuples takes 10 bytes, but note
that projections and joins produce tuples of different arity and thus different size. You have to take this
into account. The page size is 1000 bytes and we assume for simplicity that all of this space can be used
to store tuples (the page header does not take space). Assume 22 buffer pages.
• Relation R has 100,000 tuples. Attribute B is the key of R. The domain of attribute A is integer
values in the interval [1, 1000]. The domain values of attribute A are uniformly distributed among
the tuples in R (i.e., each value is equally likely).
• Relation T has 100,000 tuples. Attribute C is the key of T. The domain of attribute D is integer
values in the interval [1, 1000]. The values of attribute D are distributed among the tuples in T
according to the following histogram (given in textual form):
Value of T.D in interval [1 .. 500] : 20000 tuples.
Value of T.D in interval [501 .. 1000] : 80000 tuples.
• Relation S consists of 1,000,000 tuples. Column B is the key of R referenced by foreign key B of
S and column C is the key of T referenced by foreign key C of S.
SELECT *
FROM R, S, T
WHERE R.B = S.B and S.C = T.C AND R.A <= 10 AND T.D > 100
1. Assume that you are using the System R style dynamic programming query optimization algorithm.
Show in detail the plans that the System R query optimization algorithm produces after the first
pass (i.e., “best” one relation plans). Explain why these plans are chosen over other plans. Clearly
state any assumptions you make. (3 points)
8
CS186 class account:
2. Choose the optimal query plan that the System R algorithm can find (with respect to I/O cost)
assuming that the only join operator implemented in the system is block nested-loops join and no
index structures are to be used. Assume that the query result does not have to be written to disk
but is displayed on screen (which has no cost per se).
Write down the optimal query plan as an operator tree where the outer loop input of each join
is on the left side. Compute, for each node of the query plan, the size of its output in number
of tuples and number of pages, plus the cost incurred at that node. In addition, state the overall
cost. Write down all the formulas you use. (12 points)
9
CS186 class account:
3. Now assume you are allowed to use any join operator implementation (block nested-loops join,
sort-merge join, hash join; no index nested-loops join because you must not use indexes). Is there
a better query plan that can be obtained by replacing the block nested-loops joins in the previous
plan but leaving all else (in particular, the join order) equal? If yes, draw a better (not necessarily
optimal) plan and calculate its cost. If no, argue why not. (5 points)
10
CS186 class account:
Consider the following transaction schedule, where time increases from left to right:
T1 : R(A) W(B) Commit
T2 : R(B) R(A) Commit
T3 : R(B) W(A) Commit
1. Draw the conflict graph for the above schedule. Is the schedule conflict serializable? (4 points)
2. Explain when a schedule can be view serializable but not conflict serializable. Give a small example
schedule that has this property. (4 points)
11
CS186 class account:
3. Explain in no more than three sentences the main idea of optimistic concurrency control. (4 points)
12
CS186 class account:
13
CS186 class account:
Extra Page for Scratch Work (to be discarded and not graded)
14
CS186 class account:
Extra Page for Scratch Work (to be discarded and not graded)
15
CS186 class account:
Extra Page for Scratch Work (to be discarded and not graded)
16