[DMDB] Start sorting

This commit is contained in:
2026-07-15 16:46:28 +02:00
parent e005c7608b
commit 1e0ed598aa
6 changed files with 23 additions and 0 deletions
@@ -83,6 +83,7 @@ In addition, the end user does not have to worry about optimization too much. Th
\include{parts/01_sql/main.tex}
\include{parts/02_theory-background/main.tex}
\include{parts/03_systems/main.tex}
\include{parts/04_query-processing/main.tex}
\printGlossary
@@ -0,0 +1,6 @@
\section{Sorting}
Sorting is crucial in DBMS due to the possibility that the user requests sorted data using an \texttt{ORDER BY} clause.
In addtion, it can help speed up some operators.
Something like Quicksort is fast, but creates many random accesses, which slows things down.
Thus, sorting data as it is loaded into memory is beneficial, for which typically (a variant of) merge sort is used.
@@ -0,0 +1,16 @@
\subsection{Merge Sort}
\subsubsection{Merging Sorted Runs}
When we sort each page on load into memory, we then have to merge all pages together, or more precisely, combine elements into pages in correct order.
For that, we keep pointers to each pair of frames, then we first take first element of either the first or second frame, depending on which one comes first in the order,
and copy it into the empty frame. We apply the same again to fill up the empty frame (to a threshold or fully). When the frame is full, we create a second one and link it.
We do this for all pairs, then apply the same procedure to each sorted frame group, repeating this until we have a unified, sorted set of frames.
If there is ever just one frame (group) left, we add an empty one implicitly and proceed normally.
The cost for this is for the 1-page sorted runs (i.e. sorting the original pages) is a single pass (read + write pass).
The total cost is given by $2 \cdot M \cdot N$ I/O operations, with $M$ the number of passes / phases (i.e. how many merge steps there are) and $N$ being the number of frames.
For Two-Way Merge Sort, the number of passes, $M$ is given by $M = \lceil \log_2(N) \rceil + 1$,
so the total cost to sort is $2N(\lceil \log_2 N \rceil + 1)