Skip to content

Commit

Permalink
CsvResultSetUtils: Query results in bulk..
Browse files Browse the repository at this point in the history
..if there's no limit imposed. PDO runs queries in buffered mode
by default. Fetching without a limit needlessly increases the risk
to require more memory than available.
  • Loading branch information
nilmerg committed Mar 22, 2024
1 parent 84735b6 commit 9de4e33
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions library/Icingadb/Data/CsvResultSetUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,33 @@ public static function stream(Query $query): void
$query->setResultSetClass(__CLASS__);
}

foreach ($query->execute()->disableCache() as $i => $keysAndValues) {
if ($i === 0) {
echo implode(',', array_keys($keysAndValues));
}
if ($query->hasLimit()) {
// Custom limits should still apply
$query->peekAhead(false);
$offset = $query->getOffset();
} else {
$query->limit(1000);
$query->peekAhead();
$offset = 0;
}

echo "\r\n";
do {
$query->offset($offset);
$result = $query->execute()->disableCache();
foreach ($result as $i => $keysAndValues) {
if ($i === 0) {
echo implode(',', array_keys($keysAndValues));
}

echo implode(',', array_values($keysAndValues));
}
echo "\r\n";

echo implode(',', array_values($keysAndValues));

JsonResultSet::giveMeMoreTime();
}

$offset += 1000;
} while ($result->hasMore());

exit;
}
Expand Down

0 comments on commit 9de4e33

Please sign in to comment.