Docsity

Advanced Database Systems-Home Work 04-Computer Science, Study notes of Database Management Systems (DBMS)

Toggle sidebar
bg1
CPS 216 Spring 2005
Homework #4
Assigned: Thursday, April 21
Due: Thursday, April 28
Problem 1.
Consider tables R(A, B, C), S(C, D), and T(D, E). Transform the following query into an
equivalent query that:
• Contains no cross products;
• Performs projections and selections as early as possible.
(a) πR.B, S.D, T.E σ(R.A=10) and (R.C = S.C) and (S.D = T.D) and (R.A > T.E) (R × S × T)
Suppose we have the following statistics:
• | R | = 1,000; | πA R | = 1,000; | πB R | = 100; | πC R | = 500;
• | S | = 5,000; | πC S | = 300; | πD S | = 10;
• | T | = 4,000; | πD T | = 4,000; | πE T | = 1,500.
Estimate the number of the rows returned by the following queries:
(b) σA=10 R
(c) σA=10 or B = “Bart” R
(d) R S
(e) R S T
For the following question, further suppose that:
• Each disk/memory block can hold up to 10 rows;
• All tables are stored compactly on disk (10 rows per block) in no particular order;
• No indexes are available;
• 11 memory blocks are available for query processing.
(f) What is the best execution plan (in terms of number of I/O’s performed) you can come
up with for the query σR.B = “Bart” and S.D = 100 (R S)? Describe your plan and show the
calculation of its I/O cost.
Problem 2.
Consider the following relational schema and SQL query. The database stores information
about employees, departments, and company finances (organized on a per-department basis).
CREATE TABLE Emp(eid INT NOT NULL PRIMARY KEY,
did INT, salary INT, hobby CHAR(20));
CREATE TABLE Dept(did INT NOT NULL PRIMARY KEY,
name CHAR(20), floor INT, phone CHAR(10));
pf3

Partial preview of the text

Download Advanced Database Systems-Home Work 04-Computer Science and more Study notes Database Management Systems (DBMS) in PDF only on Docsity!

CPS 216 Spring 2005 Homework # Assigned: Thursday, April 21 Due: Thursday, April 28

Problem 1.

Consider tables R ( A , B , C ), S ( C , D ), and T ( D , E ). Transform the following query into an equivalent query that:

  • Contains no cross products;
  • Performs projections and selections as early as possible.

(a) π R. B , S. D , T. E σ( R. A =10) and ( R. C = S. C ) and ( S. D = T. D ) and ( R. A > T. E ) ( R × S × T )

Suppose we have the following statistics:

  • | R | = 1,000; | π A R | = 1,000; | π B R | = 100; | π C R | = 500;
  • | S | = 5,000; | π C S | = 300; | π D S | = 10;
  • | T | = 4,000; | π D T | = 4,000; | π E T | = 1,500.

Estimate the number of the rows returned by the following queries:

(b) σ A =10 R (c) σ A =10 or B = “Bart” R (d) R  S (e) R  S  T

For the following question, further suppose that:

  • Each disk/memory block can hold up to 10 rows;
  • All tables are stored compactly on disk (10 rows per block) in no particular order;
  • No indexes are available;
  • 11 memory blocks are available for query processing.

(f) What is the best execution plan (in terms of number of I/O’s performed) you can come up with for the query σ R. B = “Bart” and S. D = 100 ( R  S )? Describe your plan and show the calculation of its I/O cost.

Problem 2.

Consider the following relational schema and SQL query. The database stores information about employees, departments, and company finances (organized on a per-department basis).

CREATE TABLE Emp(eid INT NOT NULL PRIMARY KEY, did INT, salary INT, hobby CHAR(20)); CREATE TABLE Dept(did INT NOT NULL PRIMARY KEY, name CHAR(20), floor INT, phone CHAR(10));

CREATE TABLE Finance(did INT NOT NULL PRIMARY KEY, budget FLOAT, sales FLOAT, expenses FLOAT); SELECT d.name, f.budget FROM Emp e, Dept d, Finance f WHERE e.did = d.did AND d.did = f.did AND d.floor = 1 AND e.salary > 59000 AND e.hobby = ‘yodeling’;

(a) Identify a logical plan (using relational algebra operators) that reflects the order of operations a decent query optimizer would choose. (b) List all join orders (i.e., the orders in which pairs of tables are joined to compute the query result) considered by a query optimizer that follows the heuristic of considering only left-deep plans without cross products. (c) Suppose that the following information is available:

  • There are primary (clustering) indexes on all primary keys.
  • There are secondary (non-clustering) indexes on Emp(did), Emp(salary), and Dept(floor).
  • There are a total of 50,000 employees and 5,000 departments (each with corresponding financial information) in the database.
  • The system’s statistics indicate that employee salaries range from 10,001 to 60,000 (inclusive), employees enjoy 200 different hobbies, and the company has two floors in the building. For each of the query’s base tables (Emp, Dept, and Finance), estimate the number of rows that would be initially selected if all selection predicates were pushed down as much as possible. (d) Suppose that the database system has only one join method available—index nested- loop join. Using the same information in (c), which of the join orders considered by the optimizer in (b) has the lowest estimated cost?

Problem 3.

For this problem, you are going to experiment with the query optimizer in IBM DB2. You can see the execution plan chosen by the optimizer using the following command in shell: dynexpln -d dbcourse -q ” query ” -g -t Here, query is a string containing the SQL query (with no terminating “;”) that you wish to optimize. The output should be fairly self-explanatory. For the questions below, write meaningful queries over the system catalog tables including syscat.tables, syscat.columns, syscat.indexes, etc. To see what information is available in these tables, you can use the describe command: db2 “describe select * from table ” It describes the schema of the specified table.

(a) Write a query for which the optimizer chooses a nested-loop join (NLJOIN). (b) Write a query for which the optimizer chooses a sort-merge join (MSJOIN). (c) Write a join query involving three tables. Note the join order chosen by the optimizer.