From 63b5204fb05fdf8a89d915a3a37888fc8fce993b Mon Sep 17 00:00:00 2001 From: Ben Thorner Date: Wed, 5 Jan 2022 17:21:49 +0000 Subject: [PATCH] Optimise query to populate notification statuses Investigation with EXPLAIN and EXPLAIN ANALYZE for the notification history table shows this is another instance of [1] but for the key type column. Swapping "!=" for "IN" solves the problem. [1]: https://github.com/alphagov/notifications-api/pull/3360 --- app/dao/fact_notification_status_dao.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/dao/fact_notification_status_dao.py b/app/dao/fact_notification_status_dao.py index ae7d816a3..ad26ddd2d 100644 --- a/app/dao/fact_notification_status_dao.py +++ b/app/dao/fact_notification_status_dao.py @@ -10,6 +10,8 @@ from sqlalchemy.types import DateTime, Integer from app import db from app.dao.dao_utils import autocommit from app.models import ( + KEY_TYPE_NORMAL, + KEY_TYPE_TEAM, KEY_TYPE_TEST, NOTIFICATION_CANCELLED, NOTIFICATION_CREATED, @@ -72,7 +74,7 @@ def query_for_fact_status_data(table, start_date, end_date, notification_type, s table.created_at < end_date, table.notification_type == notification_type, table.service_id == service_id, - table.key_type != KEY_TYPE_TEST + table.key_type.in_((KEY_TYPE_NORMAL, KEY_TYPE_TEAM)), ).group_by( table.template_id, table.service_id,