From 47025a1060eb18123d49bc6ce7cd546a0e93ee75 Mon Sep 17 00:00:00 2001 From: Tomas Kopecek Date: Mon, 2 Nov 2020 11:12:52 +0100 Subject: [PATCH] hub: use CTE for build_references Fixes: https://pagure.io/koji/issue/2535 --- hub/kojihub.py | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/hub/kojihub.py b/hub/kojihub.py index 2df0fb0b..c4e0611f 100644 --- a/hub/kojihub.py +++ b/hub/kojihub.py @@ -8000,27 +8000,31 @@ def build_references(build_id, limit=None, lazy=False): if build_rpm_ids: # psql planner gots confused if buildroot table is large (>trillion) # and len(rpm_ids) > ~500. In such case it switched to looped sequential scans - _dml("SET enable_hashjoin=off", {}) - q = """SELECT MAX(create_event) - FROM standard_buildroot - WHERE buildroot_id IN ( - SELECT buildroot_id + # using "SET enabled_hashjoin=off" improved it for some cases. CTE could be slower for + # simple cases but would improve complicated ones. + q = """WITH buildroot_ids as ( + SELECT DISTINCT buildroot_id FROM buildroot_listing WHERE rpm_id IN %(rpm_ids)s - )""" - event_id = (_fetchSingle(q, {'rpm_ids': build_rpm_ids}) or (0,))[0] or 0 - _dml("SET enable_hashjoin=on", {}) - if build_archive_ids: - _dml("SET enable_hashjoin=off", {}) - q = """SELECT MAX(create_event) + ) + SELECT MAX(create_event) FROM standard_buildroot WHERE buildroot_id IN ( - SELECT buildroot_id + SELECT buildroot_id FROM buildroot_ids + )""" + event_id = (_fetchSingle(q, {'rpm_ids': build_rpm_ids}) or (0,))[0] or 0 + if build_archive_ids: + q = """WITH buildroot_ids as ( + SELECT DISTINCT buildroot_id FROM buildroot_archives WHERE archive_id IN %(archive_ids)s + ) + SELECT MAX(create_event) + FROM standard_buildroot + WHERE buildroot_id IN ( + SELECT buildroot_id FROM buildroot_ids )""" event_id2 = (_fetchSingle(q, {'archive_ids': build_archive_ids}) or (0,))[0] or 0 - _dml("SET enable_hashjoin=on", {}) event_id = max(event_id, event_id2) if event_id: q = """SELECT EXTRACT(EPOCH FROM get_event_time(%(event_id)i))"""