You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
You should see the rails console filled with tonnes of DB queries.
Then:
Open up app/controllers/inspections_controller.rb
Comment out the self.sideload_allowlist = code
Visit the url above again
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.
The text was updated successfully, but these errors were encountered:
@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?
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:
rails db:migrate
rails db:seed
rails server -p 5000
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:
app/controllers/inspections_controller.rb
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.
The text was updated successfully, but these errors were encountered: