Great ideas, thank you. However
my $rowcache;
while (my $aref = shift(@$rowcache)
|| shift ( @{$rowcache= $sth->fetchall_arrayref(undef, $max_rows)} )
)
fails (presumably when there is nothing left) with
Can't use an undefined value as an ARRAY reference
So some refactoring is neede.