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.
ΔQ | (ideally) Small & fast query |
+ | (ideally) Fast "merge" operation |
+ is like ⊎
⋈ is like ×
Are these types of patterns common?
Semiring: ⟨S,+,×,0,1⟩
Any set of 'things' S such that...
"Closed" Si+Sj=Sk Si×Sj=Sk |
Additive, Multiplicative Identities Si+0=Si Si×1=Si Si×0=0 |
Si×(Sj+Sk)=(Si×Sj)+(Sj×Sk)
Addition distributes over multiplication |
Ring: ⟨S,+,×,0,1,−⟩
Any semiring with an additive inverse....
Si+(−Si)=0Insertions
Deletions
Updates
A Set/Bag of Insertions
+
A Set/Bag of Deletions
R | + | ΔR |
---|---|---|
A Set/Bag | "+" | A Set/Bag of Insertions A Set/Bag of Deletions |
R | ∪/⊎
− |
ΔRins
ΔRdel |
... this feels a bit wrong
VIEW←VIEW+ΔQ(D,ΔD)
Given Q(R,S,…)
Construct ΔQ(R,ΔR,S,ΔS,…)
σ(R)→σ(R⊎ΔR)
≡ σ(R) ⊎ σ(ΔR)
Q(D)=σ(R)
ΔQ(D,ΔD)=σ(ΔR)
Set/Bag difference also commutes through selection
π(R)→π(R⊎ΔR)
≡ π(R) ⊎ π(ΔR)
Q(D)=π(R)
ΔQ(D,ΔD)=π(ΔR)
Does this work under set semantics?
R1⊎R2→R1⊎ΔR1⊎R2⊎ΔR2
≡ R1⊎R2 ⊎ ΔR1⊎ΔR2
Q(D)=R1⊎R2
ΔQ(D,ΔD)=ΔR1⊎ΔR2
So far: ΔQins(D,ΔD)=Q(ΔQins) ΔQdel(D,ΔD)=Q(ΔQdel)
... but what if R and S both change
{1,1,1,2,2,2,2,2,3,3,4,4,4,4,4,4,5}
(not compact)
{1→×3;2→×5;3→×2;4→×6;5→×1}
Multiset representation: Tuple → # of occurrences
multiplicity
Insertions = Positive Multiplicity
Deletions = Negative Multiplicity
+ = Bag/Multiset Union
{A→1,B→2}⊎{B→2,C→4}=
={ A→1, B→5, C→4 }
{A→1}⊎{A→−1} ={A→0} ={}
{A→1,B→3}×{C→4}
={⟨A,C⟩→ 4 ⟨B,C⟩→ 12 }
π1st attr{⟨A,X⟩→1,⟨A,Y⟩→2,⟨B,Z⟩→5,}
={ A→1,A→2, B→5}
={A→3,B→5}
σA{A→1,B→5,C→3}
={A→1,B→0,C→0}
={A→1}