1. Nested Loops Join (NLJ)
a. Tuple-oriented NLJ
Cost=NPages(R)+NTuples(R)×NPages(S)\text{Cost} = NPages(R) + NTuples(R) \times NPages(S)Cost=NPages(R)+NTuples(R)×NPages(S)
b. Page-oriented NLJ
Cost=NPages(R)+(NPages(R)×NPages(S))\text{Cost} = NPages(R) + (NPages(R) \times NPages(S))Cost=NPages(R)+(NPages(R)×NPages(S))
c. Block-oriented NLJ (BONLJ)
Cost={NPages(R)+NPages(S)if B≥NPages(R)+2NPages(R)+⌈NPages(R)B−2⌉×NPages(S)otherwise\text{Cost} = \begin{cases} NPages(R) + NPages(S) & \text{if } B \geq NPages(R) + 2 \\ NPages(R) + \left\lceil \frac{NPages(R)}{B - 2} \right\rceil \times NPages(S) & \text{otherwise} \end{cases}Cost={NPages(R)+NPages(S)NPages(R)+⌈B−2NPages(R)⌉×NPages(S)if B≥NPages(R)+2otherwise Cost={NPages(R)+NPages(S)if B≥NPages(R)+2NPages(R)+⌈NPages(R)B−2⌉×NPages(S)otherwise\text{Cost} = \begin{cases} NPages(R) + NPages(S) & \text{if } B \geq NPages(R) + 2 \\ NPages(R) + \left\lceil \frac{NPages(R)}{B - 2} \right\rceil \times NPages(S) & \text{otherwise} \end{cases}Cost={NPages(R)+NPages(S)NPages(R)+⌈B−2NPages(R)⌉×NPages(S)if B≥NPages(R)+2otherwise
 
2. Hash Join
The cost depends on whether the buffer size can hold the entire outer relation RRR:
a. Sufficient Buffer (B>NPages(R)B > NPages(R)B>NPages(R))
Cost=2×(NPages(R)+NPages(S))\text{Cost} = 2 \times (NPages(R) + NPages(S))Cost=2×(NPages(R)+NPages(S))
b. Insufficient Buffer (B≤NPages(R)B \leq NPages(R)B≤NPages(R))
Cost=3×(NPages(R)+NPages(S))\text{Cost} = 3 \times (NPages(R) + NPages(S))Cost=3×(NPages(R)+NPages(S))
 
3. Sort-Merge Join (SMJ)
CostSMJ=NPages(R)+NPages(S)+2×NPages(R)×num_passes(R)+2×NPages(S)×num_passes(S)\text{Cost}_{SMJ} = NPages(R) + NPages(S) + 2 \times NPages(R) \times \text{num\_passes}(R) + 2 \times NPages(S) \times \text{num\_passes}(S)CostSMJ=NPages(R)+NPages(S)+2×NPages(R)×num_passes(R)+2×NPages(S)×num_passes(S)
Assumption: The number of passes for sorting is predetermined (e.g., 2 passes).
 
4. Selecting the Optimal Join Strategy
To ensure the most efficient join operation, always select the join strategy with the minimum cost based on the available buffer size BBB.
Optimal Cost=min⁡(CostTuple-NLJ,CostPage-NLJ,CostBlock-NLJ,CostHash Join,CostSMJ)\text{Optimal Cost} = \min(\text{Cost}_{\text{Tuple-NLJ}}, \text{Cost}_{\text{Page-NLJ}}, \text{Cost}_{\text{Block-NLJ}}, \text{Cost}_{\text{Hash Join}}, \text{Cost}_{SMJ})Optimal Cost=min(CostTuple-NLJ,CostPage-NLJ,CostBlock-NLJ,CostHash Join,CostSMJ)
Note: Incorporate conditional logic to evaluate which join strategy yields the lowest cost based on BBB.
 
Revised Steps Incorporating Buffer Size and Optimal Strategy Selection
1.	Incorporate Buffer Size in All Join Formulas
o	Always factor in the number of available buffer pages BBB when calculating join costs.
o	Buffer size directly influences the efficiency of join algorithms, especially Hash Join, Sort-Merge Join, and Block-oriented NLJ.
2.	Use Conditional Formulas Based on Buffer Availability
o	Hash Join:
	Sufficient Buffer (B>NPages(R)B > NPages(R)B>NPages(R)): Cost=2×(NPages(R)+NPages(S))\text{Cost} = 2 \times (NPages(R) + NPages(S))Cost=2×(NPages(R)+NPages(S))
	Insufficient Buffer (B≤NPages(R)B \leq NPages(R)B≤NPages(R)): Cost=3×(NPages(R)+NPages(S))\text{Cost} = 3 \times (NPages(R) + NPages(S))Cost=3×(NPages(R)+NPages(S))
o	Sort-Merge Join:
	The number of passes required for sorting depends on buffer size.
	More buffer pages can reduce the number of passes, thereby reducing I/O costs.
o	Block-oriented NLJ:
	Sufficient Buffer (B≥NPages(R)+2B \geq NPages(R) + 2B≥NPages(R)+2): Cost=NPages(R)+NPages(S)\text{Cost} = NPages(R) + NPages(S)Cost=NPages(R)+NPages(S)
	Otherwise: Cost=NPages(R)+⌈NPages(R)B−2⌉×NPages(S)\text{Cost} = NPages(R) + \left\lceil \frac{NPages(R)}{B - 2} \right\rceil \times NPages(S)Cost=NPages(R)+⌈B−2NPages(R)⌉×NPages(S)
3.	Validate Underlying Assumptions Before Applying Formulas
o	Check Relation Sizes: Ensure that the size of the outer relation RRR relative to buffer size BBB is considered.
o	Data Distribution: Be aware of how data is distributed across pages, as skewed data can affect join performance.
o	Index Availability: Even if no indexes are present, always verify if alternative access methods can be utilized.
4.	Understand the Mechanics of Each Join Algorithm
o	Nested Loops Join (NLJ): Simple but can be expensive; performance varies with the choice of outer relation and join type (tuple, page, block-oriented).
o	Hash Join: Efficient for equijoins; buffer size plays a critical role in determining the number of passes.
o	Sort-Merge Join (SMJ): Effective for large datasets, especially when both relations are sorted or can be sorted efficiently.
5.	Thoroughly Analyze Each Step in the Join Process
o	For Hash Join:
	Build Phase: Reading and hashing the outer relation.
	Probe Phase: Scanning the inner relation and probing the hash table.
	Partitioning (if needed): Additional I/O if the outer relation doesn't fit in memory.
o	For Sort-Merge Join:
	Sorting Phase: Number of passes required based on buffer size.
	Merging Phase: Sequentially merging the sorted relations.
o	For Block-oriented NLJ:
	Reading Blocks: Efficiently reading blocks of the outer relation.
	Probing Inner Relation: Scanning the inner relation for matching tuples.
6.	Double-Check Calculations and Assumptions
o	Re-validate Calculations: Ensure that all arithmetic operations align with the formulas.
o	Cross-Verify with Theoretical Expectations: Compare calculated costs with theoretical benchmarks to identify discrepancies.
7.	Document Assumptions and Decisions
o	Clearly State Assumptions: E.g., number of passes, buffer allocations.
o	Document Decisions: Like choosing one join strategy over another based on cost estimations.
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. 
1.	Page-oriented Nested Loops Join. Consider A as the outer relation. (1 mark)
2.	Block-oriented Nested Loops Join. Consider A as the outer relation. (1 mark)
3.	Sort-Merge Join. Assume that Sort-Merge Join can be done in 2 passes. (1 mark)
4.	Hash Join (1 mark)
5.	Assuming that no indexes are built on any of the two relations, and assuming that sufficient buffer space is available: 
A.	What would be the lowest possible cost to perform this query ? (0.5 mark)
B.	What would be the minimum buffer size required to achieve this cost from 5A? (0.5 mark)
 
Flag question: Question 1 
Question 1 1 pts 
Q1.1
Page-oriented Nested Loops Join. Consider A as the outer relation. (1 mark) 
[do not give any decimals]
 
Flag question: Question 2 
Question 2 1 pts 
Q1.2
Block-oriented Nested Loops Join. Consider A as the outer relation. (1 mark)
[do not give any decimals]
 
Flag question: Question 3 
Question 3 1 pts 
Q1.3
Sort-Merge Join. Assume that Sort-Merge Join can be done in 2 passes. (1 mark)
[do not give any decimals]
 
Flag question: Question 4 
Question 4 1 pts 
Q1.4
Hash Join (1 mark)
[do not give any decimals]
 
Flag question: Question 5 
Question 5 0.5 pts 
Q1.5A
Assuming that no indexes are built on any of the two relations, and assuming that sufficient buffer space is available:
1.	
A.	What would be the lowest possible cost to perform this query ? (0.5 mark)
[do not give any decimals]
 
Flag question: Question 6 
Question 6 0.5 pts 
Q1.5B
 Assuming that no indexes are built on any of the two relations, and assuming that sufficient buffer space is available:
       B. What would be the minimum buffer size required to achieve this cost from Q1.5A? (0.5 mark)
[do not give any decimals]
Solve questions 1 through to 6 without fail.