From 222b36f284cdabbdcbb67f0371a00b795cc3e30e 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 | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Koha/Item.pm b/Koha/Item.pm index 22eaceac948..858ff545343 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,20 @@ Returns whether the item is a bundle or not sub is_bundle { my ($self) = @_; - return $self->bundle_items->count ? 1 : 0; + my $dbi_cache = Koha::Cache::Memory::Lite->get_instance; + my $cache_key = "DBI->count:is_bundle_count"; + my $sth = $dbi_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); + $dbi_cache->set_in_cache( $cache_key, $sth ); + } + + $sth->execute($self->itemnumber); + my $count = $sth->fetchrow_arrayref()->[0]; + + return $count ? 1 : 0; } =head3 bundle_host -- 2.43.0