Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit de4134d

Browse files
committedJun 30, 2021
Avoid unnecessary information in query hints to improve query cache hit 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.
1 parent 73aa6e8 commit de4134d

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed
 

‎lib/Doctrine/ORM/Tools/Pagination/Paginator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public function getIterator()
163163
$ids = array_map('current', $foundIdRows);
164164

165165
$this->appendTreeWalker($whereInQuery, WhereInWalker::class);
166-
$whereInQuery->setHint(WhereInWalker::HINT_PAGINATOR_ID_COUNT, count($ids));
166+
$whereInQuery->setHint(WhereInWalker::HINT_PAGINATOR_ID_COUNT, $ids !== []);
167167
$whereInQuery->setFirstResult(null)->setMaxResults(null);
168168
$whereInQuery->setParameter(WhereInWalker::PAGINATOR_ID_ALIAS, $ids);
169169
$whereInQuery->setCacheable($this->query->isCacheable());

‎lib/Doctrine/ORM/Tools/Pagination/WhereInWalker.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ public function walkSelectStatement(SelectStatement $AST)
9797
$pathExpression = new PathExpression(PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION, $rootAlias, $identifierFieldName);
9898
$pathExpression->type = $pathType;
9999

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

102-
if ($count > 0) {
102+
if ($hasIds) {
103103
$arithmeticExpression = new ArithmeticExpression();
104104
$arithmeticExpression->simpleArithmeticExpression = new SimpleArithmeticExpression(
105105
[$pathExpression]

0 commit comments

Comments
 (0)
Please sign in to comment.