Detecting the n+1 queries problem in Python
Project description
nplusone is a library for detecting the n+1 queries problem in Python ORMs, including SQLAlchemy, Peewee, and the Django ORM.
The Problem
Many object-relational mapping (ORM) libraries default to lazy loading for relationships. This pattern can be efficient when related rows are rarely accessed, but quickly becomes inefficient as relationships are accessed more frequently. In these cases, loading related rows eagerly using a JOIN can be vastly more performant. Unfortunately, understanding when to use lazy versus eager loading can be challenging: you might not notice the problem until your app has slowed to a crawl.
nplusone is an ORM profiling tool to help diagnose and improve poor performance caused by inappropriate lazy loading. nplusone monitors applications using Django or SQLAlchemy and sends warnings when potentially expensive lazy loads are emitted. It can identify the offending relationship attribute and specific lines of code behind the problem, and recommend fixes for better performance.
Installation
pip install -U nplusone
nplusone supports Python >= 2.7 or >= 3.3.
Usage
Note: nplusone should only be used for development and should not be deployed to production environments.
Django
Add nplusone to INSTALLED_APPS
INSTALLED_APPS = ( ... 'nplusone.ext.django', )
Add NPlusOneMiddleware
MIDDLEWARE_CLASSES = ( ... 'nplusone.ext.django.NPlusOneMiddleware', )
Optionally configure logging settings
NPLUSONE_LOGGER = logging.getLogger('nplusone') NPLUSONE_LOG_LEVEL = logging.WARN
When your app loads data lazily, nplusone will emit a warning
Potential n+1 query detected on `<model>.<field>`
Consider using select_related or prefetch_related in this case.
Flask-SQLAlchemy
Wrap application with NPlusOne
from flask import Flask from nplusone.ext.flask_sqlalchemy import NPlusOne app = Flask(__name__) NPlusOne(app)
Optionally configure logging settings
app = Flask(__name__) app.config['NPLUSONE_LOGGER'] = logging.getLogger('app.nplusone') app.config['NPLUSONE_LOG_LEVEL'] = logging.ERROR NPlusOne(app)
When your app loads data lazily, nplusone will emit a warning
Potential n+1 query detected on `<model>.<field>`
Consider using subqueryload or joinedload in this case; see SQLAlchemy’s guide to relationship loading for complete documentation.
Ignoring Warnings
To suppress warnings thrown by intentional lazy loading, use the ignore context manager
from nplusone.core import signals with signals.ignore(): # lazy-load rows # ...
License
MIT licensed. See the bundled LICENSE file for more details.
Project details
Release history Release notifications | RSS feed
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
File details
Details for the file nplusone-0.1.0.tar.gz
.
File metadata
- Download URL: nplusone-0.1.0.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 95db4e5825f2707f1c4f82287a51191c99bfa544ac1f0811da6c75e111b5555d |
|
MD5 | a10fed1fab909b227c1037cf8f6ac4ad |
|
BLAKE2b-256 | c6789becd4f0aec9cdff11ba85a3406a4f5549ce68385fddd7b75f3b2908cc74 |
File details
Details for the file nplusone-0.1.0-py2.py3-none-any.whl
.
File metadata
- Download URL: nplusone-0.1.0-py2.py3-none-any.whl
- Upload date:
- Size: 10.6 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 98fb345f0ffcb1323c21562af6776098f97598e1b6b2ac9642b5714fe23215cc |
|
MD5 | ce3b1310ac244f08244a3c9d5155effb |
|
BLAKE2b-256 | 1b44d5c26a0a0166120b347728c08dbf944b6cf747bb738ee13804d3edb08a44 |