[Koha-bugs] [Bug 30169] Prefetching is expensive for performance

bugzilla-daemon at bugs.koha-community.org bugzilla-daemon at bugs.koha-community.org
Thu Feb 24 16:45:56 CET 2022


https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=30169

--- Comment #3 from Martin Renvoize <martin.renvoize at ptfs-europe.com> ---
I just wrote a benchmark for this:

#!/usr/bin/env perl

use strict;
use warnings;

use Benchmark qw/cmpthese timethese/;
use Koha::Libraries;
use Koha::Database;

my $schema = Koha::Database->schema;

cmpthese(
    -10,
    {
        kohaPrefetch => sub {
            my $checkouts = Koha::Checkouts->search({},{ prefetch => [ 'item',
'patron' ]});
            while ( my $checkout = $checkouts->next ) {
                my $item_id = $checkout->item->itemnumber;
                my $patron_id = $checkout->patron->borrowernumber;
            }
        },
        kohaJoin => sub {
            my $checkouts = Koha::Checkouts->search({});
            while ( my $checkout = $checkouts->next ) {
                my $item_id = $checkout->item->itemnumber;
                my $patron_id = $checkout->patron->borrowernumber;
            }
        },
        dbicPrefetch => sub {
            my $checkouts = $schema->resultset('Issue')->search({},{ prefetch
=> [ 'item', 'patron' ]});
            while ( my $checkout = $checkouts->next ) {
                my $item_id = $checkout->item->itemnumber;
                my $patron_id = $checkout->patron->borrowernumber;
            }
        },
        dbicJoin => sub {
            my $checkouts = $schema->resultset('Issue')->search({});
            while ( my $checkout = $checkouts->next ) {
                my $item_id = $checkout->item->itemnumber;
                my $patron_id = $checkout->patron->borrowernumber;
            }
        }
    }
);

It suggests prefetch is quicker in my findings.. but it's testing at a slightly
different level.. I'm testing one prefetch over a large resultset.. you were
testing lots of prefetches over a single result... I'll write a benchmark for
that too.. 

I think it's object instantiation that's at fault here..  you are effectively
instantiating the object for each iteration whereas I'm instantiating one
object for all results.

In short.. it depends on how we code as to whether adding prefetch is a good
idea or not.

-- 
You are receiving this mail because:
You are watching all bug changes.
You are the assignee for the bug.


More information about the Koha-bugs mailing list