`select_related` and `prefetch_related`
When I inspected the actual SQL queries being run against DB, I found that for foreignkey and manytomany relationships, we have the `n+1` problem. For example, suppose there are 5 teams, each with 11 members, this query: ``` query { teams { edges { node { members { edges { node { name } } } } } } } ``` will result in 1 query for selecting `teams`, and 5 more queries for selecting members of each team. 6 queries in total. Normally, we would do a query like this `Team.objects.all().prefetch_related('members')` to reduce to 2 queries. I think it would be extremely beneficial if `DjangoObjectType` can detect special fields: - ForeignField - OneToOneField - OneToManyField - ManyToManyField and apply appropriate `prefetch_related` and/or `select_related` when such fields are present in graph queries.
Posted by khanetor on Nov 13, 2016, 3:05 PM
Tags
Related issues
- struct's lifetime requirements can be bypassed via covariant field assignment
- Proposal to Reorganize the Moderation Team and Review Moderation Policies
- Progressive FPS degradation appears to be related to dota2.com — blocking the domain prevents the performance loss
- proposal: container/...: generic collection types
- Better Custom Endpoint BYOK
- A concrete policy for backward compatibility until Godot 5.0
Raise real-world problems, find people facing the same issue, and discuss solutions.4,500 issues0 comments
github (Other)
struct's lifetime requirements can be bypassed via covariant field assignment
Proposal to Reorganize the Moderation Team and Review Moderation Policies
Progressive FPS degradation appears to be related to dota2.com — blocking the domain prevents the performance loss
proposal: container/...: generic collection types
Better Custom Endpoint BYOK
A concrete policy for backward compatibility until Godot 5.0
New MTP models not supported
Tracking: 429 / Capacity Issues
`select_related` and `prefetch_related`
When I inspected the actual SQL queries being run against DB, I found that for foreignkey and manytomany relationships, we have the `n+1` problem. For example, suppose there are 5 teams, each with 11 members, this query: ``` query { teams { edges { node { members { edges { node { name } } } } } } } ``` will result in 1 query for selecting `teams`, and 5 more queries for selecting members of each team. 6 queries in total. Normally, we would do a query like this `Team.objects.all().prefetch_related('members')` to reduce to 2 queries. I think it would be extremely beneficial if `DjangoObjectType` can detect special fields: - ForeignField - OneToOneField - OneToManyField - ManyToManyField and apply appropriate `prefetch_related` and/or `select_related` when such fields are present in graph queries.
Comments (0)
No comments yet. Start the discussion.