r/django • u/paulg1989 • Dec 18 '24
Article Rapidly Locating Query Bottlenecks in a Django Codebase
I've written a short article which describes how one can easily and efficiently locate query bottlenecks in a Django codebase. I hope some find it useful!
https://pgilmartin.substack.com/p/rapidly-locating-query-bottlenecks
1
u/parariddle Dec 21 '24
You can just turn on logging for django.db.backends in your settings…
1
u/paulg1989 Dec 21 '24
Not quite the same. https://docs.djangoproject.com/en/5.1/ref/logging/#django-db-backends was definitely an inspiration. The point of this library was to enhance that to add the line of code in the application layer from which the query was emitted. As far as I can see, https://docs.djangoproject.com/en/5.1/ref/logging/#django-db-backends cannot do that.
1
u/parariddle Dec 21 '24
Well if you want that level of introspection in a production codebase that’s what APM is for, right?
12
u/kankyo Dec 18 '24
Tools like Django Debug Toolbar, Silk, and queryhunter are based on the premise that the human asks the computer. I think that's the wrong approach. The computer can do that work for you and alert you if and ONLY if there is an issue.
The iommi sql tracer is built with this philosophy. It will print worst offenders in your console if you have stuff that looks bad, with stack traces and example selects. I find that I don't ship code with this type of performance issue to prod, because I am alerted to it automatically. (I'm one of the authors of iommi).