diff --git a/posthog/clickhouse/migrations/0049_add_inserted_at_projection.py b/posthog/clickhouse/migrations/0049_add_inserted_at_projection.py new file mode 100644 index 0000000000000..f802f97e53cde --- /dev/null +++ b/posthog/clickhouse/migrations/0049_add_inserted_at_projection.py @@ -0,0 +1,22 @@ +from posthog.clickhouse.client.migration_tools import run_sql_with_exceptions +from posthog.settings import CLICKHOUSE_CLUSTER + +ADD_PROJECTION_SQL = """ +ALTER TABLE sharded_events +ON CLUSTER '{cluster}' +ADD PROJECTION events_inserted_at_projection ( + SELECT * ORDER BY (team_id, inserted_at, event, cityHash64(distinct_id), cityHash64(uuid)) +); +""" + +MATERIALIZE_PROJECTION_SQL = """ +ALTER TABLE sharded_events +ON CLUSTER '{cluster}' +MATERIALIZE PROJECTION events_inserted_at_projection +IN PARTITION '202310'; +""" + +operations = [ + run_sql_with_exceptions(ADD_PROJECTION_SQL.format(cluster=CLICKHOUSE_CLUSTER)), + run_sql_with_exceptions(MATERIALIZE_PROJECTION_SQL.format(cluster=CLICKHOUSE_CLUSTER)), +]