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.

`findOne` optimization

See original GitHub issue

To quote from an earlier specs issue:

One of the main usecases of Photon Prisma Client is to use it inside a GraphQL server. The resolver system of a GraphQL server is prone to suffer from the N+1 problem. To mitigate this problem users can choose to implement the DataLoader pattern within the application server. This burdens our users with unnecessary work that is not related to their core domain. This issue explores how we could include a DataLoader implementation within Photon Prisma Client.

As a first step we want to look specifically into optimizing findOne queries by batching them.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:18 (11 by maintainers)

github_iconTop GitHub Comments

4reactions
pimeyscommented, Feb 20, 2020

Memo: unsurprisingly faster with optimization. batch Green: batching without optimization Purple: with optimization Y: response time in nanoseconds X: requests per second

Tested three queries in batches of 100, $user_id randomized from 1 to 3000. 3000 users, 30000 posts and 30000 comments.

query NoJoin {
  findOneUser(where: { id: $user_id }) {
    firstName
    lastName
  }
}
query OneJoin {
  findOneUser(where: { id: $user_id }) {
    firstName
    lastName
    posts {
      content
    }
  }
}
query TwoJoins {
  findOneUser(where: { id: $user_id }) {
    firstName
    lastName
    posts {
      content
      comments {
        content
      }
    }
  }
}
2reactions
maartenraescommented, Jan 30, 2020

@mavilein We didn’t really solve it, we just added timings just before and just after resolvers to see how fast queries were resolved.

  1. Doing all saved up queries in one go (what batching would look like) manually in Postgres takes about 12ms, with the exact where on the top level. So I don’t think that’s the issue.
  2. Activities doesn’t have that many entries, during our tests about 40. So even if Postgres would be scanning it shouldn’t take that long.
  3. We have put indexes on all foreign keys related to activities, also on all fields that are used in the where (deletedAt, departmentId, etch) both singular and combined indexes.

I do agree that it’s a hefty query. But production workloads are gonna go way beyond this I think 😄 The N+1 is definitely the big slowdown in this case, the worst thing about the N+1 is probably the overhead it takes for every query to resolve and go back and forth between Prisma-client and the database. (Which both run on localhost, so also shouldn’t be an issue, deployed on GCP both running in Cloud Run we have similar timings)

My example is also a findMany on activities, I don’t think a findOne would be as bad as a findMany. Think batching would be much more usefull in findMany than a findOne. But then again, the logic from findOne would probably be reuseable in de findMany

I don’t mind open ended comments, performance surely isn’t as straightforward as it seems!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Optimize Query Performance — MongoDB Manual
Optimize Query Performance · Create Indexes to Support Queries · Limit the Number of Query Results to Reduce Network Demand · Use Projections...
Read more >
How to Optimize MongoDB Queries Using find() & Indexes
Help optimize MongoDB queries with this advanced tutorial on how to use the find() method and indexes to spot slow queries.
Read more >
How to optimize MongoDB & Mongoose for Performance
So its usually optimal for GET endpoints and .find() operations that don't use .save() or virtuals. 2. Create custom Indexes for your queries....
Read more >
How to Find & Optimize Your MVPs (Most Valuable Pages)
Your most valuable pages can bring the greatest business impact. Find out what MVPs are and how to optimize them here.
Read more >
Optimization Problems - Calculus - YouTube
This calculus video explains how to solve optimization problems. It explains how to solve the fence along the river problem, ...
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