Skip to content

Commit

Permalink
Avoid unnecessary information in query hints to improve query cache h…
Browse files Browse the repository at this point in the history
…it ratio

I've noticed that over time my query caches fill up with redundant queries, i. e. different cache entries for the DQL -> SQL translation that are exactly the same. For me, it's an issue because the cache entries fill up precious OPcache memory.

Further investigation revealed that the queries themselves do not differ, but only the query hints – that are part of the computed cache key – do.

In particular, only the value for the `WhereInWalker::HINT_PAGINATOR_ID_COUNT` query hint are different. Since `WhereInWalker` only needs to know _if_ there are matching IDs but not _how many_, we could avoid such cache misses by using just a boolean value as cache hint.
  • Loading branch information
mpdude committed Jun 30, 2021
1 parent 73aa6e8 commit de4134d
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Tools/Pagination/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public function getIterator()
$ids = array_map('current', $foundIdRows);

$this->appendTreeWalker($whereInQuery, WhereInWalker::class);
$whereInQuery->setHint(WhereInWalker::HINT_PAGINATOR_ID_COUNT, count($ids));
$whereInQuery->setHint(WhereInWalker::HINT_PAGINATOR_ID_COUNT, $ids !== []);
$whereInQuery->setFirstResult(null)->setMaxResults(null);
$whereInQuery->setParameter(WhereInWalker::PAGINATOR_ID_ALIAS, $ids);
$whereInQuery->setCacheable($this->query->isCacheable());
Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/ORM/Tools/Pagination/WhereInWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ public function walkSelectStatement(SelectStatement $AST)
$pathExpression = new PathExpression(PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION, $rootAlias, $identifierFieldName);
$pathExpression->type = $pathType;

$count = $this->_getQuery()->getHint(self::HINT_PAGINATOR_ID_COUNT);
$hasIds = $this->_getQuery()->getHint(self::HINT_PAGINATOR_ID_COUNT);

if ($count > 0) {
if ($hasIds) {
$arithmeticExpression = new ArithmeticExpression();
$arithmeticExpression->simpleArithmeticExpression = new SimpleArithmeticExpression(
[$pathExpression]
Expand Down

0 comments on commit de4134d

Please sign in to comment.