-
Notifications
You must be signed in to change notification settings - Fork 766
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
Revert field resolver logic to fix poor query performance #1401
Revert field resolver logic to fix poor query performance #1401
Conversation
This reverts the change to `convert_field_to_djangomodel` introduced in graphql-python#1315 for the reasons discussed here https://github.com/graphql-python/graphene-django/pull/1315/files#r1015659857. As mentioned there, without reverting this code, "queries are forced every time an object is resolved, making an exponential number of queries when nesting without any possibility of optimizing". That regression prevented `graphene-django-optimizer` from working with `graphene-django` v3.0.0b9+ (where this change first was published), as discussed in graphql-python#1356 (comment), tfoxy/graphene-django-optimizer#86, and tfoxy/graphene-django-optimizer#83 (comment). For now, this marks the two tests that depended on this problematic code as "expected to fail", and perhaps they can be reintroduced if there's a way to support this logic in a way that does not prevent `select_related` and `prefetch_related` query-optimization and introduce nested N+1s. As mentioned here graphql-python#1315 (comment), this is blocking upgrade to graphene-django v3 for many users, and fixing this would allow many to begin upgrading and contributing to keep graphene-django going.
Thanks for taking the time to look into this @sjdemartini! It's great to see more people active here after a while :) It's a bit worrying to remove/merge given those tests failing, if people are relying on using the |
Thanks for taking a look and commenting @jaw9c! Agreed it's nice to see graphene-django coming back! I understand the concern, but I think this recent regression is a larger problem. Here are some thoughts:
Thanks again for discussing! |
btw I just noticed there was an attempt to improve the performance of this code in #1391, which is maybe a good starting place to explore this later, but I tested that code locally with graphene-django-optimizer and it still causes 8 performance-related test failures in that project unfortunately (same as without that PR). |
Hey guys, have you tested #1391? |
@sjdemartini Thank you for taking the time to investigate more about this issue. I have worked on this PR #1391 as a work around to reduce DB queries from 2 queries to 1 each time a FK/1:1 field is resolved in an attempt to enhance the performance a little while keeping the functionality proposed in #1315. Regarding |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are still tests (skipped) testing non-exiting functionality that should be removed
Thanks for the additional discussion!
@mahdyhamad I've gone ahead and deleted the two tests I'd marked with |
Sounds as a good idea to keep some temporary documentation of this discussion 💯 Thank you @sjdemartini |
@jaw9c any thoughts? I think this is good to, and we can address this in the release notes to avoid breaking code. |
Thanks for the stamp firaskafri! I've also just added a new unit-test that validates For instance, adding back the resolver code now gives a test failure showing:
|
e0f1814
to
f3f6e08
Compare
…related This test passes after reverting the `CustomField` resolver change introduced in graphql-python#1315, but fails with that resolver code present. For instance, adding back the resolver code gives a test failure showing: ``` Failed: Expected to perform 2 queries but 11 were done ``` This should ensure there aren't regressions that prevent query-optimization in the future.
f3f6e08
to
5e8952d
Compare
Whoops, didn't realize |
@sjdemartini Sounds good to me, thought I'd raise since the tests seem worrying but agree that it's something that we can work towards. I'm not convinced it's a particularly good strategy to use the @firaskafri I think it would be worth noting this change in the release notes 👍 |
NOTE: For now, we do not expect this get_queryset method to be called for nested | ||
objects, as the original attempt to do so prevented SQL query-optimization with | ||
`select_related`/`prefetch_related` and caused N+1 queries. See discussions here | ||
https://github.com/graphql-python/graphene-django/pull/1315/files#r1015659857 | ||
and here https://github.com/graphql-python/graphene-django/pull/1401. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
The new 3.0.2 release includes a fix for the SQL query performance regression https://github.com/graphql-python/graphene-django/releases/tag/v3.0.2 (graphql-python/graphene-django#1401) which was causing test failures on this branch.
This reverts the change to
convert_field_to_djangomodel
introduced in #1315 for the reasons discussed herehttps://github.com/graphql-python/graphene-django/pull/1315/files#r1015659857. As mentioned there, without reverting this code, "queries are forced every time an object is resolved, making an exponential number of queries when nesting without any possibility of optimizing".
That regression prevented
graphene-django-optimizer
from working withgraphene-django
v3.0.0b9+ (where this change first was published), as discussed in#1356 (comment), tfoxy/graphene-django-optimizer#86, and tfoxy/graphene-django-optimizer#83 (comment).
For now, this marks the two tests that depended on this problematic code as "expected to fail", and perhaps they can be reintroduced if there's a way to support this logic in a way that does not prevent
select_related
andprefetch_related
query-optimization and introduce nested N+1s.As mentioned here #1315 (comment), the query performance regression in
graphene-django
is blocking upgrade to graphene-django v3 for many users, and fixing this would allow many to begin upgrading and contributing to keep graphene-django going.Thanks again @firaskafri for pushing
graphene-django
along, and I appreciate your consideration in merging/publishing this! 🙏I have tested this change with
graphene-django-optimizer
, and it should resolve the v3.0.0+ compatibility question/issue for that library mentioned here #1356 and filed here tfoxy/graphene-django-optimizer#86.