Bugzilla – Attachment 193747 Details for
Bug 14962
Temp Shelving Location / On Display Module
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 14962: (QA follow-up) Eliminate N+1 Koha::Items->find in search result rendering
644e161.patch (text/plain), 3.94 KB, created by
Martin Renvoize (ashimema)
on 2026-02-24 13:37:22 UTC
(
hide
)
Description:
Bug 14962: (QA follow-up) Eliminate N+1 Koha::Items->find in search result rendering
Filename:
MIME Type:
Creator:
Martin Renvoize (ashimema)
Created:
2026-02-24 13:37:22 UTC
Size:
3.94 KB
patch
obsolete
>From 644e161b407f4021b048ea0f89209471ff60475b Mon Sep 17 00:00:00 2001 >From: Martin Renvoize <martin.renvoize@openfifth.co.uk> >Date: Tue, 24 Feb 2026 13:02:35 +0000 >Subject: [PATCH] Bug 14962: (QA follow-up) Eliminate N+1 Koha::Items->find in > search result rendering > >Five display-aware helper closures in C4::Search each independently called >Koha::Items->find($item->{itemnumber}). For a search page with 20 results >and 5 items each, this produced up to 500 extra DB queries per page when >UseDisplayModule was enabled. > >Introduce a shared $item_obj_cache keyed by itemnumber so the item object >is fetched at most once per item across all five closures. > >Sponsored-by: ByWater Solutions >Signed-of-by: Martin Renvoize <martin.renvoize@openfifth.co.uk> >--- > C4/Search.pm | 24 +++++++++++++++++------- > 1 file changed, 17 insertions(+), 7 deletions(-) > >diff --git a/C4/Search.pm b/C4/Search.pm >index e7cf3a9cf54..edeb4f2f524 100644 >--- a/C4/Search.pm >+++ b/C4/Search.pm >@@ -1699,15 +1699,25 @@ sub searchResults { > my $itemtypes = Koha::ItemTypes->search_with_localization; > my %itemtypes = map { $_->{itemtype} => $_ } @{ $itemtypes->unblessed }; > >- # Cache for display lookups to avoid repeated queries >- my $display_cache = {}; >+ # Caches shared across all display-aware helper closures >+ my $display_cache = {}; >+ my $item_obj_cache = {}; >+ >+ # Returns a Koha::Item object for the given itemnumber, cached per result set >+ my $get_item_obj = sub { >+ my ($itemnumber) = @_; >+ unless ( exists $item_obj_cache->{$itemnumber} ) { >+ $item_obj_cache->{$itemnumber} = Koha::Items->find($itemnumber); >+ } >+ return $item_obj_cache->{$itemnumber}; >+ }; > > # Helper function to get effective location for search results > my $get_effective_collection_code = sub { > my ($item) = @_; > return $collections->{ $item->{ccode} } unless C4::Context->preference('UseDisplayModule'); > >- my $item_obj = Koha::Items->find( $item->{itemnumber} ); >+ my $item_obj = $get_item_obj->( $item->{itemnumber} ); > return $collections->{ $item->{ccode} } unless $item_obj; > > my $effective_collection_code = $item_obj->effective_collection_code; >@@ -1732,7 +1742,7 @@ sub searchResults { > my ($item) = @_; > return $itemtypes{ $item->{itype} }{translated_description} unless C4::Context->preference('UseDisplayModule'); > >- my $item_obj = Koha::Items->find( $item->{itemnumber} ); >+ my $item_obj = $get_item_obj->( $item->{itemnumber} ); > return $itemtypes{ $item->{itype} }{translated_description} unless $item_obj; > > my $effective_itemtype = $item_obj->effective_itemtype; >@@ -1757,7 +1767,7 @@ sub searchResults { > my ($item) = @_; > return $shelflocations->{ $item->{location} } unless C4::Context->preference('UseDisplayModule'); > >- my $item_obj = Koha::Items->find( $item->{itemnumber} ); >+ my $item_obj = $get_item_obj->( $item->{itemnumber} ); > return $shelflocations->{ $item->{location} } unless $item_obj; > > my $effective_loc = $shelflocations->{ $item_obj->effective_location }; >@@ -1780,7 +1790,7 @@ sub searchResults { > my ($item) = @_; > return $item->{homebranch} unless C4::Context->preference('UseDisplayModule'); > >- my $item_obj = Koha::Items->find( $item->{itemnumber} ); >+ my $item_obj = $get_item_obj->( $item->{itemnumber} ); > return $item->{homebranch} unless $item_obj; > > my $effective_homebranch = $item_obj->effective_homebranch; >@@ -1805,7 +1815,7 @@ sub searchResults { > my ($item) = @_; > return $item->{holdingbranch} unless C4::Context->preference('UseDisplayModule'); > >- my $item_obj = Koha::Items->find( $item->{itemnumber} ); >+ my $item_obj = $get_item_obj->( $item->{itemnumber} ); > return $item->{holdingbranch} unless $item_obj; > > my $effective_holdingbranch = $item_obj->effective_holdingbranch; >-- >2.53.0 >
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 14962
:
190216
|
190217
|
190218
|
190219
|
190220
|
190221
|
190222
|
190223
|
190224
|
190225
|
190226
|
190227
|
190228
|
190229
|
190230
|
190231
|
190232
|
190571
|
190572
|
193719
|
193720
|
193721
|
193722
|
193723
|
193724
|
193725
|
193726
|
193727
|
193728
|
193729
|
193730
|
193731
|
193732
|
193733
|
193734
|
193735
|
193736
|
193737
|
193738
|
193739
|
193740
|
193741
|
193742
|
193743
|
193744
|
193745
|
193746
|
193747
|
193748
|
193749
|
193750
|
193751
|
193752
|
193753
|
193754
|
193755
|
194285
|
194286
|
194287
|
194288
|
194289
|
194290
|
194291
|
194292
|
194293
|
194294
|
194295
|
194296
|
194297
|
194298
|
194299
|
194300
|
194301
|
194302
|
194303
|
194304
|
194305
|
194306
|
194307
|
194308
|
194309
|
194310
|
194311
|
194312
|
194313
|
194314
|
194315
|
194316
|
194317
|
194318
|
194320
|
194321
|
194322