\documentclass{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{geometry}
\usepackage{booktabs}
\usepackage{hyperref}
\geometry{a4paper, margin=1in}
\title{Query Processing Cost Formulas}
\author{}
\date{}
\begin{document}
\maketitle
\tableofcontents
\newpage
% ... [Previous sections] ...
\section{7. Query Plans with Left-Deep Join Trees}
In query optimization, a \textbf{left-deep join tree} is a join tree where the left child of each join node is either a base relation or another join node, and the right child is always a base relation. Left-deep trees are particularly useful because they allow for pipelining and efficient computation of joins by utilizing previously stored information.
\subsection{a. Cost of Left-Deep Tree Joins}
When considering join algorithms under a left-deep tree, the cost formulas account for the reuse of intermediate results and the potential storage of previous computations. The total cost of evaluating a left-deep join tree depends on the sequence of joins, the join algorithms used, and whether intermediate results are materialized or pipelined.
Consider a sequence of (n) joins between relations (R_1, R_2, \dots, R_n). The left-deep join tree evaluates the joins as:
[
(((R_1 \Join R_2) \Join R_3) \dots ) \Join R_n
]
\subsubsection{i. Nested Loop Joins in Left-Deep Trees}
When using Nested Loop Joins (NLJ) in a left-deep tree, the cost of each join depends on whether intermediate results are materialized or pipelined.
\textbf{Materialized Intermediate Results:}
If intermediate results are stored after each join, the cost of the (i)-th join is:
[
\text{Cost}i = \text{NPages}(R{1,\dots,i-1}) + \text{NPages}(R_{1,\dots,i-1}) \times \text{NPages}(R_i)
]
Where:
\begin{itemize}
\item (\text{NPages}(R_{1,\dots,i-1})) is the number of pages of the intermediate result after joining (R_1) to (R_{i-1}).
\item (\text{NPages}(R_i)) is the number of pages in relation (R_i).
\end{itemize}
The total cost is the sum of the costs of all individual joins:
[
\text{Total Cost} = \sum_{i=2}^{n} \text{Cost}_i
]
\textbf{Pipelined Intermediate Results:}
If pipelining is used, intermediate results are not written to disk but passed directly to the next join operation. This reduces disk I/O costs since intermediate results do not need to be materialized. However, sufficient memory is required to hold intermediate tuples.
The cost of the (i)-th join is approximately:
[
\text{Cost}i = \text{NPages}(R{1,\dots,i-1}) + \text{NPages}(R_i)
]
The total cost becomes:
[
\text{Total Cost} = \sum_{i=1}^{n} \text{NPages}(R_i)
]
\subsubsection{ii. Hash Joins in Left-Deep Trees}
Using hash joins in a left-deep tree, assuming intermediate results are materialized, the cost of the (i)-th join is:
[
\text{Cost}i = 3 \times (\text{NPages}(R{1,\dots,i-1}) + \text{NPages}(R_i))
]
The total cost is:
[
\text{Total Cost} = \sum_{i=2}^{n} 3 \times (\text{NPages}(R_{1,\dots,i-1}) + \text{NPages}(R_i))
]
\subsubsection{iii. Sort-Merge Joins in Left-Deep Trees}
For sort-merge joins, the cost of the (i)-th join includes sorting the intermediate result and the next relation, followed by merging:
[
\begin{aligned}
\text{Cost}i &= 2 \times \text{NPages}(R{1,\dots,i-1}) \times \text{num_passes}(R_{1,\dots,i-1}) + 2 \times \text{NPages}(R_i) \times \text{num_passes}(R_i) \
&\quad + (\text{NPages}(R_{1,\dots,i-1}) + \text{NPages}(R_i))
\end{aligned}
]
Total cost:
[
\text{Total Cost} = \sum_{i=2}^{n} \text{Cost}_i
]
\subsection{b. Estimating Intermediate Result Sizes}
To accurately compute the cost of joins in a left-deep tree, it is essential to estimate the sizes of intermediate results.
[
\text{NTuples}(R_{1,\dots,i}) = \text{NTuples}(R_{1,\dots,i-1}) \times \text{NTuples}(R_i) \times \text{RF}_i
]
Where:
\begin{itemize}
\item (\text{NTuples}(R_{1,\dots,i-1})) is the number of tuples in the intermediate result after joining (R_1) to (R_{i-1}).
\item (\text{NTuples}(R_i)) is the number of tuples in relation (R_i).
\item (\text{RF}i) is the reduction factor for the join between (R{1,\dots,i-1}) and (R_i).
\end{itemize}
The number of pages in the intermediate result can be estimated as:
[
\text{NPages}(R_{1,\dots,i}) = \left\lceil \frac{\text{NTuples}(R_{1,\dots,i}) \times \text{TupleSize}_{i}}{\text{PageSize}} \right\rceil
]
\subsection{c. Example Calculation}
Consider joining three relations (R), (S), and (T) using nested loop joins in a left-deep tree, with materialized intermediate results.
\textbf{First Join ((R \Join S)):}
[
\text{Cost}_1 = \text{NPages}(R) + \text{NPages}(R) \times \text{NPages}(S)
]
Estimate the intermediate result:
[
\text{NTuples}(R \Join S) = \text{NTuples}(R) \times \text{NTuples}(S) \times \text{RF}_{R,S}
]
Calculate (\text{NPages}(R \Join S)) using the tuple size and page size.
\textbf{Second Join (((R \Join S) \Join T)):}
[
\text{Cost}_2 = \text{NPages}(R \Join S) + \text{NPages}(R \Join S) \times \text{NPages}(T)
]
Total cost:
[
\text{Total Cost} = \text{Cost}_1 + \text{Cost}_2
]
\subsection{d. Advantages of Left-Deep Join Trees}
\begin{itemize}
\item \textbf{Pipelining:} Enables the use of pipelined execution, reducing the need to write intermediate results to disk.
\item \textbf{Memory Efficiency:} Requires less memory compared to bushy trees due to processing one join at a time.
\item \textbf{Index Utilization:} Easier to utilize indexes on base relations, potentially reducing overall join costs.
\end{itemize}
\subsection{e. Limitations and Considerations}
\begin{itemize}
\item \textbf{Join Order Matters:} The sequence of joins significantly impacts the cost; optimizer must find the optimal join order.
\item \textbf{Intermediate Result Size:} Large intermediate results can increase costs; estimating sizes is critical.
\item \textbf{Memory Constraints:} Pipelining requires sufficient memory to hold intermediate tuples.
\end{itemize}
\subsection{f. General Formula for Left-Deep Join Cost}
Assuming nested loop joins with materialization, the general cost formula for (n) relations is:
[
\text{Total Cost} = \sum_{i=2}^{n} \left( \text{NPages}(R_{1,\dots,i-1}) + \text{NPages}(R_{1,\dots,i-1}) \times \text{NPages}(R_i) \right)
]
If pipelining is possible, the cost reduces to:
[
\text{Total Cost} = \sum_{i=1}^{n} \text{NPages}(R_i)
]
\subsection{g. Incorporating Reduction Factors}
Cumulative reduction factors up to join (i):
[
\text{Total RF}i = \prod{k=2}^{i} \text{RF}_k
]
Intermediate result size after join (i):
[
\text{NTuples}(R_{1,\dots,i}) = \left( \prod_{j=1}^{i} \text{NTuples}(R_j) \right) \times \text{Total RF}_i
]
Number of pages:
[
\text{NPages}(R_{1,\dots,i}) = \left\lceil \frac{\text{NTuples}(R_{1,\dots,i}) \times \text{TupleSize}_i}{\text{PageSize}} \right\rceil
]
\section{8. Conclusion}
Understanding and correctly applying these query processing cost formulas is essential for optimizing database queries. By considering the algorithms used in left-deep join trees and the impact of storing or pipelining intermediate results, database systems can select efficient execution plans. Careful estimation of intermediate result sizes and costs for each join enables better query performance and resource utilization.
\end{document}