fix filter to look at right table

a query for notifications was filtering on FtNotificationStatus - we
aren't joining to that table in the query, so sqlalchemy added a cross
join between ft_notification_status (3.7k rows) and Notifications (3.9m
rows), resulting in a 1.3 trillion row materialised table. This query
took 17 hours and pending.

Also, remove orders from querys other than the outer one, since we're
grouping anyway.
This commit is contained in:
Leo Hemsted
2019-01-09 11:26:08 +00:00
parent 9c75e08e2c
commit 5d838415d3

View File

@@ -153,8 +153,6 @@ def fetch_notification_status_totals_for_all_services(start_date, end_date):
FactNotificationStatus.notification_type, FactNotificationStatus.notification_type,
FactNotificationStatus.notification_status, FactNotificationStatus.notification_status,
FactNotificationStatus.key_type, FactNotificationStatus.key_type,
).order_by(
FactNotificationStatus.notification_type
) )
today = get_london_midnight_in_utc(datetime.utcnow()) today = get_london_midnight_in_utc(datetime.utcnow())
if start_date <= today.date() <= end_date: if start_date <= today.date() <= end_date:
@@ -184,7 +182,9 @@ def fetch_notification_status_totals_for_all_services(start_date, end_date):
all_stats_table.c.notification_type all_stats_table.c.notification_type
) )
else: else:
query = stats query = stats.order_by(
FactNotificationStatus.notification_type
)
return query.all() return query.all()
@@ -245,7 +245,7 @@ def fetch_stats_for_all_services_by_date_range(start_date, end_date, include_fro
Notification.service_id Notification.service_id
) )
if not include_from_test_key: if not include_from_test_key:
subquery = subquery.filter(FactNotificationStatus.key_type != KEY_TYPE_TEST) subquery = subquery.filter(Notification.key_type != KEY_TYPE_TEST)
subquery = subquery.subquery() subquery = subquery.subquery()
stats_for_today = db.session.query( stats_for_today = db.session.query(
@@ -261,7 +261,7 @@ def fetch_stats_for_all_services_by_date_range(start_date, end_date, include_fro
).outerjoin( ).outerjoin(
subquery, subquery,
subquery.c.service_id == Service.id subquery.c.service_id == Service.id
).order_by(Service.id) )
all_stats_table = stats.union_all(stats_for_today).subquery() all_stats_table = stats.union_all(stats_for_today).subquery()
query = db.session.query( query = db.session.query(