Assignment3 Sol

  • Upload
    x

  • View
    9

  • Download
    0

Embed Size (px)

DESCRIPTION

awdawd

Citation preview

  • Exercise 13.2 Answer Exercise 13.1 assuming that a two-way external sort is used. Answer 13.2 The answer to each question is given below.

    1. (3 points) In the first pass (Pass 0), N runs of 1 page each are produced, where N is the number of file pages:(a) 10000 sorted runs.(b) 20000 sorted runs. (c) 2000000 sorted runs.

    2. (3 points) The number of passes required to sort the file completely, including the initial sorting pass, is log2N 1 + 1, where N 1 = N is the number of runs produced by Pass 0:(a) log210000 + 1 = 15 passes. (b) log220000 + 1 = 16 passes. (c) log22000000 + 1 = 22 passes.

    3. (3 points) Since each page is read and written once per pass, the total number of page I/Os for sorting the file is 2 N (#passes):(a) 2*10000*15 = 300000.(b) 2*20000*16 = 640000. (c) 2*2000000*22 = 88000000.

    4. (2 points) Using 2-way merge sort, it is impossible to sort these files in 2 passes. Additional buffer pages do not help, since the algorithm always uses just 3 buffer pages.

    Answer 14.4 Let M = 1000 be the number of pages in R, N = 200 be the number

    of pages in S, and B = 52 be the number of buffer pages available.

    1. (2 points) Basic idea is to read each page of the outer relation, and for each page scan the inner relation for matching tuples. Total cost would be #pages in outer + (#pages in outer #pages in inner) which is minimized by having the smaller relation be the outer relation. TotalCost = N + (N M ) = 200, 200 The minimum number of buffer pages for this cost is 3.

    2. (2 points) This time read the outer relation in blocks, and for each block scan the inner relation for matching tuples. So the outer relation is still read once, but the inner relation is scanned only once for each outer block, of which there are #pages in the outer/(B-2)

  • = 200/50 = 4.

    TotalCost=N+MN/(B-2)=4,200

    If the number of buffer pages is less than 52, the number of scans of the inner would be more than 4 since = 200/49 is 5. The minimum number of buffer pages for this cost is therefore 52.

    3. (2 points) Since B > M > N we can use the refinement to Sort-Merge discussed on pages 254-255 in the text. TotalCost = 3 (M + N) = 3,600

    The minimum number of buffer pages required is 25. With 25 buffer pages, the initial sorting pass will split R into 20 runs of size 50 and split S into 4 runs of size 50 (approximately). These 24 runs can then be merged in one pass, with one page left over to be used as an output buffer. With fewer than 25 buffer pages the number of runs produced by the first pass over both relations would exceed the number of available pages, making a one-pass merge impossible.

    4. (2 points) The cost of Hash Join is 3(M+N) if B > sqrt(fN) where f is a fudge factor used to capture the small increase in size involved in building a hash table, and N is the number of pages in the smaller relation, S. We will also assume uniform partitioning from our hash function. Assuming that this condition is met, the TotalCost = 3 (M + N) = 3,600. The minimum number of buffer pages for this cost is sqrt(fN).

    5. (2 points) The optimal cost would be achieved if each relation was only read once. We could do such a join by storing the entire smaller relation in memory, reading in the larger relation page-by-page, and for each tuple in the larger relation we search the smaller relation (which exists entirely in memory) for matching tuples. The buffer pool would have to hold the entire smaller relation, one page for reading in the larger relation, and one page to serve as an output buffer. T otalCost = M + N = 1, 200The minimum number of buffer pages for this cost is N + 1 + 1 = 202.

    6. (2 points) Any tuple in R can match at most one tuple in S because S.b is a primary key (which means the S.b field contains no duplicates). So the maximum number of tuples in the result is equal to the number of tuples in R, which is 10,000. The size of a tuple in the result could be as large as the size of an R tuple plus the size of an S tuple (minus the size of the shared attribute). This may allow only 5

  • tuples to be stored on a page. Storing 10,000 tuples at 5 per page would require 2000 pages in the result.

    7. (3 points) The foreign key constraint tells us that for every R tuple there is exactly one matching S tuple (because S.b is a key). The Sort-Merge and Hash Joins would not be affected, but we could reduce the cost of the two Nested Loops joins. If we make R the outer relation then for each tuple of R we only have to scan S until a match is found. This will require scanning only 50% of S on average. For Page-Oriented Nested Loops, the new cost would be TotalCost = M + (M N/2) = 101, 000 and 3 buffer pages are still required.For Block Nested Loops, the new cost would be TotalCost=M+(N/2)M/(B-2)=3,000 and again this cost can only be achieved with 52 available buffer pages.

    Answer 15.4 1. (a) (2 points) The best plan, a B+ tree search, would involve using the B+ tree to find the first title index such that title=CFO, cost = 2. Then, due to the clustering of the index, the relation pages can be scanned from that indexs reference, cost = 10000 * 10% + 2500 * 10% ( Scanning the index ). 1000 + 250 + 2 = 1252 ( total cost ) .

    . (b) (2 points) An unclustered index would preclude the low cost of the previous plan and necessitate the choice of a simple filescan, cost = 10000, as the best.

    . (c) (2 points) Due to the WHERE clause, the clustered B+ index on ename doesnt help at all. The best alternative is to use a filescan, cost = 10000.

    . (d) (2 points) Again, as in the previous answer, the best choice is a filescan, cost = 10000.

    . (e) (2 points) Although the order of the B+ index key makes the tree much less useful, the leaves can still be scanned in an index-only scan, and the increased number of tuples per page lowers the I/O cost. Cost = 10000 * .5 = 5000.

    2. (a) (2 points) A clustered index on title would allow scanning of only the 10% of the tuples desired. Thus the total cost is 2 (lookup) + 10000 * 10% + 2500 * 10%= 1252.

    . (b) (2 points) A clustered index on dname works functionally in the same manner as that in the previous question, for a cost 1002 + 250 = 1252 . The ename field still must be retrieved from the relation data pages.

    . (c) (2 points) In this case, using the index lowers the cost of the query slightly, due to the greater selectivity of the combined query and to the search key taking

  • advantage of it. The total cost = 2 (look up) + 10000 * 5% + 5000 * 5% =752.

    (d) (2 points) Although this index does contain the output field, the dname still must be retrieved from the relational data pages, for a cost of 2 (lookup) + 10000 * 10% + 5000 * 10%= 1502.

    (e) (2 points) Since this index contains all three indexes needed for an index-only scan, the cost drops to 2 (look up) + 10000 * 5% * .75 (smaller size) = 402.

    (f) (2 points) Finally, in this case, the prefix cannot be matched with the equality informa- tion in the WHERE clause, and thus a scan would be the superior method of retrieval. However, as the clustered B+ trees index contains all the indexes needed for the query and has a smaller tuple, scanning the leaves of the B+ tree is the best plan, costing 10000 * .75 = 7500 I/Os.