Query Debugging Utilities
Capturing queries around a function call
from gyrinx.query import capture_queries, log_query_info
from gyrinx.core.models import MyModel
# Capture queries executed inside the lambda
result, info = capture_queries(lambda: MyModel.objects.filter(foo="bar").count())
print(info.count, "queries in", info.total_time, "seconds")
for q in info.queries:
print(q["time"], q["sql"])
# Or log them nicely
log_query_info(info)DEBUG:gyrinx.db:Captured 3 queries in 0.012 seconds
DEBUG:gyrinx.db: 1. (0.003s) SELECT ...
DEBUG:gyrinx.db: 2. (0.004s) SELECT ...
DEBUG:gyrinx.db: 3. (0.005s) SELECT ...Using as a decorator
Options
When to use
Using in pytest tests
Last updated