April 1, 1921
CREATE VIEW salesSinceLastMonth AS
SELECT l.*
FROM lineitem l, orders o
WHERE l.orderkey = o.orderkey
AND o.orderdate > DATE(NOW() - '1 Month')
SELECT partkey FROM salesSinceLastMonth
ORDER BY shipdate DESC LIMIT 10;
SELECT suppkey, COUNT(*)
FROM salesSinceLastMonth
GROUP BY suppkey;
SELECT partkey, COUNT(*)
FROM salesSinceLastMonth
GROUP BY partkey;
Opportunity: Views exist to be queried frequently
Idea: Pre-compute and save the view’s contents!
(like an index)
When the base data changes,
the view needs to be updated too!
Our view starts off initialized
Idea: Recompute the view from scratch when data changes.
Not quite facepalm-worthy, but not ideal.
$\Delta Q$ | (ideally) Small & fast query |
$+$ | (ideally) Fast "merge" operation |
$+$ is like $\uplus$
$\bowtie$ is like $\times$
Are these types of patterns common?
Semiring: $\left< \mathbb S, +, \times, \mathbb 0, \mathbb 1\right>$
Any set of 'things' $\mathbb S$ such that...
"Closed" $$S_i + S_j = S_k$$ $$S_i \times S_j = S_k$$ |
Additive, Multiplicative Identities $$S_i + 0 = S_i$$ $$S_i \times 1 = S_i$$ $$S_i \times 0 = 0$$ |
$$S_i \times (S_j + S_k) = (S_i \times S_j) + (S_j \times S_k)$$
Addition distributes over multiplication |
Ring: $\left< \mathbb S, +, \times, \mathbb 0, \mathbb 1, -\right>$
Any semiring with an additive inverse....
$$S_i + (-S_i) = 0$$Insertions
Deletions
Updates
A Set/Bag of Insertions
+
A Set/Bag of Deletions
$\mathcal R$ | $+$ | $\Delta \mathcal R$ |
---|---|---|
A Set/Bag | "+" | A Set/Bag of Insertions A Set/Bag of Deletions |
$\mathcal R$ | $\cup$/$\uplus$
$-$ |
$\Delta \mathcal R_{ins}$
$\Delta \mathcal R_{del}$ |
... this feels a bit wrong
$\texttt{VIEW} \leftarrow \texttt{VIEW} + $$\Delta Q(\mathcal D, \Delta \mathcal D)$
Given $Q(\mathcal R, \mathcal S, \ldots)$
Construct $\Delta Q(\mathcal R, \Delta \mathcal R, \mathcal S, \Delta \mathcal S, \ldots)$
$\sigma(\mathcal R) \rightarrow \sigma(\mathcal R \uplus \Delta \mathcal R)$
$ \equiv $ $\sigma(\mathcal R)$ $ \uplus $ $\sigma(\Delta \mathcal R)$
$Q(\mathcal D) = \sigma(\mathcal R)$
$\Delta Q(\mathcal D, \Delta \mathcal D) = \sigma(\Delta \mathcal R)$
Set/Bag difference also commutes through selection
$\pi(\mathcal R) \rightarrow \pi(\mathcal R \uplus \Delta \mathcal R)$
$ \equiv $ $\pi(\mathcal R)$ $ \uplus $ $\pi(\Delta \mathcal R)$
$Q(\mathcal D) = \pi(\mathcal R)$
$\Delta Q(\mathcal D, \Delta \mathcal D) = \pi(\Delta \mathcal R)$
Does this work under set semantics?
$\mathcal R_1 \uplus \mathcal R_2 \rightarrow \mathcal R_1 \uplus \Delta \mathcal R_1 \uplus \mathcal R_2 \uplus \Delta \mathcal R_2$
$ \equiv $ $\mathcal R_1 \uplus \mathcal R_2$ $ \uplus $ $\Delta \mathcal R_1 \uplus \Delta \mathcal R_2$
$Q(\mathcal D) = \mathcal R_1 \uplus \mathcal R_2$
$\Delta Q(\mathcal D, \Delta \mathcal D) = \Delta \mathcal R_1 \uplus \Delta \mathcal R_2$
So far: $$\Delta Q_{ins}(\mathcal D, \Delta \mathcal D) = Q(\Delta \mathcal Q_{ins})$$ $$\Delta Q_{del}(\mathcal D, \Delta \mathcal D) = Q(\Delta \mathcal Q_{del})$$
... but what if $\mathcal R$ and $\mathcal S$ both change
$$\{1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 4, 4, 4, 4, 4, 4, 5\}$$
(not compact)
$$\{1 \rightarrow \times 3; 2 \rightarrow \times 5; 3 \rightarrow \times 2; 4 \rightarrow \times 6; 5 \rightarrow \times 1\}$$
Multiset representation: Tuple $\rightarrow$ # of occurrences
multiplicity
Insertions = Positive Multiplicity
Deletions = Negative Multiplicity
+ = Bag/Multiset Union
$\{ A \rightarrow 1, B \rightarrow 2 \} \uplus \{ B \rightarrow 2, C \rightarrow 4 \} = $
$=\{$ $A \rightarrow 1,$ $B \rightarrow 5,$ $C \rightarrow 4$ $\}$
$\{ A \rightarrow 1 \} \uplus \{ A \rightarrow -1 \}$ $= \{A \rightarrow 0\}$ $ = \{\}$
$\{ A \rightarrow 1, B \rightarrow 3 \} \times \{ C \rightarrow 4 \} $
$= \{ \left< A,C \right> \rightarrow$ $4$ $\left< B,C \right> \rightarrow$ $12$ $\}$
$\pi_{\text{1st attr}}\{ \left< A,X \right> \rightarrow 1, \left< A,Y \right> \rightarrow 2, \left< B,Z \right> \rightarrow 5, \}$
$= \{$ $A \rightarrow 1, A \rightarrow 2,$ $B \rightarrow 5\}$
$= \{A \rightarrow 3, B \rightarrow 5\}$
$\sigma_{A}\{ A \rightarrow 1, B \rightarrow 5, C \rightarrow 3 \}$
$= \{ A \rightarrow 1, B \rightarrow 0, C \rightarrow 0 \}$
$= \{ A \rightarrow 1\}$