From f1bc5097028524f6f6168c3d328b618d3898af25 Mon Sep 17 00:00:00 2001 From: Crag Wolfe Date: Tue, 22 Nov 2016 10:06:11 -0800 Subject: [PATCH] More efficient pruning of events Avoid a potentially large select in clause. synchronize_session may be False here, as we don't have to worry about old stale pruned events in the session (keeping in mind the prune gets triggered when new events are created). Change-Id: I27a00a1afb6518176c031551d6a74e8b12546ef2 --- heat/db/sqlalchemy/api.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/heat/db/sqlalchemy/api.py b/heat/db/sqlalchemy/api.py index 025479c135..5fe0cf8769 100644 --- a/heat/db/sqlalchemy/api.py +++ b/heat/db/sqlalchemy/api.py @@ -892,13 +892,16 @@ def _delete_event_rows(context, stack_id, limit): # So we must manually supply the IN() values. # pgsql SHOULD work with the pure DELETE/JOIN below but that must be # confirmed via integration tests. - query = _query_all_by_stack(context, stack_id) session = context.session - ids = [r.id for r in query.order_by( - models.Event.id).limit(limit).all()] - q = session.query(models.Event).filter( - models.Event.id.in_(ids)) - return q.delete(synchronize_session='fetch') + res = session.query(models.Event.id).filter_by( + stack_id=stack_id).order_by(models.Event.id).limit(limit).all() + if not res: + return 0 + (max_id, ) = res[-1] + return session.query(models.Event).filter( + models.Event.id <= max_id).filter( + models.Event.stack_id == stack_id).delete( + synchronize_session=False) def event_create(context, values):