From 2381b168746128c98e1ec53e5254854dded14308 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 We also don't fetch the specific current transfer, knowing we have current transfers is enough 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 Signed-off-by: Jonathan Druart --- C4/XSLT.pm | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/C4/XSLT.pm b/C4/XSLT.pm index 422a02b1ea7..88443ab27af 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->holds->count && $item->holds->waiting->count ) { $status = 'other'; $substatus = 'Hold waiting'; - } elsif ( $item->get_transfer ) { + } elsif ( $item->_result->current_branchtransfers->count ) { $status = 'other'; $substatus = 'In transit'; } elsif ( $item->damaged && !C4::Context->preference('AllowHoldsOnDamagedItems') ) { -- 2.34.1