From ab44ccd0203a4e359504353c76ba1994acbd4040 Mon Sep 17 00:00:00 2001 From: Stefan Berndtsson Date: Fri, 4 Nov 2022 13:46:15 +0100 Subject: [PATCH] Bug 36441: Improve performance of Item::is_bundle Perform count using DBI-query in Item::is_bundle to significantly improve performance. To test: 1) Go to the details page of a bibio with a large number of holdings holdings, preferably more than a hundred, and take note of page load time. 3) Apply patch 4) Reload the page and take note of page load time. 5) There should be an improvement of about 10-15% 6) Ensure tests in t/db_dependent/Koha/Item.t pass Sponsored-by: Gothenburg University Library --- Koha/Item.pm | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Koha/Item.pm b/Koha/Item.pm index 22eaceac948..7d3c9c22d5c 100644 --- a/Koha/Item.pm +++ b/Koha/Item.pm @@ -55,6 +55,7 @@ use Koha::StockRotationItem; use Koha::StockRotationRotas; use Koha::TrackedLinks; use Koha::Policy::Holds; +use Koha::Cache::Memory::Lite; use base qw(Koha::Object); @@ -1857,7 +1858,17 @@ Returns whether the item is a bundle or not sub is_bundle { my ($self) = @_; - return $self->bundle_items->count ? 1 : 0; + my $memory_cache = Koha::Cache::Memory::Lite->get_instance; + my $cache_key = 'Koha::Item:is_bundle_dbi_sth'; + my $sth = $memory_cache->get_from_cache($cache_key); + unless (defined $sth) { + my $query = "SELECT COUNT(*) FROM items i LEFT JOIN item_bundles ib ON i.itemnumber = ib.item WHERE ib.host = ?"; + my $dbh = C4::Context->dbh; + $sth = $dbh->prepare($query); + $memory_cache->set_in_cache($cache_key, $sth); + } + $sth->execute($self->itemnumber); + return $sth->fetchrow_arrayref()->[0] ? 1 : 0; } =head3 bundle_host -- 2.43.0