Skip to main content

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

Metadata

Release files for fastapi-nplusone 0.1.0

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

Source distribution (sdist)

Source distribution for fastapi-nplusone 0.1.0
File Size Uploaded Provenance Actions
fastapi_nplusone-0.1.0.tar.gz 3.7 kB Without provenance Details for fastapi_nplusone-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-nplusone 0.1.0
File Interpreter ABI Platform Actions
fastapi_nplusone-0.1.0-py3-none-any.whl Python 3 none any Details for fastapi_nplusone-0.1.0-py3-none-any.whl

Total release size: 8.5 kB

Release files / fastapi_nplusone-0.1.0.tar.gz

Download URL fastapi_nplusone-0.1.0.tar.gz
Size 3.7 kB
Tags Source
SHA-256 checksum
How to use checksums
084f901381e7f0b7505b90d7268fc5c49342f01d61168d1426e6d288de158ace
BLAKE2b-256 checksum
How to use checksums
f74e78896562c08cf8c03dfd45e8de610cd87f6760b8350e451f8759f4b26357
Upload date
Uploaded using Trusted Publishing?
What is 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}

Release files / fastapi_nplusone-0.1.0-py3-none-any.whl

Download URL fastapi_nplusone-0.1.0-py3-none-any.whl
Size 4.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
153b9723333f336aaaaeb29446e46bd50fb09f77d5b4b3ec15d1b4ae69fbce21
BLAKE2b-256 checksum
How to use checksums
c3a1b77e615aad2d0663c86daacf37b6945409251ce2556ffe2cc989400d27b7
Upload date
Uploaded using Trusted Publishing?
What is 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}

Security for fastapi-nplusone 0.1.0

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