From 8dd11c45d648c8cb6605959154e00e82d1e741e6 Mon Sep 17 00:00:00 2001 From: Betsy Lorton Date: Tue, 20 Oct 2026 15:28:48 -0400 Subject: [PATCH 1/2] Simplify account search query to improve performance The query was doing multiple unnecessary joins and returning many unneeded columns --- django/evaluate_m2/managers.py | 16 +++++++--------- django/evaluate_m2/tests/test_managers.py | 3 +++ django/evaluate_m2/views.py | 1 + 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/django/evaluate_m2/managers.py b/django/evaluate_m2/managers.py index 29184bb2..5f3b794d 100644 --- a/django/evaluate_m2/managers.py +++ b/django/evaluate_m2/managers.py @@ -14,17 +14,16 @@ def no_previous_bankruptcy_indicators(self): ) def with_hit_count(self, event): - from evaluate_m2.models import EvaluatorResult + from evaluate_m2.models import EvaluatorResultMaterializedView - # Subquery for the number of evaluators hit for + # Subquery for the number of evaluators hits for # each account subquery = ( - EvaluatorResult.objects.filter( - result_summary__event=event, - acct_num=OuterRef("cons_acct_num"), + EvaluatorResultMaterializedView.objects.filter( + event_id=event.id, + cons_acct_num=OuterRef("cons_acct_num"), ) - .order_by() - .values("acct_num") + .values("cons_acct_num") .annotate( n=Count("id"), ) @@ -41,10 +40,9 @@ def with_hit_count(self, event): def with_record_count(self, event): subquery = ( self.model._default_manager.filter( - data_file__event=event, + event=event, cons_acct_num=OuterRef("cons_acct_num"), ) - .order_by() .values("cons_acct_num") .annotate( n=Count("id"), diff --git a/django/evaluate_m2/tests/test_managers.py b/django/evaluate_m2/tests/test_managers.py index d2bda818..c66b59a2 100644 --- a/django/evaluate_m2/tests/test_managers.py +++ b/django/evaluate_m2/tests/test_managers.py @@ -2,6 +2,7 @@ from django.test import TestCase +from evaluate_m2.models import EvaluatorResultMaterializedView from evaluate_m2.tests.evaluator_test_helper import acct_record, evaluator_result_record from parse_m2.models import AccountActivity, M2DataFile, Metro2Event @@ -158,6 +159,7 @@ def test_inconsistency_counts(self): ) evaluator_result_record(self.event, "Sample-1", acct) evaluator_result_record(self.event, "Sample-2", acct) + EvaluatorResultMaterializedView.create_or_refresh_materialized_view() qs = AccountActivity.objects.with_hit_count(self.event) query_acct = qs.filter(cons_acct_num=acct.cons_acct_num).first() @@ -183,6 +185,7 @@ def test_inconsistency_counts_same_eval(self): # One hit across two months evaluator_result_record(self.event, "Sample-1", acct1) evaluator_result_record(self.event, "Sample-1", acct2) + EvaluatorResultMaterializedView.create_or_refresh_materialized_view() qs = AccountActivity.objects.with_hit_count(self.event) query_acct = qs.filter(cons_acct_num=acct1.cons_acct_num).first() diff --git a/django/evaluate_m2/views.py b/django/evaluate_m2/views.py index a162ce2b..0e522f51 100644 --- a/django/evaluate_m2/views.py +++ b/django/evaluate_m2/views.py @@ -410,6 +410,7 @@ def event(self): def get_queryset(self): return ( self.event.get_all_account_activity() + .only('cons_acct_num', 'port_type', 'acct_type') .with_hit_count(self.event) .with_record_count(self.event) ) From 4c2ef479d15e43a79133d1e0d1e941617e2039ed Mon Sep 17 00:00:00 2001 From: Betsy Lorton Date: Tue, 20 Oct 2026 16:50:20 -0400 Subject: [PATCH 2/2] Index on consumer account number to improve account search endpoint performance --- django/evaluate_m2/models.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/django/evaluate_m2/models.py b/django/evaluate_m2/models.py index 34975e52..5d8470e4 100644 --- a/django/evaluate_m2/models.py +++ b/django/evaluate_m2/models.py @@ -381,7 +381,12 @@ def materialized_view_exists(cls) -> bool: CREATE INDEX idx_event_id_evaluator_id ON mv_all_evaluator_results (event_id, evaluator_id); """, - # Index on sample results (to improve sample view performance) + # Index on results by consumer account (for acct search performance) + """ + CREATE INDEX idx_event_id_cons_acct_num + ON mv_all_evaluator_results (event_id, cons_acct_num); + """, + # Index on sample results (for sample view performance) """ CREATE INDEX idx_mv_eval_results_sample ON mv_all_evaluator_results (event_id, evaluator_id, sample);