mirror of
https://github.com/janishutz/eth-summaries.git
synced 2026-09-10 19:15:25 +02:00
[DMDB] Some remarks for SQL
This commit is contained in:
@@ -2,8 +2,9 @@
|
||||
The basic structure of a \sql\ statement is \texttt{SELECT I FROM T WHERE C},
|
||||
which corresponds to $\Pi_{\texttt{I}}(\sigma_{\texttt{C}} \texttt{T})$.
|
||||
|
||||
We can set \texttt{I = *} if we want to return all columns for a table.
|
||||
To rename, we can set \texttt{I = Column as Name, Column2 as Name2}, etc
|
||||
We can set \texttt{I = *} if we want to return all columns for a table, or we can even set \texttt{I = A.*}, for a table \texttt{A} in \texttt{T} called \texttt{A},
|
||||
i.e. with e.g. \texttt{T = Table A}.
|
||||
To rename, we can set \texttt{I = Column AS Name, Column2 AS Name2}, etc
|
||||
|
||||
\inlinetheorem Every SPJR \acrshort{ra} expression can be written in \texttt{SELECT ... FROM ... WHERE ...} form:
|
||||
\begin{tables}{lll}{Operation & Notation & SQL}
|
||||
@@ -36,3 +37,6 @@ To compute time delta, we can use \texttt{DATEDIFF('interval', DateOne, DateTwo)
|
||||
To get just the year from an existing date object (or string), use \texttt{YEAR}. The result is an integer and thus you can use comparison operations with numbers.
|
||||
|
||||
The current date, time, year, etc is provided using \texttt{current\_date} (etc).
|
||||
|
||||
There are also conditional statements, namely \texttt{CASE WHEN condition1 THEN result1 WHEN condition2 THEN result2 ELSE else END}, or of course, more conditions.
|
||||
This can even be used inside aggregations, etc!
|
||||
|
||||
@@ -18,6 +18,7 @@ Using SQL, this is achieved using the \texttt{GROUP BY} clause.
|
||||
We specify the columns on which the grouping should occur.
|
||||
|
||||
\hl{\textbf{\textit{IMPORTANT}}} We can only use aggregates and columns appearing in the \texttt{GROUP BY} clause in the \texttt{SELECT} clause.
|
||||
It is possible to specify multiple grouping columns, the aggregates are computed on the first
|
||||
|
||||
Using a \texttt{JOIN} function, it is of course then possible to go back retrieve the other columns.
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
\subsubsection{Merge Sort}
|
||||
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,
|
||||
For that, we keep pointers to each pair of frames, then we first take the 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.
|
||||
|
||||
@@ -10,7 +10,7 @@ It works as follows:
|
||||
\State Read $B - 1$ pages of relation $R$, store in a heap (priority queue)
|
||||
\For{every page}
|
||||
\If{Top of heap is smaller than the end of the sorted run}
|
||||
\State Continue with next iteration
|
||||
\State Commit it to the run and continue with next iteration
|
||||
\ElsIf{No element is left in memory that is larger than last element of current sorted run}
|
||||
\State create new run
|
||||
\Else
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
Two terms important here are \textit{logical selection}, which describes \bi{what} we want to select and \textit{physical selection},
|
||||
which describes \bi{how} the algorithm or procedure works that actually retrieves, or filters, the data.
|
||||
|
||||
The options include an \textit{file scan}, where we scan the entire file and thus the I/O cost is \cost{$N \div P_F$},
|
||||
The options include a \textit{file scan}, where we scan the entire file and thus the I/O cost is \cost{$N \div P_F$},
|
||||
where $N$ is the number of records in the relation and $P_F$ the number of records per page.
|
||||
Alternatively, we can use \textit{index scan}, where we use an index to retrieve the matching rows.
|
||||
The cost then of course depends on the index used and if said index can even be used to generate the resulsts needed. We will cover that in more detail now.
|
||||
The cost then of course depends on the index used and if said index can even be used to generate the results needed. We will cover that in more detail now.
|
||||
|
||||
Reference in New Issue
Block a user