Materialized views are INSERT triggers
A column-store materialized view is not a periodic refresh — it is a trigger that runs the MV's SELECT against the incoming block only and appends the result to a tiny rollup table, so direct INSERT into the target table silently bypasses it and the rollup drifts.
Skip indexes prune at read time. The bigger lever is to PRE-COMPUTE the answer at write time — turn a billion-row scan into a two-hundred-row scan by materializing the aggregation as data lands.
Scene 11
Materialized views are INSERT triggers
- Watch
- Try it
- Predict
- Capture
An INSERT lands in events. The MV's SELECT runs against the just-arrived 1M-row block and appends ~5 rollup rows to events_by_country_hourly. The dashboard reads the rollup in 2 ms instead of scanning the source for 30 s.
Highlighted lines are the ones running in the diagram right now.
def on_insert_to_source(source, block):writePart(source, block) # immutable part on sourcefor mv in source.attached_mvs: # post-INSERT triggerrollup = mv.select.run(block) # block-only, not whole tablewritePart(mv.target, rollup) # appends to target rollup
def direct_insert_to_target(target, rows):# writes a part on the TARGET tablewritePart(target, rows)# nothing inspects target.attached_mvs — MVs trigger off SOURCE# rollup now contains rows the source has no record ofreturn # silent drift: target > source
def create_mv(source, select_stmt, target, populate=False):mv = Mv(select_stmt, target)source.attached_mvs.append(mv) # active from this moment onif populate: # opt-in one-shot backfillmv.select.run(scan(source)) >> writePart(target)# rows already in `source` were inserted BEFORE mv existed —# they never ran through select_stmt. Backfill by hand:# INSERT INTO target SELECT ... FROM source WHERE ts < cutoffreturn mv
Where this sits in Build a columnar OLAP store (ClickHouse / Druid style)
Scene 11 of 13, in the Patterns act — Materialized views and the OLAP answer to JOIN — denormalize or dictGet.. A column-store MV is not a refresh — it's a trigger over the incoming block. Direct INSERT into the target silently bypasses it and drifts.
Up next. Materialized views collapse one big scan into a tiny one. But the reader's other OLTP instinct — JOIN across normalized tables — hits a different kind of wall on a column store, and the answer feels wrong.
All 13 scenes in Build a columnar OLAP store (ClickHouse / Druid style) · Every curriculum