From f5815c3e72f511bfbb37162d38fc2bf7a98f1af2 Mon Sep 17 00:00:00 2001 From: Jakub Novak Date: Mon, 13 Apr 2026 13:06:30 +0000 Subject: [PATCH 1/5] chore(clickhouse): add projection for events table --- ...080000_add_sandbox_events_team_projection.sql | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 packages/clickhouse/migrations/20260413080000_add_sandbox_events_team_projection.sql diff --git a/packages/clickhouse/migrations/20260413080000_add_sandbox_events_team_projection.sql b/packages/clickhouse/migrations/20260413080000_add_sandbox_events_team_projection.sql new file mode 100644 index 0000000000..195c395ccf --- /dev/null +++ b/packages/clickhouse/migrations/20260413080000_add_sandbox_events_team_projection.sql @@ -0,0 +1,16 @@ +-- +goose Up +-- Add a projection ordered by (sandbox_team_id, sandbox_id, timestamp) so that +-- queries filtering by sandbox_team_id can skip the full table scan. +-- The base ORDER BY is (sandbox_id, timestamp) which doesn't help when only +-- sandbox_team_id is provided. +ALTER TABLE sandbox_events_local + ADD PROJECTION proj_team_id ( + SELECT * ORDER BY sandbox_team_id, sandbox_id, timestamp + ); + +ALTER TABLE sandbox_events_local + MATERIALIZE PROJECTION proj_team_id; + +-- +goose Down +ALTER TABLE sandbox_events_local + DROP PROJECTION IF EXISTS proj_team_id; From a8496f2521c27a8cca6c191167de346991e1a432 Mon Sep 17 00:00:00 2001 From: Jakub Novak Date: Mon, 13 Apr 2026 13:28:26 +0000 Subject: [PATCH 2/5] chore: add comment --- .../20260413080000_add_sandbox_events_team_projection.sql | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/clickhouse/migrations/20260413080000_add_sandbox_events_team_projection.sql b/packages/clickhouse/migrations/20260413080000_add_sandbox_events_team_projection.sql index 195c395ccf..c04c358e97 100644 --- a/packages/clickhouse/migrations/20260413080000_add_sandbox_events_team_projection.sql +++ b/packages/clickhouse/migrations/20260413080000_add_sandbox_events_team_projection.sql @@ -1,8 +1,10 @@ -- +goose Up --- Add a projection ordered by (sandbox_team_id, sandbox_id, timestamp) so that --- queries filtering by sandbox_team_id can skip the full table scan. +-- TEMPORARY: Add a projection ordered by (sandbox_team_id, sandbox_id, timestamp) +-- so that queries filtering by sandbox_team_id can skip the full table scan. -- The base ORDER BY is (sandbox_id, timestamp) which doesn't help when only -- sandbox_team_id is provided. +-- This projection should be removed once we migrate the table's ORDER BY to +-- (sandbox_team_id, sandbox_id, timestamp) via a CREATE + RENAME swap. ALTER TABLE sandbox_events_local ADD PROJECTION proj_team_id ( SELECT * ORDER BY sandbox_team_id, sandbox_id, timestamp From aa504ef8674bc015af02173993406c920e3b96af Mon Sep 17 00:00:00 2001 From: Jakub Novak Date: Mon, 13 Apr 2026 13:29:36 +0000 Subject: [PATCH 3/5] chore: add if not exists --- .../20260413080000_add_sandbox_events_team_projection.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/clickhouse/migrations/20260413080000_add_sandbox_events_team_projection.sql b/packages/clickhouse/migrations/20260413080000_add_sandbox_events_team_projection.sql index c04c358e97..b19b2ce374 100644 --- a/packages/clickhouse/migrations/20260413080000_add_sandbox_events_team_projection.sql +++ b/packages/clickhouse/migrations/20260413080000_add_sandbox_events_team_projection.sql @@ -6,7 +6,7 @@ -- This projection should be removed once we migrate the table's ORDER BY to -- (sandbox_team_id, sandbox_id, timestamp) via a CREATE + RENAME swap. ALTER TABLE sandbox_events_local - ADD PROJECTION proj_team_id ( + ADD PROJECTION IF NOT EXISTS proj_team_id ( SELECT * ORDER BY sandbox_team_id, sandbox_id, timestamp ); From 4073dfd91c67f135c71f2e7ef37f01ad546bee45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Nov=C3=A1k?= Date: Tue, 14 Apr 2026 03:04:34 -0700 Subject: [PATCH 4/5] Update packages/clickhouse/migrations/20260413080000_add_sandbox_events_team_projection.sql --- .../20260413080000_add_sandbox_events_team_projection.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/clickhouse/migrations/20260413080000_add_sandbox_events_team_projection.sql b/packages/clickhouse/migrations/20260413080000_add_sandbox_events_team_projection.sql index b19b2ce374..9faf39f3de 100644 --- a/packages/clickhouse/migrations/20260413080000_add_sandbox_events_team_projection.sql +++ b/packages/clickhouse/migrations/20260413080000_add_sandbox_events_team_projection.sql @@ -7,7 +7,7 @@ -- (sandbox_team_id, sandbox_id, timestamp) via a CREATE + RENAME swap. ALTER TABLE sandbox_events_local ADD PROJECTION IF NOT EXISTS proj_team_id ( - SELECT * ORDER BY sandbox_team_id, sandbox_id, timestamp + SELECT * ORDER BY sandbox_team_id, timestamp ); ALTER TABLE sandbox_events_local From e2cbbd7487ddefb9ff2647447f42c57ad659c06d Mon Sep 17 00:00:00 2001 From: Jakub Novak Date: Tue, 14 Apr 2026 10:12:38 +0000 Subject: [PATCH 5/5] chore(clickhouse): drop redundant bloom filter and fix projection comments --- ...3080000_add_sandbox_events_team_projection.sql | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/clickhouse/migrations/20260413080000_add_sandbox_events_team_projection.sql b/packages/clickhouse/migrations/20260413080000_add_sandbox_events_team_projection.sql index 9faf39f3de..8f2a76541b 100644 --- a/packages/clickhouse/migrations/20260413080000_add_sandbox_events_team_projection.sql +++ b/packages/clickhouse/migrations/20260413080000_add_sandbox_events_team_projection.sql @@ -1,10 +1,12 @@ -- +goose Up --- TEMPORARY: Add a projection ordered by (sandbox_team_id, sandbox_id, timestamp) +-- TEMPORARY: Add a projection ordered by (sandbox_team_id, timestamp) -- so that queries filtering by sandbox_team_id can skip the full table scan. -- The base ORDER BY is (sandbox_id, timestamp) which doesn't help when only -- sandbox_team_id is provided. -- This projection should be removed once we migrate the table's ORDER BY to --- (sandbox_team_id, sandbox_id, timestamp) via a CREATE + RENAME swap. +-- (sandbox_team_id, timestamp, sandbox_id) via a CREATE + RENAME swap. +-- Also drops the now-redundant bloom_filter index on sandbox_team_id since +-- the projection's primary index covers that filtering. ALTER TABLE sandbox_events_local ADD PROJECTION IF NOT EXISTS proj_team_id ( SELECT * ORDER BY sandbox_team_id, timestamp @@ -13,6 +15,15 @@ ALTER TABLE sandbox_events_local ALTER TABLE sandbox_events_local MATERIALIZE PROJECTION proj_team_id; +ALTER TABLE sandbox_events_local + DROP INDEX IF EXISTS idx_team_id; + -- +goose Down +ALTER TABLE sandbox_events_local + ADD INDEX IF NOT EXISTS idx_team_id sandbox_team_id TYPE bloom_filter(0.01) GRANULARITY 1; + +ALTER TABLE sandbox_events_local + MATERIALIZE INDEX idx_team_id; + ALTER TABLE sandbox_events_local DROP PROJECTION IF EXISTS proj_team_id;