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

[Bug] Sideload Allowlist causes n+1 queries #487

Open
Reizar opened this issue Nov 21, 2024 · 1 comment
Open

[Bug] Sideload Allowlist causes n+1 queries #487

Reizar opened this issue Nov 21, 2024 · 1 comment

Comments

@Reizar
Copy link

Reizar commented Nov 21, 2024

Hi,

We've been getting hit hard with n+1 queries in our codebase and its been causing quite a few issues. We do a lot of nested includes in our models but I thought Graphiti was supposed to handle loading them without n+1 queries.

I set up a new codebase and mirrored some of our models/associations to see if I could work out what the cause of the issue is. And I've found that when the sideload_allowlist is used, the n+1 queries happen. When I remove it, eager loading is used correctly.

Please see this codebase: https://github.com/Reizar/graphiti-nplus1-example for a reproduction of the issue.

Steps to reproduce:

  1. Clone codebase linked above.
  2. Run rails db:migrate
  3. Run rails db:seed
  4. Run rails server -p 5000
  5. Visit: http://127.0.0.1:5000/api/v1/inspections?include=tenancy.tenancy_memberships.user,tenancy.property&page[number]=1&page[size]=100

You should see the rails console filled with tonnes of DB queries.

Then:

  1. Open up app/controllers/inspections_controller.rb
  2. Comment out the self.sideload_allowlist = code
  3. Visit the url above again
  4. The queries should be reduced down to just the number of associations.

Feels like this is probably a bug, but it could also be an issue on our side of how we're using the sideload allowlist. Happy to hear any thoughts or provide additional information.

@jkeen
Copy link
Collaborator

jkeen commented Dec 12, 2024

@Reizar I don't think graphiti does anything super magical to avoid N+1 queries. When I've run into N+1s they've been solved by changing my base_scope in my resource to use Rails eager loading.

Thank you for setting up the example repo! When I looked at it briefly it seemed like you were getting N+1s on loading the user record with or without the sideload allow list, but I didn't look at it deep enough to fully understand your domain model and what .includes or .eager_loads you'd need to add to avoid that N+1. Have you tried modifying your base_scopes?

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

2 participants