fix: optimize query for expired workflow runs by adding date filter and limiting results (#16491)

This commit is contained in:
Yeuoly 2025-03-22 11:17:21 +08:00 committed by GitHub
parent 4448a54cc1
commit 1907d2a90a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -149,7 +149,15 @@ class ClearFreePlanTenantExpiredLogs:
while True:
with Session(db.engine).no_autoflush as session:
workflow_runs = session.query(WorkflowRun).filter(WorkflowRun.tenant_id == tenant_id).all()
workflow_runs = (
session.query(WorkflowRun)
.filter(
WorkflowRun.tenant_id == tenant_id,
WorkflowRun.created_at < datetime.datetime.now() - datetime.timedelta(days=days),
)
.limit(batch)
.all()
)
if len(workflow_runs) == 0:
break