From 8ca05344a5b3254ea0615e721e0b7f621e00137e Mon Sep 17 00:00:00 2001 From: ivan Date: Wed, 22 Apr 2009 20:24:52 +0000 Subject: [PATCH] hopefully better performance running the big query once and then fetching results with a cursor, rather than running it multiple times with an OFFSET and LIMIT, RT#4412 --- FS/FS/Cron/bill.pm | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/FS/FS/Cron/bill.pm b/FS/FS/Cron/bill.pm index 899b117b3..4e8817343 100644 --- a/FS/FS/Cron/bill.pm +++ b/FS/FS/Cron/bill.pm @@ -95,24 +95,23 @@ END push @search, "( $where_pkg OR $where_event )"; - my $prev_custnum = 0; - while ( 1 ) { + ### + # get a list of custnums + ### - ### - # get a list of custnums - ### + warn "searching for customers:\n". join("\n", @search). "\n" + if $opt{'v'} || $opt{'l'}; + + dbh->do( + "DECLARE cron_bill_cursor CURSOR WITH HOLD FOR ". #no WITH HOLD for mysql? + " SELECT custnum FROM cust_main ". + " WHERE ". join(' AND ', @search). + " ORDER BY custnum " #LIMIT 1000 " + ) or die dbh->errstr; - warn "searching for customers:\n". - join("\n", @search). - "custnum > $prev_custnum\n" - if $opt{'v'} || $opt{'l'}; + while ( 1 ) { - my $sth = dbh->prepare( - "SELECT custnum FROM cust_main". - " WHERE ". join(' AND ', @search). - " AND custnum > $prev_custnum ". - " ORDER BY custnum LIMIT 1000 " - ) or die dbh->errstr; + my $sth = dbh->prepare('FETCH 1000 FROM cron_bill_cursor'); #mysql? $sth->execute or die $sth->errstr; @@ -120,8 +119,6 @@ END last unless scalar(@custnums); - $prev_custnum = $custnums[-1]; - ### # for each custnum, queue or make one customer object and bill # (one at a time, to reduce memory footprint with large #s of customers) -- 2.11.0