Skip to main content

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

leo-optimizer-0.0.8.tar.gz (5.0 kB view hashes)

Uploaded Source

Built Distribution

leo_optimizer-0.0.8-py3-none-any.whl (5.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page