Skip to content

Commit

Permalink
SimpleQuery: Cache count query result and use it in hasResult()
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nilmerg committed Oct 11, 2019
1 parent 908c408 commit 8e53802
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion library/Icinga/Data/SimpleQuery.php
Expand Up @@ -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
*
Expand Down Expand Up @@ -450,7 +457,7 @@ public function hasMore()
*/
public function hasResult()
{
return $this->iteratorPosition !== null || $this->fetchRow() !== false;
return $this->cachedCount > 0 || $this->iteratorPosition !== null || $this->fetchRow() !== false;
}

/**
Expand Down Expand Up @@ -647,6 +654,7 @@ public function count()
$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;
}
Expand Down

0 comments on commit 8e53802

Please sign in to comment.