From 9de4e33f942b7e6b337511f5f5fa84497a51b180 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Wed, 20 Mar 2024 16:39:31 +0100 Subject: [PATCH] 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. --- library/Icingadb/Data/CsvResultSetUtils.php | 32 ++++++++++++++++----- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/library/Icingadb/Data/CsvResultSetUtils.php b/library/Icingadb/Data/CsvResultSetUtils.php index 254341af1..61995d3a2 100644 --- a/library/Icingadb/Data/CsvResultSetUtils.php +++ b/library/Icingadb/Data/CsvResultSetUtils.php @@ -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; }