-
-
Notifications
You must be signed in to change notification settings - Fork 115
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
Optimiser not working with custom prefetch query #389
Comments
I think this should be possible fairly easily. If I understand this correctly, here an inspection of the field's prefetches should be added. If a |
Hey @diesieben07 . Thanks for trying to help with this :) Left me know if you need any help with that. Either ping me here or on our discord community |
Thank you for the heads up @bellini666. |
Thanks @diesieben07 :) Will take a look at it very soon! |
@bellini666 and @diesieben07 any update on this? |
@Sanyambansal76 I will hopefully have time to look at the remaining work on my pull request on the weekend. |
The strawberry django optimiser is not working as expected when we provide hints (like custom prefetch) for a field in a type.
It results in n+1 queries when we do that
Describe the Bug
Suppose I have 2 models Model and ModelVariables, ModelVariables has a foreign key reference to Model. So, in django we can refer ModelVariables from Model using the
related_name
.Consider my models to be something like this:
Since,
ModelVariable
has a foreign key toModel
. I can get all theModelVariable
for aModel
using ->Model.model_variables.all()
Now, for the above models I created strawberry django types in the following way
Now, in my ModelType, I want
editable_model_variables
field to return all ModelVariables that are editable, so I added a prefetch_related for it and updated the query. Which works.Now, when I execute a query and mention
related_field
in the query. It causes n+1 queries. The obvious reason is that, in my prefetch related query I did not select_related therelated_field
.AFAIK the optimiser takes these hints and not just override the optimised queries.
I also tried passing
field_name="model_variables"
to the@strawberry_django.field
decorator for hinting but that didn't work too.System Information
Upvote & Fund
The text was updated successfully, but these errors were encountered: