From afca5603bd3fae9aeb8f1376eeb567ecb9792991 Mon Sep 17 00:00:00 2001 From: Tomas Kopecek Date: Nov 11 2020 13:31:36 +0000 Subject: hub: use CTE for build_references Fixes: https://pagure.io/koji/issue/2535 --- diff --git a/hub/kojihub.py b/hub/kojihub.py index 9488eaa..e0a0c46 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 + ) + SELECT MAX(create_event) + FROM standard_buildroot + WHERE buildroot_id IN ( + SELECT buildroot_id FROM buildroot_ids )""" 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) - FROM standard_buildroot - WHERE buildroot_id IN ( - SELECT buildroot_id + 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))"""