Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

perf(experiments): index running experiments for hourly recalculation scans (#114847)

posthog[bot]00853006f358
diff --git a/products/experiments/backend/migrations/0048_experiment_running_start_date_index.py b/products/experiments/backend/migrations/0048_experiment_running_start_date_index.py
new file mode 100644
index 000000000000..269ccfc8a948
--- /dev/null
+++ b/products/experiments/backend/migrations/0048_experiment_running_start_date_index.py
@@ -0,0 +1,22 @@
+from django.db import migrations, models
+
+from posthog.migration_helpers import SafeAddIndexConcurrently
+
+
+class Migration(migrations.Migration):
+ atomic = False
+
+ dependencies = [
+ ("experiments", "0047_drop_legacy_recalculation_time_column"),
+ ]
+
+ operations = [
+ SafeAddIndexConcurrently(
+ model_name="experiment",
+ index=models.Index(
+ condition=models.Q(("deleted", False), ("status", "running")),
+ fields=["start_date"],
+ name="posthog_exp_running_start_idx",
+ ),
+ ),
+ ]
diff --git a/products/experiments/backend/migrations/max_migration.txt b/products/experiments/backend/migrations/max_migration.txt
index ef207206abcf..8121f46e2d67 100644
--- a/products/experiments/backend/migrations/max_migration.txt
+++ b/products/experiments/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0047_drop_legacy_recalculation_time_column
+0048_experiment_running_start_date_index
diff --git a/products/experiments/backend/models/experiment.py b/products/experiments/backend/models/experiment.py
index c4e22e0812f0..a2d216cdf62f 100644
--- a/products/experiments/backend/models/experiment.py
+++ b/products/experiments/backend/models/experiment.py
@@ -168,6 +168,14 @@ class Experiment(Taggable, FileSystemSyncMixin, ModelActivityMixin, RootTeamMixi
name="posthog_experiment_feature_flag_rule_id_uniq",
)
]
+ indexes = [
+ # The hourly recalculation scans select running experiments across all teams.
+ models.Index(
+ fields=["start_date"],
+ name="posthog_exp_running_start_idx",
+ condition=models.Q(deleted=False, status="running"),
+ ),
+ ]
def __str__(self):
return self.name or "Untitled"