Skip to content

Commit 9de4e33

Browse files
committed
CsvResultSetUtils: Query results in bulk..
..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.
1 parent 84735b6 commit 9de4e33

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

library/Icingadb/Data/CsvResultSetUtils.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,33 @@ public static function stream(Query $query): void
7373
$query->setResultSetClass(__CLASS__);
7474
}
7575

76-
foreach ($query->execute()->disableCache() as $i => $keysAndValues) {
77-
if ($i === 0) {
78-
echo implode(',', array_keys($keysAndValues));
79-
}
76+
if ($query->hasLimit()) {
77+
// Custom limits should still apply
78+
$query->peekAhead(false);
79+
$offset = $query->getOffset();
80+
} else {
81+
$query->limit(1000);
82+
$query->peekAhead();
83+
$offset = 0;
84+
}
8085

81-
echo "\r\n";
86+
do {
87+
$query->offset($offset);
88+
$result = $query->execute()->disableCache();
89+
foreach ($result as $i => $keysAndValues) {
90+
if ($i === 0) {
91+
echo implode(',', array_keys($keysAndValues));
92+
}
8293

83-
echo implode(',', array_values($keysAndValues));
84-
}
94+
echo "\r\n";
95+
96+
echo implode(',', array_values($keysAndValues));
97+
98+
JsonResultSet::giveMeMoreTime();
99+
}
100+
101+
$offset += 1000;
102+
} while ($result->hasMore());
85103

86104
exit;
87105
}

0 commit comments

Comments
 (0)