A middlewere to help find n+1 queries in sqlalchemy.
Project description
FastAPI / SQLAlchemy N+1 query middleware
A FastAPI middleware to find N+1 queries in development. This middleware helps developers identify and fix N+1 query issues in their FastAPI applications. It provides logging information to help locate the query and the number of N+1 query occurrences.
This middleware should probably not be used in production, but it can be useful during development and testing.
Installation
Pip Install:
pip install fastapi-nplusone
UV install:
uv install fastapi-nplusone
Poetry install:
poetry add fastapi-nplusone
Usage
This middleware while suitable to run in production should probably be limited to development and tesring. The code snippet below shows how to add the middleware to your application.
from fastapi import FastAPI
from fastapi_nplusone import NPlusOneMiddleware
app = FastAPI()
if not app.debug:
app.add_middleware(NPlusOneMiddleware)
Example Output:
WARNING:fastapi_nplusone:Potential N+1 query detected on GET /api/changes: Query executed 3 times. Normalized query: SELECT change.id AS change_id
Tests
To run the tests:
uv run pytest ./tests
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_nplusone-0.1.0.tar.gz.
File metadata
- Download URL: fastapi_nplusone-0.1.0.tar.gz
- Upload date:
- Size: 3.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
084f901381e7f0b7505b90d7268fc5c49342f01d61168d1426e6d288de158ace
|
|
| MD5 |
a28d2f617cd48c18bfb764cb988fda02
|
|
| BLAKE2b-256 |
f74e78896562c08cf8c03dfd45e8de610cd87f6760b8350e451f8759f4b26357
|
File details
Details for the file fastapi_nplusone-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fastapi_nplusone-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
153b9723333f336aaaaeb29446e46bd50fb09f77d5b4b3ec15d1b4ae69fbce21
|
|
| MD5 |
e28a3b63f757358027e89bf489080cd4
|
|
| BLAKE2b-256 |
c3a1b77e615aad2d0663c86daacf37b6945409251ce2556ffe2cc989400d27b7
|