Extra select query when association on entity with DiscriminatorMap #5686

Closed
opened 2026-01-22 15:14:31 +01:00 by admin · 2 comments
Owner

Originally created by @Seb33300 on GitHub (Sep 5, 2017).

Originally assigned to: @Ocramius on GitHub.

Hello,

On my Entity A, I have an association to an Entity B which is an abstract entity with a DiscriminatorMap.

Every time I do a getReference() on my Entity A, if I check the logs, I can see 2 queries:

  • the select query to get the Entity A data (good)
  • a select query to get Entity B data (why? this should be call only when needed on lazy associations)

This seems to only happen because entity B is an abstract Entity (who can be a different class from the discriminator).
If I change the association to force it to be one class of the discriminator (not an abstract), I do not have the extra select query.

Seb

Originally created by @Seb33300 on GitHub (Sep 5, 2017). Originally assigned to: @Ocramius on GitHub. Hello, On my Entity A, I have an association to an Entity B which is an abstract entity with a DiscriminatorMap. Every time I do a getReference() on my Entity A, if I check the logs, I can see 2 queries: - the select query to get the Entity A data (good) - a select query to get Entity B data (why? this should be call only when needed on lazy associations) This seems to only happen because entity B is an abstract Entity (who can be a different class from the discriminator). If I change the association to force it to be one class of the discriminator (not an abstract), I do not have the extra select query. Seb
admin added the Can't FixQuestion labels 2026-01-22 15:14:31 +01:00
admin closed this issue 2026-01-22 15:14:31 +01:00
Author
Owner

@malukenho commented on GitHub (Sep 5, 2017):

Try to read about n + 1 problem. I think that is what you are referencing to.

@malukenho commented on GitHub (Sep 5, 2017): Try to read about `n + 1` problem. I think that is what you are referencing to.
Author
Owner

@Ocramius commented on GitHub (Sep 5, 2017):

Every time I do a getReference() on my Entity A, if I check the logs, I can see 2 queries

This is normal, because there is no way to get a reference of the correct proxy type for B. The association cannot be lazily loaded because there must be a query looking at least at the discriminator column value in order to decide what the runtime type of the reference is.

See also:

@Ocramius commented on GitHub (Sep 5, 2017): > Every time I do a getReference() on my Entity A, if I check the logs, I can see 2 queries This is normal, because there is no way to get a reference of the correct proxy type for `B`. The association cannot be lazily loaded because there must be a query looking at least at the discriminator column value in order to decide what the runtime type of the reference is. See also: * http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html#performance-impact * https://tideways.io/profiler/blog/5-doctrine-orm-performance-traps-you-should-avoid * https://stackoverflow.com/a/7101339/347063
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5686