Update subquery to be more efficient.

Update subquery to run again but for test keys. Test data is never inserted in Notifications so they need to be deleted separately now given the join to NotificationHistory.
This commit is contained in:
Rebecca Law
2019-06-03 15:16:46 +01:00
parent b8399b8b9b
commit cfd42a2eb9
2 changed files with 28 additions and 6 deletions

View File

@@ -336,21 +336,35 @@ def delete_notifications_older_than_retention_by_type(notification_type, qry_lim
return deleted
def _delete_notifications(
deleted, notification_type, date_to_delete_from, service_id, query_limit):
def _delete_notifications(deleted, notification_type, date_to_delete_from, service_id, query_limit):
subquery = db.session.query(
Notification.id
).join(NotificationHistory, NotificationHistory.id == Notification.id).filter(
Notification.notification_type == notification_type,
Notification.service_id == service_id,
Notification.created_at < date_to_delete_from,
).limit(query_limit).subquery()
deleted += _delete_for_query(subquery)
subquery_for_test_keys = db.session.query(
Notification.id
).filter(
Notification.notification_type == notification_type,
Notification.service_id == service_id,
Notification.created_at < date_to_delete_from,
NotificationHistory.id == Notification.id
Notification.key_type == KEY_TYPE_TEST
).limit(query_limit).subquery()
deleted += _delete_for_query(subquery_for_test_keys)
return deleted
def _delete_for_query(subquery):
number_deleted = db.session.query(Notification).filter(
Notification.id.in_(subquery)).delete(synchronize_session='fetch')
deleted += number_deleted
deleted = number_deleted
db.session.commit()
while number_deleted > 0:
number_deleted = db.session.query(Notification).filter(