From da07f9dde817702f29a56c847b22250379b99608 Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Thu, 2 Feb 2017 09:45:06 +0000 Subject: [PATCH] Performance improvement to the template usage query. --- app/dao/notifications_dao.py | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index 5560a55d1..13317afd7 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -93,22 +93,30 @@ def dao_get_template_usage(service_id, limit_days=None): if limit_days and limit_days <= 7: # can get this data from notifications table table = Notification - query = db.session.query( - func.count(table.template_id).label('count'), - table.template_id, - Template.name, - Template.template_type - ) - query_filter = [table.service_id == service_id, table.key_type != KEY_TYPE_TEST] if limit_days is not None: query_filter.append(table.created_at >= days_ago(limit_days)) - return query.filter(*query_filter) \ - .join(Template) \ - .group_by(table.template_id, Template.name, Template.template_type) \ - .order_by(asc(Template.name)) \ - .all() + notifications_aggregate_query = db.session.query( + func.count().label('count'), + table.template_id + ).filter( + *query_filter + ).group_by( + table.template_id + ).subquery() + + query = db.session.query( + Template.id.label('template_id'), + Template.name, + Template.template_type, + notifications_aggregate_query.c.count + ).join( + notifications_aggregate_query, + notifications_aggregate_query.c.template_id == Template.id + ).order_by(Template.name) + + return query.all() @statsd(namespace="dao")