Skip to main content

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)

Metadata

Release files for leo-optimizer 0.0.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for leo-optimizer 0.0.8
File Size Uploaded Provenance Actions
leo-optimizer-0.0.8.tar.gz 5.0 kB Without provenance Details for leo-optimizer-0.0.8.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for leo-optimizer 0.0.8
File Interpreter ABI Platform Actions
leo_optimizer-0.0.8-py3-none-any.whl Python 3 none any Details for leo_optimizer-0.0.8-py3-none-any.whl

Total release size: 10.3 kB

Release files / leo-optimizer-0.0.8.tar.gz

Download URL leo-optimizer-0.0.8.tar.gz
Size 5.0 kB
Tags Source
SHA-256 checksum
How to use checksums
3c498bb00ef5546af24602474938e00f1d2a3cb5bbdf5abb6f6b6485a67503fe
BLAKE2b-256 checksum
How to use checksums
7b067601a533290d6fa9a9b1d2a48d01dd02e41fad2c585b46ee33ca43f33846
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.12 CPython/3.7.11 Linux/5.11.0-43-generic

Release files / leo_optimizer-0.0.8-py3-none-any.whl

Download URL leo_optimizer-0.0.8-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5cdc77a0e393329b344d9d65f10552ddca92c817e96a186124b7196049d7c0ed
BLAKE2b-256 checksum
How to use checksums
c99dcf48c45e29d600e0d5fb1e8b1bfbe2f9aa58beb631ddf057c3226869aa2b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.12 CPython/3.7.11 Linux/5.11.0-43-generic

Security for leo-optimizer 0.0.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page