5. Joins Estimating the cost of different join algorithms between two relations, R…
- User
- Joins Estimating the cost of different join algorithms between two relations, R (outer) and S (inner). a. Nested Loop Join (NLJ) i. Tuple-Oriented Nested Loop Join [ \text{Cost} = \text{NPages(R)} + \text{NTuples(R)} \times \text{NPages(S)} ] • Explanation: Read all pages of R, then for each tuple in R, scan all pages of S. ii. Page-Oriented Nested Loop Join [ \text{Cost} = \text{NPages(R)} + \text{NPages(R)} \times \text{NPages(S)} ] • Explanation: Read all pages of R, then for each page in R, scan all pages of S. iii. Block-Oriented Nested Loop Join (Block Size B) [ \text{Cost} = \text{NPages(R)} + \left\lceil \frac{\text{NPages(R)}}{\text{B} - 2} \right\rceil \times \text{NPages(S)} ] • Explanation: Utilizes block I/O. B represents the number of blocks that can be loaded into memory. Reduces the number of scans on S by processing multiple pages of R simultaneously. b. Hash Join [ \text{Cost} = 3 \times (\text{NPages(R)} + \text{NPages(S)}) ] • Explanation: Involves building and probing hash tables, typically requiring three passes over both relations. c. Sort-Merge Join [ \text{Cost}_{\text{SMJ}} = \text{NPages(R)} + \text{NPages(S)} + 2 \times \text{NPages(R)} \times \text{num_passes(R)} + 2 \times \text{NPages(S)} \times \text{num_passes(S)} ] • Explanation: Costs include reading both relations, sorting them (each requires num_passes passes), and then merging.
Q1 Introduction (Total marks for Q1: 5 marks)
Consider two relations A and B. A has 80,000 tuples, and B has 100,000 tuples. Both relations store 100 tuples per page. Consider the following SQL statement:
SELECT *
FROM A INNER JOIN B
ON A.a = B.a;We wish to evaluate an equijoin between A and B, with an equality condition A.a = B.a. There are 102 buffer pages available for this operation. Both relations are stored as (unsorted) heap files. Neither relation has any indexes built on it.
Consider the alternative join strategies described below and calculate the cost of each alternative. Evaluate the algorithms using the number of disk I/O's (i.e., pages) as the cost.
Assuming that no indexes are built on any of the two relations, and assuming that sufficient buffer space is available: What would be the lowest possible cost to perform this query ? (0.5 mark) What would be the minimum buffer size required to achieve this cost from 5A? (0.5 mark)NOTE WHEN SOLVING THE ABOVE YOU MUST USE THE FORMULAS PROVIDED ABOVE WHERE APPLICABLE WITHOUT FAIL AS THEY WILL NET THE CORRECT ANSWER
- Assistant
To determine the most efficient way to perform the equijoin between relations A and B, we'll evaluate the join algorithms based on the provided formulas.
Given:
- Relation A: 80,000 tuples → NPages(A) = 800 pages
- Relation B: 100,000 tuples → NPages(B) = 1,000 pages
- Buffer Pages Available: 102
- Join Type: Equijoin on attribute
a(A.a = B.a) - No Indexes: Both relations are unsorted heap files.
Q1 Introduction
a. Lowest Possible Cost
The Hash Join is generally the most efficient for equijoins without indexes when sufficient buffer space is available. The cost formula for a Hash Join is: