Package for on fly optimizing SQL queries generated by Django ORM. This package targets the N+1 problem so well known to Django.
Project description
Leo Optimizer
This Django plugin allows you to optimize SQL queries and solve N+1 bottleneck for queries resolved in GraphQL, as well as in admin pages.
Install
pip install leo-optimizer
Context
Let's say that the business context that you are modeling is around the concept of a city. You modeled your solution following way:
- City has one mayor
- City has many districts
- City belong to one state
- State is in the country
- Country is located in a continent
Outcome of these relations allows you to build a complex GraphQL query that might look like this.
query CityQuery {
allCities{
mayor {
city {
name
}
}
state {
country {
continent {
name
}
}
}
district {
city {
mayor {
city {
district {
name
}
}
}
}
}
}
}
Problem
The problem is that if you didn't use select_related and prefetch_related on the model that you are resolving then by default you will get N+1 problem.
Solution
You can fix this problem by importing gql_optimizer
function from leo_optimizer
package.
In your GraphQL resolver, wrap your model along with FiledNode that you would like to resolve against and return Django QuerySet. Your code should look like the code below.
from leo_optimizer import gql_optimizer
from app.models import City
def resolve_all_city(self, info):
queryset = gql_optimizer(City.objects.all(), info.field_nodes[0])
return queryset
Admin
Automatically generated django admin pages suffer from the same N+1 problem. To solve slow loading admin pages you can import QuickAdmin
class in your admin.py
file as shown on the example below.
from django.apps import apps
from django.contrib import admin
from leo_optimizer import QuickAdmin
for model in apps.get_app_config("name_of_your_application").get_models():
admin.site.register(model, QuickAdmin)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for leo_optimizer-0.0.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | beb111931752645a74193826eed28e00de56fce5400e8686fc0ee1049c69b58d |
|
MD5 | a8c21f3a496b200af4c7518eb3a366d1 |
|
BLAKE2b-256 | 2012094a0d62cb53226665874103d2bfe540269c33492a089b53d1f1b67468fb |