SQLAlchemy model N+1 debugger
Project description
QueryCounter
SQLAlchemy model N+1 debugger.
NOTE: This is a debugging tool that is not meant to be deployed in production.
About
This module will help identify N+1 DB calls made through SQLAlchemy. It takes advantage of the SQLAlchemy event listener for do_orm_execute
.
QueryCounter will provide insights into which model DB calls are made multiple times, with optional tracebacks and heuristics to determine where these calls originate.
By default, QueryCounter will log results with an optional config to raise an Exception.
Installation
pip install query-counter
Usage
Usage: Create QueryCounter with a SQLalchemy session, an optional config, and initialize
when you would like to start tracking requests:
from query_counter import QueryCounter
query_counter = QueryCounter(session=session)
query_counter.initialize()
Run analyze
to dig into queries that ran since initialization:
query_counter.analyze()
This also works as a context manager:
with QueryCounter(
session=session, config=QueryAnalysisConfig(alert_threshold=0)
) as counter:
session.query(User).first()
counter.analyze()
Setting a breakpoint in the analyze function will allow you to inspect all of the queries and their stack traces.
Configuration
QueryCounter
accepts an optional config
kwarg of type QueryAnalysisConfig
.
QueryAnalysisConfig
is a dataclass with the following specifications/defaults:
# QueryCounter analyze will not log or raise exceptions if the number
# of duplicated DB calls is less than the alert_threshold
alert_threshold: int = 10
# QueryCounter analyze will raise an exception if True
raise_if_exceeds: bool = False
# QueryCounter analyze will info log if no DB calls
# exceed the threshold
log_no_alert: bool = False
# QueryCounter will store the stacktrace relevant to the DB call
traceback_enabled: bool = False
# QueryInstance will inspect frames and filter the stack down
# to codepaths specified in heuristic_paths
heuristics_enabled: bool = False
# Requires heuristics_enabled=True - filters stack down to
# these codepaths
heuristic_paths: list = field(default_factory=list)
TODO
- Linting
- Tests
- Pipeline
License
QueryCounter is distributed under the MIT License.
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
Built Distribution
File details
Details for the file query-counter-0.3.1.tar.gz
.
File metadata
- Download URL: query-counter-0.3.1.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7f930485d6c3d7740e6364bbe5753e547f653e8cb8c88466f8ddc5f984844556 |
|
MD5 | bfbef85005522db7a3b6c27912d9acac |
|
BLAKE2b-256 | d687d0d390f09b7d1df3a3d7deba8a30d5fe137385d80ab7753c75f8b2bf7cba |
File details
Details for the file query_counter-0.3.1-py3-none-any.whl
.
File metadata
- Download URL: query_counter-0.3.1-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fa76a7ee32cf7ef212e4628c14344e4d1d13c572b1a23430b485e1e869a49e1f |
|
MD5 | 0d839eb95bd19d0503f6b976625e6c0f |
|
BLAKE2b-256 | 2e32b9dafc4594f4a3d6fc300532ef73383daf8108d9ea8010b081311248b563 |