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 <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Tomás Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
This commit is contained in:
Nick Clemens 2025-09-03 13:24:30 +00:00 committed by Lucas Gass
parent c9af16546b
commit 6f135d4392
No known key found for this signature in database
GPG Key ID: 60B447398B0D36CA
1 changed files with 4 additions and 3 deletions

View File

@ -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') ) {