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(data-modeling): index the matview health check's team scan (#100726)

posthog[bot]4651d814f6a4
diff --git a/products/data_modeling/backend/migrations/0042_dwsavedquery_team_live_matvw_idx.py b/products/data_modeling/backend/migrations/0042_dwsavedquery_team_live_matvw_idx.py
new file mode 100644
index 000000000000..7960dbfb02ea
--- /dev/null
+++ b/products/data_modeling/backend/migrations/0042_dwsavedquery_team_live_matvw_idx.py
@@ -0,0 +1,22 @@
+from django.db import migrations, models
+
+from posthog.migration_helpers import SafeAddIndexConcurrently
+
+
+class Migration(migrations.Migration):
+ atomic = False
+
+ dependencies = [
+ ("data_modeling", "0041_datamodelingjob_sq_engine_run_idx"),
+ ]
+
+ operations = [
+ SafeAddIndexConcurrently(
+ model_name="datawarehousesavedquery",
+ index=models.Index(
+ fields=["team_id", "is_materialized"],
+ name="dwsavedquery_team_live_matvw",
+ condition=~models.Q(deleted=True),
+ ),
+ ),
+ ]
diff --git a/products/data_modeling/backend/migrations/max_migration.txt b/products/data_modeling/backend/migrations/max_migration.txt
index 470afe775426..5bd5123cd4d0 100644
--- a/products/data_modeling/backend/migrations/max_migration.txt
+++ b/products/data_modeling/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0041_datamodelingjob_sq_engine_run_idx
+0042_dwsavedquery_team_live_matvw_idx
diff --git a/products/data_modeling/backend/models/datawarehouse_saved_query.py b/products/data_modeling/backend/models/datawarehouse_saved_query.py
index f3f1a15d6da1..dca0ba30693f 100644
--- a/products/data_modeling/backend/models/datawarehouse_saved_query.py
+++ b/products/data_modeling/backend/models/datawarehouse_saved_query.py
@@ -212,7 +212,15 @@ class DataWarehouseSavedQuery(CreatedMetaFields, UUIDTModel, UpdatedMetaFields,
fields=["team_id", "name"],
name="dwsavedquery_team_live_name",
condition=~models.Q(deleted=True),
- )
+ ),
+ # The daily materialized view health check reads the live materialized views of a
+ # batch of teams. Without `is_materialized` in the key it reads every live saved query
+ # those teams own. The partial condition matches the index above, for the same reason.
+ models.Index(
+ fields=["team_id", "is_materialized"],
+ name="dwsavedquery_team_live_matvw",
+ condition=~models.Q(deleted=True),
+ ),
]
@property