mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
DDC-1081: Unnecessary JOIN when selecting ManyToMany/Join Table by ID. #1354
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 @doctrinebot on GitHub (Mar 27, 2011).
Originally assigned to: @billschaller on GitHub.
Jira issue originally created by user putgeminmouth:
With the schema:
Given the following DQL,
Doctrine Generates this SQL
Which unncessarily joins against Tag, given that the foreign key Tag.id is also found in Tag_Image.tag.
@doctrinebot commented on GitHub (Mar 27, 2011):
Comment created by @beberlei:
This is not a bug, but expected behavior.
You can select against the alias if its on the owning side of the association:
In this case it is not a left join though, if you want a left join you HAVE to join.
@doctrinebot commented on GitHub (Mar 27, 2011):
Comment created by putgeminmouth:
There is no owning isde of the association, you can clearly see there is an association table/entity.
I can't understand how this behavior is expected. If no properties of Tag are selected for, there is no need to join against Tag since the id is already available via the association table.
@doctrinebot commented on GitHub (Mar 28, 2011):
Comment created by @beberlei:
I misread the mappings, sorry, i though its a @OneToOne but its actually an assocition entity with @OneToOnes.
Can you show me the Image::$tags mapping also?
@doctrinebot commented on GitHub (Mar 28, 2011):
Comment created by putgeminmouth:
That is correct, thanks for taking another look at this.
Sorry I had forgotten to include that information.
@doctrinebot commented on GitHub (Mar 28, 2011):
Comment created by @beberlei:
The targetEntity is wrong. I suppose it should be Image_Tag or not? If it should be Tag, then you don't need that Image_Tag entity at all.
In that case i have to check if you can use the shortcut notation, however it will again not work with the left join - only inner. This is an assumption the ORM makes and there is not yet code included for the optimization. This is not a bug, but an improvement ticket. The functionality works.
@doctrinebot commented on GitHub (Mar 28, 2011):
Comment created by putgeminmouth:
No argument on the ticket type...
Ahh, I store some metadata in Tag_Image, which is why I manage it explicitly.
In any case thanks for looking at this.
@doctrinebot commented on GitHub (Mar 28, 2011):
Comment created by @beberlei:
If you change the targetEntity to Tag_Image then it might already be enough to get this working without another join.
@doctrinebot commented on GitHub (Mar 28, 2011):
Comment created by putgeminmouth:
With this change, the original query is invalid:
Because i.tags of type Tag_Image has no field id
{quote}
[Semantical Error] line 0, col 138 near 'id=:tag ': Error: Class domain\Tag_Image has no field or association named id
{quote}
I attempt the logical modification:
and get
{quote}
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 't1_
{quote}
@billschaller commented on GitHub (Jan 6, 2016):
This appears to have been mostly a mapping/modelling conundrum.
@maximivanov commented on GitHub (May 13, 2018):
I do also see an unnecessary JOIN with many-to-many mapping. Quick example:
Post.php:
Tag.php:
Fetching with query builder:
Results in following SQL:
While I would expect query without an extra JOIN:
Is there a way to instruct doctrine to do so?
@Ocramius commented on GitHub (May 13, 2018):
Not really, but if you are optimising an SQL query, just keep it in SQL?
@maximivanov commented on GitHub (May 13, 2018):
Keeping plain SQL is a possible solution but not really convenient. I simplified query here to keep example short. In project I'm fetching entities, not just fields.
@kiler129 commented on GitHub (Jan 24, 2019):
It will be really good to be able to not join resulting table but just the m2m table. Is there any way @Ocramius to do that without going to native SQL (and hardcoding column names)?
@Ocramius commented on GitHub (Feb 3, 2019):
What's the big holdup with getting your hands dirty where needed? It's not like your schema will change each week, and if this can be improved in a demonstrable efficient way, then it should be a new issue. This has been closed for years now.