mirror of
https://github.com/janishutz/eth-summaries.git
synced 2026-09-10 13:05:24 +02:00
49 lines
3.7 KiB
TeX
49 lines
3.7 KiB
TeX
\newpage
|
|
\subsection{Query Processing}
|
|
\subsubsection{Sorting}
|
|
Given $B$ frames of memory and $N$ records, we have (typically I/Os in pages to be read)
|
|
\begin{itemize}
|
|
\item \bi{Merge Sort}: $2N \cdot P$, with $P = (1 + \ceil{\log_{B - 1}\ceil{N \div B}})$ the number of passes.
|
|
After the first pass, $\ceil{N \div B}$ number of sorted runs were created (typically)
|
|
\item \bi{Replacement Sort / Heap Sort}: $2N \cdot P$, with $P = (1 + \ceil{\log_{B - 1} \ceil{N \div E}})$ the number of passes, with $E$ the number of records per run,
|
|
given by $E = 2B$ in the average case, $E = N$ in the best case (thus single run, data already sorted), $B - 1$ (worst case, reversed data)
|
|
\end{itemize}
|
|
|
|
|
|
\subsubsection{Selection}
|
|
\begin{itemize}
|
|
\item \bi{File Scan}: $N \div P_F$, with $P_F$ the number of pages, because we need to run through all pages
|
|
\item \bi{B+ Tree}: $\texttt{height}(T) - 1 + \texttt{cnt}(P_L) + \texttt{cnt}(P_F)$, $P_L$ the affected leaf pages and $P_F$ the file pages,
|
|
the minus one comes from the fact that we count the leaf page already using $P_L$.
|
|
Note that $\texttt{height}(T) = 1 + \ceil{\log_K(P_L)}$, with $K$ the number of elements in the inner nodes
|
|
\begin{itemize}
|
|
\item For a clustered index, $\texttt{cnt}(P_F) = \texttt{cnt}(R') \div (F_L \cdot R_L)$, with $R_L$ the number of records per leaf page and $F_L$ the fill factor.
|
|
\item For an unclustered index, $\texttt{cnt}(P_F) = \texttt{cnt}(R')$, with $\texttt{cnt}(R') = \texttt{cnt}(R) \cdot S$, with $S$ the selectivity of the predicate.
|
|
\end{itemize}
|
|
\item \bi{Hash Index}: Cost is $1 + \texttt{cnt}(R') \div R_L + \texttt{cnt}(R')$, where the $1$ is to do the lookup,
|
|
$\texttt{cnt}(R') \div R_L$ to fetch the row IDs and $\texttt{cnt}(R')$ to fetch the records
|
|
(this is an up-to, it is $\texttt{cnt}(R') \div R_F$ as minimum, for clustered index), with $R_L$ the number of records per Leaf and $R_F$ the number of records per file.
|
|
\end{itemize}
|
|
|
|
|
|
\subsubsection{Projection}
|
|
Remember that we need the sort-based and hash-based approach to eliminate duplicates.
|
|
\begin{itemize}
|
|
\item \bi{Partitioning}: $\texttt{Cost}_\texttt{part}(R) = \texttt{cnt}(R) + \texttt{cnt}(R')$ with
|
|
$\texttt{cnt}(R') = \texttt{cnt}(R) \cdot Q$, with $Q$ the fraction of selected attributes divided by total attributes
|
|
\item \bi{Sort-Based}: The number of sorted runs are $M = \ceil{\texttt{cnt}(R') \div B}$, with the merge passes $P = 1 + \ceil{\log_{B - 1}(M)}$, total cost:
|
|
$2 \cdot N \cdot P + \texttt{Cost}_\texttt{part}(R)$
|
|
\item \bi{Hash-Based}:
|
|
\begin{itemize}
|
|
\item \bi{Minimum $B$ for $M$-pass}: $B > \sqrt{\texttt{cnt}(R') \cdot f}$, with $f$ the Minister of Magic Factor (for Books $\leq$ 5),
|
|
aka. Fudge Factor, typically $f \approx 1.2$.
|
|
\item \bi{Below threshold}: To check if we need recursive partitioning, we use the un-approximated version of the above: $\frac{f \cdot \texttt{cnt}(R')}{B - 1} < B$
|
|
\item \bi{Cost}: The base cost is $\texttt{Cost}_\texttt{part}(R) + \texttt{cnt}(R')$ for partitioning and duplicate elimination later on
|
|
\begin{itemize}
|
|
\item If below threshold, that's our cost
|
|
\item If above threshold, then we need to repartition, which costs $2 \cdot \texttt{cnt}(R')$ (once each for write and read) for each time we do this action.
|
|
\end{itemize}
|
|
\end{itemize}
|
|
\end{itemize}
|
|
Note that both the sort- and hash-based approaches perform approximately equally well at larger $B$, since both algorithms need two passes over the data.
|