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 details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

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

Uploaded Python 3

File details

Details for the file leo-optimizer-0.0.8.tar.gz.

File metadata

  • Download URL: leo-optimizer-0.0.8.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.7.11 Linux/5.11.0-43-generic

File hashes

Hashes for leo-optimizer-0.0.8.tar.gz
Algorithm Hash digest
SHA256 3c498bb00ef5546af24602474938e00f1d2a3cb5bbdf5abb6f6b6485a67503fe
MD5 0623bc4cd9c40656a601959715ce791d
BLAKE2b-256 7b067601a533290d6fa9a9b1d2a48d01dd02e41fad2c585b46ee33ca43f33846

See more details on using hashes here.

File details

Details for the file leo_optimizer-0.0.8-py3-none-any.whl.

File metadata

  • Download URL: leo_optimizer-0.0.8-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.7.11 Linux/5.11.0-43-generic

File hashes

Hashes for leo_optimizer-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 5cdc77a0e393329b344d9d65f10552ddca92c817e96a186124b7196049d7c0ed
MD5 c42e0852cfe4b0e870991abeac0a704c
BLAKE2b-256 c99dcf48c45e29d600e0d5fb1e8b1bfbe2f9aa58beb631ddf057c3226869aa2b

See more details on using hashes here.

Supported by

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