Optimize complex GraphQL queries
See original GitHub issueEnvironment
- Python version: 3.7
- Nautobot version: 1.0.0b2
Proposed Functionality
Leverage the ORM capabilities to query multiple results at once with prefetch_related
and select_related
to optimize some complex GraphQL queries and improve their response time
Use Case
A query like this one currently takes 1.87 sec (with caching) to return in the sandbox environment because we are querying the site and the region for each device.
query {
devices {
name
site {
name
region {
name
}
}
}
}
With some optimization we should be able to return this query much faster
Database Changes
No
External Dependencies
Not sure yet but we should investigate some existing packages like graphene-django-optimizer that are trying to solve the same problem.
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (9 by maintainers)
Top Results From Across the Web
GraphQL Optimization: It's More than N+1 - The New Stack
The obvious optimization opportunity for a GraphQL operation is to minimize the trips to the backend data sources, whether they are databases,Β ...
Read more >7 Optimizing Data Fetching - GraphQL in Action
Caching and batching data-fetch operations; Using the DataLoader library with primary keys and custom IDs; Using GraphQL's union type and field arguments;Β ...
Read more >Optimizing Your GraphQL Request Waterfalls
To clarify what we're aiming for, an optimal GraphQL server is one that returns the data for any arbitrary query as efficiently as...
Read more >Optimize GraphQL Server with Lookaheads
GraphQL offers a way to optimize the data between a client and a server. We can use the declarative nature of a GraphQL...
Read more >Designing a GraphQL server for optimal performance
js. Its strategy is very simple: through promises, it defers resolving segments of the query until a later stage, wherein all objects of...
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 FreeTop 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
Top GitHub Comments
@carbonarok after our last discussion I was under the impression that your branch should work so I did some testing on it and as far as I can tell itβs working pretty well π
I wrote a management command to do some testing and I found some interesting results.
For my testing I used a dataset similar to the sandbox and I created 4 GraphQL queries that would present different use cases. You can see the 4 queries that I used here, I named them to simplify the reporting.
For each query, I measured how many SQL requests were generated, the response time with cache and without cache.
Number of SQL queries generated per GraphQL query
Just by looking at the number of SQL queries generated per GraphQL query, we can see a huge optimization, in some cases a reduction of the number of queries by a factor of 100x or more
next
carbonarok:kc_graphene_optimizer
Looking at the response time, we can also see a significant improvement with
carbonarok:kc_graphene_optimizer
branchResponse time per GraphQL query with and without cache, without optimization
branch
next
Response time per GraphQL query with and without cache, WITH optimization
branch
carbonarok:kc_graphene_optimizer
I think this is pretty good, Great work @carbonarok π We might not be able to optimize all queries yet but that would be great to get these optimizations
Not mandatory for 1.0.0 but would be good to have it for 1.1.0 if possible Iβll be happy to help with this one as time permit