while ($row=shift(@$cache) || shift@{$cache=$sth_o->fetchall_arrayref(undef,$max_rows) || []}) { $inserted += $sth2->execute(@$row); ... };