Skip to content

LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows

[MISC] Add performance indexes on page_usage table for Superset dashboard optimization (#1778)

Chandrasekharan M437266ed901e
diff --git a/backend/account_usage/migrations/0003_add_performance_indexes.py b/backend/account_usage/migrations/0003_add_performance_indexes.py
new file mode 100644
index 000000000..2212a7694
--- /dev/null
+++ b/backend/account_usage/migrations/0003_add_performance_indexes.py
@@ -0,0 +1,38 @@
+# Generated manually for Superset dashboard performance optimization
+
+from django.contrib.postgres.operations import AddIndexConcurrently
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+ """Add indexes to optimize page_usage queries for analytics dashboards.
+
+ These indexes address timeout issues in Superset multi-org usage dashboards:
+ 1. idx_page_usage_created_org - Enables date-range filtering (90-day lookback)
+ with INCLUDE columns for index-only scans (Django doesn't support INCLUDE)
+ 2. idx_page_usage_run_id - Speeds up JOIN with workflow_file_execution
+ """
+
+ # Required for CONCURRENTLY - cannot run in transaction
+ atomic = False
+
+ dependencies = [
+ ("account_usage", "0002_alter_pageusage_pages_processed"),
+ ]
+
+ operations = [
+ # Covering index - must use RunSQL because Django doesn't support INCLUDE
+ migrations.RunSQL(
+ sql="""
+ CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_page_usage_created_org
+ ON page_usage(created_at, organization_id)
+ INCLUDE (pages_processed, run_id);
+ """,
+ reverse_sql="DROP INDEX CONCURRENTLY IF EXISTS idx_page_usage_created_org;",
+ ),
+ # Simple index - using Django's concurrent index operation
+ AddIndexConcurrently(
+ model_name="pageusage",
+ index=models.Index(fields=["run_id"], name="idx_page_usage_run_id"),
+ ),
+ ]
diff --git a/backend/account_usage/models.py b/backend/account_usage/models.py
index 5fc951b10..d48e5ff51 100644
--- a/backend/account_usage/models.py
+++ b/backend/account_usage/models.py
@@ -44,4 +44,6 @@ class PageUsage(models.Model):
db_table = "page_usage"
indexes = [
models.Index(fields=["organization_id"]),
+ models.Index(fields=["run_id"], name="idx_page_usage_run_id"),
+ # idx_page_usage_created_org created via RunSQL (Django doesn't support INCLUDE)
]