Skip to content

Commit

Permalink
Avoid nested join
Browse files Browse the repository at this point in the history
  • Loading branch information
acasajus committed Sep 30, 2023
1 parent 5b3dddd commit 018c48a
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions cron.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ def delete_logs():
delete_refused_emails()
delete_old_monitoring()

for t in TransactionalEmail.filter(
for worker in TransactionalEmail.filter(
TransactionalEmail.created_at < arrow.now().shift(days=-7)
):
TransactionalEmail.delete(t.id)
TransactionalEmail.delete(worker.id)

for b in Bounce.filter(Bounce.created_at < arrow.now().shift(days=-7)):
Bounce.delete(b.id)
Expand All @@ -99,16 +99,25 @@ def delete_logs():

total_deleted = 0
batch_size = 100
max_dt = arrow.now().shift(weeks=-2)
while True:
rows = (
Session.query(EmailLog.id)
.filter(EmailLog.created_at < max_dt)
.limit(batch_size)
.all()
)
if len(rows) == 0:
break
ids = ",".join([str(row[0]) for row in rows])
deleted_count = Session.execute(
f"delete from email_log where id in ( select id from email_log where created_at < now() - interval '15 day' limit {batch_size})"
f"DELETE FROM email_log WHERE id in ({ids})"
).rowcount
total_deleted += deleted_count
LOG.i(f"Deleted {total_deleted} EmailLog entries")
if deleted_count < batch_size:
break

Session.commit()

LOG.i("Deleted %s email logs", total_deleted)


Expand Down

0 comments on commit 018c48a

Please sign in to comment.