From 67924f6b7efa9cc485a193735512f1471cf0ec42 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Wed, 3 Sep 2025 13:24:30 +0000 Subject: [PATCH] Bug 28702: Reduce DB lookups in buildKohaItemsNamespace This patch moves the prefetch to current_branchtransfers and uses the prefetched reserves and transfers to reduce the number of lookups from the DB To test: 1 - Perform a search on the OPAC 2 - Add the results to a list 3 - Load the list several times and use developer tools (F12) to view the time to load in the network tab 4 - Repeat a search several times and use developer tools (F12) to view the time to load in the network tab 5 - Record the times noted above 6 - Find or create a record with many items, biblionumber 3 in my example 7 - on the command line: export DBIC_TRACE=1 perl -e 'use C4::XSLT; my $i = C4::XSLT::buildKohaItemsNamespace( 3 );' note the lookups in reserves and transfers 8 - Apply patch 9 - Repeat 7 and note less lookups 10 - Repeat the search and list view and compare times to before the patch 11 - prove -v t/db_dependent/XSLT.t --- C4/XSLT.pm | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/C4/XSLT.pm b/C4/XSLT.pm index 422a02b1ea7..7cac9f8c743 100644 --- a/C4/XSLT.pm +++ b/C4/XSLT.pm @@ -292,7 +292,8 @@ sub buildKohaItemsNamespace { $items_rs = Koha::Items->new; } - my $items = $items_rs->search( $query, { prefetch => [ 'branchtransfers', 'reserves' ] } ); + my $items = + $items_rs->search( $query, { prefetch => [ 'current_branchtransfers', 'reserves', 'tmp_holdsqueue' ] } ); my $shelflocations = { map { $_->{authorised_value} => $_->{opac_description} } @@ -333,10 +334,10 @@ sub buildKohaItemsNamespace { } elsif ( $item->has_pending_hold ) { $status = 'other'; $substatus = 'Pending hold'; - } elsif ( $item->holds->waiting->count ) { + } elsif ( $item->_result->reserves->count && $item->holds->waiting->count ) { $status = 'other'; $substatus = 'Hold waiting'; - } elsif ( $item->get_transfer ) { + } elsif ( $item->_result->current_branchtransfers->count && $item->get_transfer ) { $status = 'other'; $substatus = 'In transit'; } elsif ( $item->damaged && !C4::Context->preference('AllowHoldsOnDamagedItems') ) { -- 2.39.5