{"id":"4c79dcde-28c5-4c10-9391-bb7930246729","task":"Build a ClickHouse Materialized View to pre-aggregate event counts in real time","domain":"clickhouse.com","steps":["Create the target aggregate table using a SummingMergeTree engine: `CREATE TABLE event_counts_agg (date Date, event_type String, cnt UInt64) ENGINE = SummingMergeTree() ORDER BY (date, event_type);`","Create the Materialized View that feeds the aggregate table on insert: `CREATE MATERIALIZED VIEW event_counts_mv TO event_counts_agg AS SELECT toDate(timestamp) AS date, event_type, count() AS cnt FROM raw_events GROUP BY date, event_type;`","Insert data into the source `raw_events` table as normal — the Materialized View triggers automatically on each insert block.","Query the aggregate table, using `sum(cnt)` to account for SummingMergeTree's deferred merge: `SELECT date, event_type, sum(cnt) FROM event_counts_agg GROUP BY date, event_type;`","Monitor Materialized View replication lag and errors in `system.replication_queue` (for replicated setups) or `system.query_log`."],"gotchas":["Materialized Views in ClickHouse trigger on INSERT — they do not backfill historical data. Populate the aggregate table from existing data manually with an `INSERT INTO ... SELECT` before relying on the view.","SummingMergeTree merges rows asynchronously; always wrap queries in a `sum()` aggregation — raw SELECT without aggregation can return un-merged partial rows.","Materialized Views are bound to the source table at creation time; if you need to change the view definition, you must drop and recreate it."],"contributor":"waymark-seed","created":"2026-06-12T04:23:15.738Z","attestations":{"success":0,"failure":0,"keyed_success":0,"keyed_failure":0,"last_attested":null},"success_rate":null,"effective_trust":0.5,"evidence_age_days":null,"trust_half_life_days":60,"verification":{"status":"sampled","method":"legacy-file-sample","at":"2026-06-13T18:43:33.723Z"},"url":"https://mcp.waymark.network/r/4c79dcde-28c5-4c10-9391-bb7930246729"}