LP#1971745 - speed up open-ils.storage.action.live_holds.wide_hash
authorJosh Stompro <stomproj@larl.org>
Tue, 22 Nov 2022 18:09:46 +0000 (12:09 -0600)
committerJason Stephenson <jstephenson@cwmars.org>
Mon, 28 Nov 2022 19:53:00 +0000 (14:53 -0500)
Postgresql 10 and EG 3.9.0

The query to grab holds on the hold shelf was taking 35-50 seconds on
a test system to return 466 records.  Explain analyze showed that the
left join on asset.call_number was causing a sequential scan and reading
all asset.call_number rows.

Letting PG know that the two conditions of the join are mutually exclusive
seems to let PG know that an index scan is faster.

Signed-off-by: Josh Stompro <stomproj@larl.org>
Signed-off-by: Jason Stephenson <jstephenson@cwmars.org>

Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Publisher/action.pm

index 2b1fba2..c519e63 100644 (file)
@@ -2399,7 +2399,7 @@ SELECT  h.id, h.request_time, h.capture_time, h.fulfillment_time, h.checkin_time
             SELECT *, (ROW_NUMBER() OVER (ORDER BY name) + 1000000) AS fallback_position
             FROM asset.copy_location
         ) acpl_ordered ON (acpl_ordered.id = cp.location)
-        LEFT JOIN asset.call_number cn ON (cn.id = cp.call_number OR (h.hold_type = 'V' AND cn.id = h.target))
+        LEFT JOIN asset.call_number cn ON ((cn.id = cp.call_number AND h.hold_type != 'V' ) OR (h.hold_type = 'V' AND cn.id = h.target))
         LEFT JOIN asset.call_number_prefix acnp ON (cn.prefix = acnp.id)
         LEFT JOIN asset.call_number_suffix acns ON (cn.suffix = acns.id)
         LEFT JOIN LATERAL (SELECT * FROM action.hold_transit_copy WHERE h.id = hold ORDER BY id DESC LIMIT 1) tr ON TRUE