Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Speed up counting past 30 day traffic via ingest_counters (#4841)

Karl-Aksel Puulmann45638d19c892
diff --git a/priv/ingest_repo/migrations/20241120064325_create_ingest_counters_site_traffic_projection.exs b/priv/ingest_repo/migrations/20241120064325_create_ingest_counters_site_traffic_projection.exs
new file mode 100644
index 0000000000..4738426a64
--- /dev/null
+++ b/priv/ingest_repo/migrations/20241120064325_create_ingest_counters_site_traffic_projection.exs
@@ -0,0 +1,38 @@
+defmodule Plausible.IngestRepo.Migrations.CreateIngestCountersSiteTrafficProjection do
+ use Ecto.Migration
+
+ def up do
+ # ClickHouse 24.8 introduced `deduplicate_merge_projection_mode` which we need to set to create this projection.
+ # As such, try to set the setting and if it fails, ignore it.
+ execute(fn ->
+ try do
+ repo().query!("""
+ ALTER TABLE ingest_counters
+ #{Plausible.MigrationUtils.on_cluster_statement("ingest_counters")}
+ MODIFY SETTING deduplicate_merge_projection_mode='rebuild'
+ """)
+ rescue
+ _e -> nil
+ end
+ end)
+
+ execute """
+ ALTER TABLE ingest_counters
+ #{Plausible.MigrationUtils.on_cluster_statement("ingest_counters")}
+ ADD PROJECTION ingest_counters_site_traffic_projection (
+ SELECT site_id, toDate(event_timebucket), sumIf(value, metric = 'buffered')
+ GROUP BY site_id, toDate(event_timebucket)
+ )
+ """
+
+ execute """
+ ALTER TABLE ingest_counters
+ #{Plausible.MigrationUtils.on_cluster_statement("ingest_counters")}
+ MATERIALIZE PROJECTION ingest_counters_site_traffic_projection
+ """
+ end
+
+ def down do
+ raise "irreversible"
+ end
+end