Docsity

Homework #3 - Database Design and Implementation | CMPSCI 645, Assignments of Principles of Database Management

Toggle sidebar
bg1
CMPSCI 645 -- Homework 3
Due March 26, before class
Question 1 Evaluation of selections
Consider a relation with this schema:
Employees(eid: integer, ename: string, sal: integer, title: string, age: integer)
Suppose that the following indexes, all using Alternative (2) for data entries, exist:
a) a hash index on eid,
b) a B+ tree index on sal,
c) a hash index on age,
d) 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 and each page holds 20 d ata records.
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 (including file scan and various kinds of index scan):
(1) sal > 100
(2) age = 25
(3) eid = 1000
(4) sal > 200 ∧ age > 20
Question 2 Output of join algorithms
Suppose we have two unary (one attribute only) relations, R and S:
R S
-------------
7 8
2 4
9 2
8 1
3 3
9 2
1 7
3 3
6
Show the result of joining R and S using each of the following algorithms. List the results in the order that
they would be output by the join algorithm. Note that the result relation contains only one attribute, which is
the common attribute between R and S.
(1) Nested loops join (one tuple at a time). Use R for the outer loop and S for the inner loop.
pf3
pf4
pf5

Partial preview of the text

Download Homework #3 - Database Design and Implementation | CMPSCI 645 and more Assignments Principles of Database Management in PDF only on Docsity!

CMPSCI 645 -- Homework 3

Due March 26, before class

Question 1 Evaluation of selections Consider a relation with this schema: Employees( eid : integer, ename : string, sal : integer, title : string, age : integer) Suppose that the following indexes, all using Alternative (2) for data entries, exist: a) a hash index on eid , b) a B+ tree index on sal , c) a hash index on age , d) 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 and each page holds 20 data records. 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 (including file scan and various kinds of index scan): (1) sal > 100 (2) age = 25 (3) eid = 1000 (4) sal > 200 ∧ age > 20 Question 2 Output of join algorithms Suppose we have two unary (one attribute only) relations, R and S: R S


7 8 2 4 9 2 8 1 3 3 9 2 1 7 3 3 6 Show the result of joining R and S using each of the following algorithms. List the results in the order that they would be output by the join algorithm. Note that the result relation contains only one attribute , which is the common attribute between R and S. (1) Nested loops join (one tuple at a time). Use R for the outer loop and S for the inner loop.

(2) Sort merge join. (3) Hash join. Assume there are two hash buckets, numbered 0 and 1, and that the hash function sends even values to bucket 0 and odd values to bucket 1. In Phase II, use R as the “build” relation and S as the “probe” relation. Assume that bucket 0 is read first and that the contents of a bucket are read in the same order as they were written. Question 3 Hybrid hash join Consider a join of relations R and S using hash join, where M = # pages_in_R = 400 pages and N = # pages_in_S = 500 pages. Assume that we have B = 40 pages of memory available in our buffer. We notice that there is more than enough memory, so we keep one of the hash buckets in memory to avoid writing it during the partitioning phase and then re-reading it during the probing phase. Assuming that all buckets have the same size. (1) How many buckets should be used in order to still be able to perform the join in two passes? (2) What would be the cost in number of I/Os of the operation proposed in part 1? Question 4 Query Optimization Consider the following schema: Sailors(sid, sname, rating, age) Boats(bid, bname, size) Reserves(sid, bid, day) Reserves.sid is a foreign key to Sailors and Reserves.bid is a foreign key to Boats.bid. We are given the following information about the database: Sailors contains 1000 records with 20 records per page. Boats contains 100 records with 10 records per page. Reserves contains 10,000 records with 40 records per page. There are 100 values for Reserves.sid. There are 50 values for Reserves.bid. There are 1000 values for Reserves.day In the following queries, assume that a System R style optimizer is used. Consider Query 1: SELECT S.sid, S.sname, B.bname FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid AND R.bid = B.bid AND R.day = 'July 4, 2003'; ( 1) Assuming uniform distribution of values and column independence, estimate the number of tuples returned by this query.

System catalog and statistics. As we learned in class, the query planner needs to estimate the number of rows retrieved by a query in order to make good choices of query plans. One important type of the statistics is the total number of entries in each table and index, as well as the number of disk blocks occupied by each table and index. This information is kept in the table pg_class , in the columns reltuples and relpages. We can look at it with queries similar to this one: SELECT relname, relkind, reltuples, relpages FROM pg_class WHERE relname LIKE 'author%'; The view pg_stats provides additional information about each attribute. Of particular importance are:

  • avg_width : the average width of an attribute, which is especially useful for variable length attributes.
  • n_distinct : the number of dinstct values in an attribute. If greater than zero, it is the estimated number of distinct values in the column. If less than zero, it is the negative of the number of distinct values divided by the number of rows. For example, - 1 indicates a unique column in which the number of distinct values is the same as the number of rows.
  • histogram_bounds : and the histogram bounds that divide the column's values into groups of approximately equal population. select tablename, attname, avg_width, n_distinct, histogram_bounds from pg_stats where tablename like 'author%'; (1) Consider the following query: select * from authors where name < 'Brian D. Withers'; What is the query plan that PostgreSQL uses for this query? What is the estimated number of rows? What is the actual number of rows returned by the query? If the estimated number of rows differs from the actual number of rows returned, explain how this happens. (Hint: Use pg_class and pg_stats to look for the statistics about the authors relation and the name attribute.) (2) Consider the next query: SELECT * FROM papers p, authors a, paperauths pa WHERE pa.paperid = p.id AND pa.authid = a.id AND a.name = 'David J. DeWitt'; What is the query plan that PostgreSQL uses for this query? What is the estimated number of returned rows? Briefly explain why PostgreSQL chooses this plan. What is the actual number of rows returned by the query? Explain why the estimated number of rows differs from the actual number of rows returned. (3) Now change the above query to: SELECT * FROM papers p, authors a, paperauths pa WHERE pa.paperid = p.id AND pa.authid = a.id

AND a.name < 'David J. DeWitt'; What is the query plan that PostgreSQL uses for this query? Briefly explain why PostgreSQL chooses this plan. (4) Finally, let us change the query to: SELECT * FROM papers p, authors a, paperauths pa WHERE pa.paperid = p.id AND pa.authid = a.id AND a.name > 'David J. DeWitt' What is the query plan that PostgreSQL uses for this query? Briefly explain why PostgreSQL chooses this plan.