Exercises 3 PDF
Exercises 3 PDF
Exercises 3 PDF
Exercises
(Course: Database Management Systems)
Chapter 3
Algorithms for Query Processing and Optimization
1. Exercise 19.13 in the text book (“Fundamentals of Database Systems- 6th Edition”, Elmasri
et al.)
Consider SQL queries Ql, Q8, QIB, Q4 in Chapter 4, and Q27 in Chapter 5.
Q1: SELECT FNAME, LNAME, ADDRESS
FROM EMPLOYEE, DEPARTMENT
WHERE DNAME='Research' AND DNUMBER=DNO;
a. Draw at least two query trees that can represent each of these queries. Under what circumstances
would you use each of your query trees?
Khoa Khoa học & Kỹ thuật Máy tính – Đại học Bách Khoa TP.HCM 1
DBMS – HK1 2016-2017 Exercises – Chapter 3
b. Draw the initial query tree for each of these queries, and then show how the query tree is
optimized by the algorithm outlined in Section 19.7.
c. For each query, compare your own query trees of part (a) and the initial and final query trees of
part (b).
2. Exercise 19.14 in the text book (“Fundamentals of Database Systems- 6th Edition”, Elmasri
et al.)
A file of 4096 blocks is to be sorted with an available buffer space of 64 blocks. How many passes
will be needed in the merge phase of the external sort-merge algorithm?
Khoa Khoa học & Kỹ thuật Máy tính – Đại học Bách Khoa TP.HCM 2
DBMS – HK1 2016-2017 Exercises – Chapter 3
a. Write the relational algebraic expression that is equivalent to the above query and draw a query
tree for the expression.
b. Apply the heuristic optimization transformation rules to find an efficient query execution plan for
the above query. Assume that the number of the suppliers in New York is larger than the number
of the projects with the budgets more than 10000000$ (i.e. the size of select(suppliers in New
York) > the size of select (project with budget > 10 000 000$).
5. Suppose that we have the EMPLOYEE & DEPARTMENT file described as follows:
The EMPLOYEE file has rE = 10000 records stored in bE = 2000 disk blocks with the blocking
factor bfrE = 5 records/block and the following access paths:
i/ A clustering index on SALARY, with levels xSALARY = 3 and first-level (base level) index blocks
bl1SALARY = 50 and average selection cardinality sSALARY = 20.
ii/ A secondary index on the key attribute SSN, with xSSN = 4 (sSSN = 1) and first-level (base level)
index blocks bl1SSN = 50.
iii/ A secondary index on the nonkey attribute DNO, with xDNO = 2 and first-level (base level) index
blocks bl1DNO = 4. There are dDNO = 125 distinct values for DNO, so the selection cardinality of
DNO is sDNO = (rE/dDNO) = 80.
iv/ A secondary index on SEX, with xSEX = 1. There are dSEX = 2 values for the SEX attribute, so the
average selection cardinality is sSEX = rE*(1/dSEX) = 5000.
The DEPARTMENT file consists of rD = 125 records stored in bD = 13 disk blocks with the
blocking factor bfrD = 10 records/block and the following access paths:
i/ A primary index on DNUMBER with xDNUMBER = 1 level (sDNUMBER = 1).
ii/ A secondary index on MGRSSN with selection cardinality sMGRSSN = 1 and levels xMGRSSN = 2 and
first-level (base level) index blocks bl1MGRSSN = 2.
Khoa Khoa học & Kỹ thuật Máy tính – Đại học Bách Khoa TP.HCM 3
DBMS – HK1 2016-2017 Exercises – Chapter 3
Determine the reasonable execution plans for each query below using cost-based optimization:
E1. SELECT *
FROM EMPLOYEE, DEPARTMENT
WHERE DNAME = 'Research' and DNUMBER = DNO;
Given: the join selectivity is jsE1 = 1/|DEPARTMENT| = 1/rD = 1/125 because DNUMBER is a key
of DEPARTMENT; the blocking factor for the resulting join file is bfrED = 4 records per block.
E2. SELECT *
FROM EMPLOYEE as E, EMPLOYEE as S
WHERE E.SUPERSSN = S.SSN;
Given: the join selectivity is jsE2 = 1/|EMPLOYEE| = 1/rE = 1/10000 because SSN is a key of
EMPLOYEE; the blocking factor for the resulting join file is bfrEE = 2 records per block; the
available buffer space in main memory is nB = 7 blocks.
Khoa Khoa học & Kỹ thuật Máy tính – Đại học Bách Khoa TP.HCM 4