Bug 23624: (QA follow-up) Optimize even more

This patch makes counting the results have no memory footprint by
leveraging on the DB to count the rows.

To test:
- Without this path, run:
  $ kshell
 k$ prove t/db_dependent/Reports/Guided.t
=> SUCCESS: Tests pass
- Apply this patch
- Run:
 k$ prove t/db_dependent/Reports/Guided.t
=> SUCCESS: Tests still pass!

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Liz Rea <wizzyrea@gmail.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
This commit is contained in:
Tomas Cohen Arazi 2019-09-19 11:13:28 -03:00 committed by Martin Renvoize
parent 7958e7f3f9
commit bca4453c50
No known key found for this signature in database
GPG Key ID: 422B469130441A0F
1 changed files with 12 additions and 7 deletions

View File

@ -418,14 +418,19 @@ sub get_criteria {
sub nb_rows {
my $sql = shift or return;
my $sth = C4::Context->dbh->prepare($sql);
$sth->execute();
my $n = 0;
# Loop through the complete results, fetching 1,000 rows at a time. This
# lowers memory requirements but increases execution time.
while (my $rows = $sth->fetchall_arrayref(undef, 1000)) {
$n += @$rows;
my $derived_name = 'xxx';
# make sure the derived table name is not already used
while ( $sql =~ m/$derived_name/ ) {
$derived_name .= 'x';
}
my $sth = C4::Context->dbh->prepare(qq{
SELECT COUNT(*) FROM
( $sql ) $derived_name
});
$sth->execute();
my $n = $sth->fetch->[0];
return $n;
}