Open erez-rabih opened 7 years ago
Yes, Korma fetches relations lazily for each entity so this is n+1 queries. You should be able to avoid it by using join
instead of with
.
I used join but the returned map is really hard to understand:
I ended up with two queries, much similar to the way you handle has_many relations. Is there a reason the same trick is not done on many_to_many relations?
Yes, join
has that drawback.
I am not sure what you mean by "the way Korma handles has-many relations". Has-many generates n+1 queries as well but without join table.
There is a with-batch directive which allows you to fetch with two queries instead of n+1 but it only applies to has-many relations. I was wondering if there's anything preventing us from implementing the same for many-to-many.
Right. I am not sure why it hasn't been implemented for many-to-many. PR welcome :)
I guess similar problem is also meant in this issue: https://github.com/korma/korma.incubator/issues/7
A PR addressing this issue would be welcome.
I'm closing older tickets. Is this still a priority?
If there hasn't been a response to this issue in 2 weeks, I'll close the ticket.
I believe it is still relevant.
We have a many to many connection between users and accounts There is a join table of users_accounts When we
(select users (with accounts))
:We only see here one of the second query but in reality the second query repeats for each user.
Isn't this N+1 queries? Is there a way to avoid it?