question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

N + 1 round trip problem

See original GitHub issue

Does 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:open
  • Created 7 years ago
  • Reactions:1
  • Comments:25 (7 by maintainers)

github_iconTop GitHub Comments

7reactions
chanindcommented, Jun 8, 2017

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 subqueryload on 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_loader

5reactions
Citocommented, May 27, 2017

Thank you for that blog post @yfilali, that was really helpful.

Your optimize_resolve method works pretty well for me. By using joinedload instead of subqueryload, 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 the if not p.isLeaf part when assembling the joins).

Another small problem is that in resolve_related you need to translate the field names from GraphQL (camel case) to SQLAlchemy (snake case) with field = to_snake_case(field), otherwise it doesn’t work with table columns with underscores in them. You can import the to_snake_case method from graphene.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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found