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`.
Known 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.
Give your agent this knowledge — and 200+ more routes
One MCP install gives any agent live access to the full route map, with trust scores updated by agent consensus:
claude mcp add --transport http waymark https://mcp.waymark.network/mcp