N + 1 round trip problem
See original GitHub issueDoes this library handle nested models (joins) in a single query from the server to the DB? For example
user {
id
posts {
id
}
}
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:25 (7 by maintainers)
Top Results From Across the Web
Distance Problems - Round Trip - Dominate the GMAT
These problem solving questions fall into one of three categories: Motion in Opposite Directions ... In this lesson we'll cover Round-Trip Motion Questions....
Read more >Round Trip Distance Problem-Find the Time - YouTube
Learn how to find the time a train traveled for a round trip situation.For more help, check out my website: http://mathandstatshelp.com.
Read more >Distance Traveled - Roundtrip (System of Equations Word ...
How to solve distance travelled problems when the boat, plane or object is making a roundtrip in this free math video tutorial by...
Read more >Distance Problem: Round-Trip (Equal Distances) - YouTube
A car travels from Dino's Den to work in 2 hours. Eight hours later, it returns to Dino's Den at a speed 20...
Read more >Understanding and fixing N+1 query - Medium
... is exactly what happens when we have a N+1 query problem. Your ORM is forced by your code to do N additional...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Another option is to use a lazy-loader for SQLAlchemy that is more intelligent about bulk loading. We wrote a custom loader that will inspect the DB session and bulk-load all relations on similar models whenever a relation is lazy-loaded. The result should be the equivalent to using
subqueryloadon all relations upfront, but without needing any custom code to do so. The loader we wrote is available here in case it’s helpful to anyone else: https://github.com/operator/sqlalchemy_bulk_lazy_loaderThank you for that blog post @yfilali, that was really helpful.
Your
optimize_resolvemethod works pretty well for me. By usingjoinedloadinstead ofsubqueryload, I was able to reduce a deeply nested GraphQL query into a single database query. I had to add all paths, however, not only the leaves (simply removed theif not p.isLeafpart when assembling the joins).Another small problem is that in
resolve_relatedyou need to translate the field names from GraphQL (camel case) to SQLAlchemy (snake case) withfield = to_snake_case(field), otherwise it doesn’t work with table columns with underscores in them. You can import theto_snake_casemethod fromgraphene.utils.str_converters.I feel something like this should be really integrated into graphene-sqlalchemy. Or at least it should provide some helper functions which make it easy to implement such optimized querying, with various loading strategies as options. After all, one of the main advantages of GraphQL over REST is that it solves the N+1 problem. This advantage would be greater if it existed also on the level of the database, not only on the network level.