mirror of
https://github.com/doctrine/orm.git
synced 2026-03-24 06:52:09 +01:00
Second Level Cache makes 350 requests to Redis for 20-rows result #6237
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @yura3d on GitHub (May 14, 2019).
Support Question
Hello. I have an entity that has 16 ManyToOne associations to other entities. All the entities and associations are configured for using Second Level Cache.
I was very interested to see how efficiently Doctrine uses cache for reading, so I added logging to cache driver Doctrine\Common\Cache\RedisCache (to doFetch() and doFetchMultiple() methods).
I made a cacheable query for 20 rows with fetch-join associations (fetch mode is eager) and what I saw... These 20 rows of result produces for about 350 requests to Redis server. For the query root entities everything is fine: only 1 request is used for retrieve them through doFetchMultiple(). But for retrieving associations Doctrine makes 16 separate doFetch() requests for every association in every root entity (similar to Cartesian product). These requests happends even for equal association (there is a case when 20 root entities has equal entity in ManyToOne association, but Doctrine sends 20 separate GETs to Redis server for every root entity).
The log for the query above is here:

I don't understand why doFetchMultiple() is not using to retrieve associations. It could help to reduce the number of queries to cache from 350 to 4-5:
@Ocramius commented on GitHub (May 14, 2019):
Would you be able to write a test case where this can be reproduced?
The test would:
@lcobucci commented on GitHub (May 14, 2019):
@yura3d
it would also be nice to know if you are also using the L2C query cache for that particular use case,perhaps you can send us the query (builder) you have too?Edit: Nevermind, I double checked your text and it seems that you're using it
@yura3d commented on GitHub (May 14, 2019):
@Ocramius, @lcobucci Thanks for your replies, I will publish test with my query and cache mock soon. And yes, I'm using L2C query cache (setCacheable(true) called on query object).
@yura3d commented on GitHub (May 15, 2019):
The test is here, results are here
Should I create a PR with added test?
@lcobucci commented on GitHub (May 15, 2019):
@yura3d that's awesome! Please send the PR, I'll check it ASAP