Docsity

Assignment 5 Solution | Database Systems | EEL 4852C, Assignments of Deductive Database Systems

Toggle sidebar
bg1
Database Systems
EEL4852C
Home work 5 solutions
15.13 Draw at least two query trees that can represented each of these queries. Under what
circumstances would you use each of your query trees?
Answer: Below are possible answers for Q1 and Q2.
Q1: SELECT E.FNAME, E.LNAME, S.FNAME, S.LNAME
FROM EMPLOYEE E, EMPLOYEE S
WHERE E.SUPERSSN = S.SSN
Q2: SELECT FNAME, LNAME, 1.1*SALARY
FROM EMPLOYEE, WORKS_ON, PROJECT
WHERE SSN = ESSN AND PNO = PNUMBER AND PNAME = 'ProductX'
Q1's tree1:
PROJECT E.FNAME, E.LNAME, S.FNAME, S.LNAME
E.SUPERSSN=S.SSN JOIN
EMPLOYEE E EMPLOYEE S
Q1'S tree2:
PROJECT
CARTESIAN PRODUCT
EMPLOYEE E EMPLOYEE S
E.FNAME, E.LNAME, S.FNAME, S.LNAME
SELECT E.SUPERSSN=S.SSN
The initial query tree for Q1 is the same as tree2 above; the only change made by the optimization
algorithm is to replace the selection and Cartesian product by the join in tree1. Thus, tree 1 is the result
after optimization.
Q2's tree1:
PROJECT FNAME, LNAME, SALARY
PNO=PNUMBER JOIN
EMPLOYEE PROJECT
SSN=ESSN JOIN SELECT PNAME="ProductX"
WORKS_ON
Q2's tree2:
PROJECT FNAME, LNAME, SALARY
PNO=PNUMBER AND SSN=ESSN AND PNAME="ProductX" SELECT
EMPLOYEE
PROJECT
CARTESIAN PRODUCT
WORKS_ON
CARTESIAN PRODUCT
The initial query tree of Q2 is tree2 above, but the result of the heuristic optimization process will NOT
be the same as tree1 in this case. It can be optimized more thoroughly, as follows:
pf3

Partial preview of the text

Download Assignment 5 Solution | Database Systems | EEL 4852C and more Assignments Deductive Database Systems in PDF only on Docsity!

Database Systems EEL4852C Home work 5 solutions 15.13 Draw at least two query trees that can represented each of these queries. Under what circumstances would you use each of your query trees? Answer: Below are possible answers for Q1 and Q2. Q1: SELECT E.FNAME, E.LNAME, S.FNAME, S.LNAME FROM EMPLOYEE E, EMPLOYEE S WHERE E.SUPERSSN = S.SSN Q2: SELECT FNAME, LNAME, 1.1*SALARY FROM EMPLOYEE, WORKS_ON, PROJECT WHERE SSN = ESSN AND PNO = PNUMBER AND PNAME = 'ProductX' Q1's tree1: PROJECT E.FNAME, E.LNAME, S.FNAME, S.LNAME E.SUPERSSN=S.SSN JOIN EMPLOYEE E EMPLOYEE S Q1'S tree2: PROJECT CARTESIAN PRODUCT EMPLOYEE E EMPLOYEE S E.FNAME, E.LNAME, S.FNAME, S.LNAME SELECT E.SUPERSSN=S.SSN The initial query tree for Q1 is the same as tree2 above; the only change made by the optimization algorithm is to replace the selection and Cartesian product by the join in tree1. Thus, tree 1 is the result after optimization. Q2's tree1: PROJECT FNAME, LNAME, SALARY PNO=PNUMBER JOIN EMPLOYEE PROJECT SSN=ESSN JOIN SELECT PNAME="ProductX" WORKS_ON Q2's tree2: PROJECT FNAME, LNAME, SALARY PNO=PNUMBER AND SSN=ESSN AND PNAME="ProductX" SELECT EMPLOYEE PROJECT CARTESIAN PRODUCT WORKS_ON CARTESIAN PRODUCT The initial query tree of Q2 is tree2 above, but the result of the heuristic optimization process will NOT be the same as tree1 in this case. It can be optimized more thoroughly, as follows:

PROJECT FNAME, LNAME, SALARY

PNO=PNUMBER JOIN EMPLOYEE

PROJECT

SSN=ESSN JOIN

SELECT PNAME="ProductX" WORKS_ON The reason is that the leaf nodes could be arranged (in Step 3 of the algorithm outlined on page 613) so that the more restrictive selects are executed first. 15.22 Compare the cost of two different query plans for the following query: salary > 40000 select (EMPLOYEE |X| DNO=DNUMBER DEPARTMENT) Use the database statistics in Figure 15. Answer: One plan might be for the following query tree |x| /
/
/
SELECT salary > 400 DEPARTMENT | | EMPLOYEE We can use the salary index on EMPLOYEE for the select operation: The table in Figure 15.8(a) indicates that there are 500 unique salary values, with a low value of 1 and a high value of 500. (It might be in reality that salary is in units of 1000 dollars, so 1 represents $1000 and 500 represents $500,000.) The selectivity for (Salary > 400) can be estimated as (500 - 400)/500 = 1/ This assumes that salaries are spread evenly across employees. So the cost (in block accesses) of accessing the index would be Blevel + (1/5) * (LEAF BLOCKS) = 1 + (1/5)* 50 = 11 Since the index is nonunique, the employees can be stored on any of the data blocks. So the the number of data blocks to be accessed would be (1/5) * (NUM_ROWS) = (1/5) * 10,000 = 2000 Since 10,000 rows are stored in 2000 blocks, we have that 2000 rows can be stored in 400 blocks. So the TEMPORARY table (i.e., the result of the selection operator) would contain 400 blocks. The cost of writing the TEMPORARY table to disk would be 400 blocks. Now, we can do a nested loop join of the temporary table and the DEPARTMENT table. The cost of this would be b + (b * b )