From 8e5380220cdae0ec19626c9513a200dd26e5829c Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Thu, 26 Sep 2019 12:50:51 +0200 Subject: [PATCH] SimpleQuery: Cache count query result and use it in `hasResult()` Does not affect views which do not run a count query. (e.g. dashlets) Though, this is a quick win for all other views with which the user interacts directly and gets the desired result quicker than before. refs #3905 refs #3836 --- library/Icinga/Data/SimpleQuery.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/library/Icinga/Data/SimpleQuery.php b/library/Icinga/Data/SimpleQuery.php index 2d0eeef6f..e4b9f1f9b 100644 --- a/library/Icinga/Data/SimpleQuery.php +++ b/library/Icinga/Data/SimpleQuery.php @@ -36,6 +36,13 @@ class SimpleQuery implements QueryInterface, Queryable, Iterator */ protected $iteratorPosition; + /** + * The amount of rows previously calculated + * + * @var int + */ + protected $cachedCount; + /** * The target you are going to query * @@ -450,7 +457,7 @@ class SimpleQuery implements QueryInterface, Queryable, Iterator */ public function hasResult() { - return $this->iteratorPosition !== null || $this->fetchRow() !== false; + return $this->cachedCount > 0 || $this->iteratorPosition !== null || $this->fetchRow() !== false; } /** @@ -647,6 +654,7 @@ class SimpleQuery implements QueryInterface, Queryable, Iterator $query->limit(0, 0); Benchmark::measure('Counting all results started'); $count = $this->ds->count($query); + $this->cachedCount = $count; Benchmark::measure('Counting all results finished'); return $count; }