From 5244baa2929268601153ed561913f7e9cc0e0f62 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Thu, 19 Sep 2019 11:13:28 -0300 Subject: [PATCH] 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 Signed-off-by: Liz Rea Signed-off-by: Tomas Cohen Arazi Signed-off-by: Martin Renvoize (cherry picked from commit bca4453c50d8b5b1d3c7029ded2b522a9a2bf868) Signed-off-by: Fridolin Somers (cherry picked from commit b332b365edbab4197580ecd4b3fe684dadfe070f) Signed-off-by: Lucas Gass --- C4/Reports/Guided.pm | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/C4/Reports/Guided.pm b/C4/Reports/Guided.pm index 4d47358569..d3ad9a5f99 100644 --- a/C4/Reports/Guided.pm +++ b/C4/Reports/Guided.pm @@ -413,14 +413,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; } -- 2.39.5