Make auto generated relation tables accessible for queries #6572

Open
opened 2026-01-22 15:35:08 +01:00 by admin · 0 comments
Owner

Originally created by @Jurek-Raben on GitHub (Nov 27, 2020).

Doctrine currently seems to be blind for its own auto generated relation tables. This leads into several performance and other kind of problems.

For example, if you define a many-many-relation in the two Entities thru the annotations, and later have to access info thru this relation, it leads into several performance problems. Let's say you have a entity A and B, and now want to count the number of Bs related to A for each entry of A in an inner select, you currently have to use the sql command MEMBER OF:

(SELECT COUNT(at.id) FROM entityA at WHERE entityB MEMBER OF entityA.entitiesB)

It seems to happen often then, that Doctrine now converts this kind of simple information request to at least two additional subqueries, in addition to this one! In a complex query context, this will lead into drastical performance problems. Let's say that MEMBER OF seems to be a very costly function.

On the other hand, if you could access the relation table itself, you simply could do a:

(SELECT COUNT(rt.entity_a_id) FROM entityA_entityB rt WHERE rt.entity_b_id = entityB.id)

This one is a ton faster than the query above. So why are the relation tables invisible? It seems to me a unneccessary handicap for the programmer. Why is that?

Originally created by @Jurek-Raben on GitHub (Nov 27, 2020). Doctrine currently seems to be blind for its own auto generated relation tables. This leads into several performance and other kind of problems. For example, if you define a many-many-relation in the two Entities thru the annotations, and later have to access info thru this relation, it leads into several performance problems. Let's say you have a entity A and B, and now want to count the number of Bs related to A for each entry of A in an inner select, you currently have to use the sql command MEMBER OF: `(SELECT COUNT(at.id) FROM entityA at WHERE entityB MEMBER OF entityA.entitiesB)` It seems to happen often then, that Doctrine now converts this kind of simple information request to at least two additional subqueries, in addition to this one! In a complex query context, this will lead into drastical performance problems. Let's say that MEMBER OF seems to be a very costly function. On the other hand, if you could access the relation table itself, you simply could do a: `(SELECT COUNT(rt.entity_a_id) FROM entityA_entityB rt WHERE rt.entity_b_id = entityB.id)` This one is a ton faster than the query above. So why are the relation tables invisible? It seems to me a unneccessary handicap for the programmer. Why is that?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6572