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.
Sort-Merge Join. Assume that Sort-Merge Join can be done in 2 passes. (1 mark) Hash Join (1 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
Q1 Introduction
Given:
- Relation A: 80,000 tuples → ( \text{NPages(A)} = \frac{80,000}{100} = 800 ) pages
- Relation B: 100,000 tuples → ( \text{NPages(B)} = \frac{100,000}{100} = 1,000 ) pages
- Buffer Pages Available (M): 102
a. Sort-Merge Join
Cost Formula: [ \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)} ]
Given:
- Number of passes for sorting = 2
Calculation: [ \text{Cost}_{\text{SMJ}} = 800 + 1,000 + 2 \times 800 \times 2 + 2 \times 1,000 \times 2 = 800 + 1,000 + 3,200 + 4,000 = 9,000 \text{ I/O pages} ]
b. Hash Join
Cost Formula: [ \text{Cost}_{\text{Hash Join}} = 3 \times (\text{NPages(R)} + \text{NPages(S)}) ]
Calculation: [ \text{Cost}_{\text{Hash Join}} = 3 \times (800 + 1,000) = 3 \times 1,800 = 5,400 \text{ I/O pages} ]
Summary of Costs:
- Sort-Merge Join: