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 queries in execution dataloader docs

See original GitHub issue
  • What is the current behavior?

https://github.com/graphql-python/graphene/blame/master/docs/execution/dataloader.rst#L57-L58

>>> users = {user.id: user for user in User.objects.filter(id__in=keys)}
>>> print(users)
{1: <User: Tom>, 2: <User: Frank>, ...}

Database is hit once for each user.


  • What is the expected behavior?
>>> user_ids = User.objects.filter(id__in=keys).values_list('id', flat=True)
>>> users = User.objects.in_bulk(user_ids)
>>> print(users)
{1: <User: Tom>, 2: <User: Frank>, ...}

A single query can be used to fetch all user IDs and users


  • What is the motivation / use case for changing the behavior?

Performance

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
nickvellioscommented, Nov 2, 2022

Closing out this issue. Using graphene 2.x and having inconsistent results. Will test further when we upgrade.

1reaction
jkimbocommented, Nov 2, 2022

@nickvellios I’m not sure what you mean. Running User.objects.filter(id__in=keys) should only execute 1 db query. Are you seeing something different?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Solving N+1 in GraphQL Python With Dataloader - Jerry Ng
Tips and guide to handle N+1 problem with Dataloader in Django Graphene while developing GraphQL API in Python.
Read more >
GraphQL/Graphene: How to Solve the N+1 Problem With ...
Learn how to avoid one of the most common GraphQL performance pitfalls: the N+1 problem, in this deep dive by Cloud Academy engineer, ......
Read more >
Batching – A powerful way to solve N+1 queries every Rubyist ...
In this post I'm going to tell you about batching as a technique to help avoid N+1 queries, existing battle-tested tools like Haskell...
Read more >
How Federation handles the N+1 query problem
In a monolithic GraphQL server, the execution engine takes these steps: Resolve the Query.topReviews field, which returns a list of Review s.
Read more >
Query optimization - Prisma
This guide describes ways to optimize query performance, debug performance issues, and how to tackle common performance issues such as the ...
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