From 5aea0d13385965c73a3ca466698fb91196393d88 Mon Sep 17 00:00:00 2001 From: David Gustafsson Date: Wed, 10 May 2023 16:02:51 +0200 Subject: [PATCH] Bug 31856: Prefetch additional fields for increased performance --- C4/Serials.pm | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/C4/Serials.pm b/C4/Serials.pm index adf88da2f34..1816f00151f 100644 --- a/C4/Serials.pm +++ b/C4/Serials.pm @@ -631,7 +631,7 @@ sub SearchSubscriptions { my $dbh = C4::Context->dbh; my $sth = $dbh->prepare($query); $sth->execute(@where_args); - my $results = $sth->fetchall_arrayref( {} ); + my $results = $sth->fetchall_arrayref( {} ); my $total_results = @{$results}; @@ -639,28 +639,30 @@ sub SearchSubscriptions { $results = [ splice( @{$results}, 0, $params->{results_limit} ) ]; } my %additional_fields_by_id = map { $_->id => $_->name } Koha::AdditionalFields->search( { tablename => 'subscription' } )->as_list; - - for my $subscription ( @$results ) { - $subscription->{cannotedit} = not can_edit_subscription( $subscription ); - $subscription->{cannotdisplay} = not can_show_subscription( $subscription ); - - if (%additional_fields_by_id) { - my @additional_field_values = Koha::AdditionalFieldValues->search( - { - field_id => { -in => [ keys %additional_fields_by_id ] }, - record_id => $subscription->{subscriptionid} - } - )->as_list; - - $subscription->{additional_fields} = { - map { $additional_fields_by_id{$_->field_id} => $_->value } @additional_field_values - }; + if (%additional_fields_by_id) { + my %subscriptions_by_id = map { $_->{subscriptionid } => $_ } @{$results}; + my $field_values_rs = Koha::AdditionalFieldValues->search( + { + field_id => { -in => [ keys %additional_fields_by_id ] }, + record_id => { -in => [ keys %subscriptions_by_id ] } + } + ); + while (my $field_value = $field_values_rs->next) { + my $field_name = $additional_fields_by_id{$field_value->field_id}; + $subscriptions_by_id{$field_value->record_id}->{additional_fields}->{$field_name} = $field_value->value; } - else { + } + else { + for my $subscription ( @{$results} ) { $subscription->{additional_fields} = {}; } } + for my $subscription ( @{$results} ) { + $subscription->{cannotedit} = not can_edit_subscription( $subscription ); + $subscription->{cannotdisplay} = not can_show_subscription( $subscription ); + } + return wantarray ? @{$results} : { results => $results, total => $total_results }; } -- 2.48.1