Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

with and many-to-many generates n+1 queries #371

Open
erez-rabih opened this issue Dec 22, 2016 · 9 comments
Open

with and many-to-many generates n+1 queries #371

erez-rabih opened this issue Dec 22, 2016 · 9 comments

Comments

@erez-rabih
Copy link

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)):

  1. There is a select query on the users table
  2. For each user, there is a separate select query on the accounts table to get all users accounts.
    This is the dry-run result:
dry run :: SELECT "users".* FROM "users" WHERE ("users"."source" = ?) ORDER BY "users"."id" DESC LIMIT 10 :: [sd]
dry run :: SELECT "accounts".* FROM "accounts" INNER JOIN "accounts_users" ON "accounts_users"."account_id" = "accounts"."id" WHERE ("accounts_users"."user_id" = ?) :: [1]

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?

@immoh
Copy link
Member

immoh commented Jan 4, 2017

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.

@erez-rabih
Copy link
Author

I used join but the returned map is really hard to understand:

  1. Columns that belong to both entities are named column and column_2
  2. The map is flat and there is no way to distinct between columns of entity 1 and entity 2

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?

@immoh
Copy link
Member

immoh commented Jan 4, 2017

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.

@erez-rabih
Copy link
Author

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.

@erez-rabih
Copy link
Author

See here:

(:has-one :belongs-to :many-to-many) (with* query sub-ent body-fn)

@immoh
Copy link
Member

immoh commented Jan 7, 2017

Right. I am not sure why it hasn't been implemented for many-to-many. PR welcome :)

@mrkam2
Copy link

mrkam2 commented Jun 23, 2017

I guess similar problem is also meant in this issue: korma/korma.incubator#7

@venantius
Copy link
Contributor

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.

@venantius venantius added the inactive-issue? This issue should be closed within 2 weeks of the most recent comment due to inactivity label Feb 27, 2018
@mrkam2
Copy link

mrkam2 commented Mar 8, 2018

I believe it is still relevant.

@venantius venantius removed the inactive-issue? This issue should be closed within 2 weeks of the most recent comment due to inactivity label Mar 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants