zealot: prevent N+1 queries

hey everyone!

i just released zealot, a library for detecting N+1 queries in your Django app.

when i worked on Rails projects, i’d always install bullet as one of the first things i did, and i’ve been wanting something similar in the Django world for a long time. there are a few packages on PyPI in this vein, but none of them did everything i wanted – for example, many of them wouldn’t detect N+1s caused by using .only() or .defer(), yet these can happen very easily!

i’ve been using zealot on our django monolith at work, and it’s already uncovered a lot of issues to fix. i hope that someone else will find it useful!

all feedback is very welcome :slight_smile:

I find this interesting, but as a faily noob django person I am thinking about if there is a way to log this to a model and then inspect it in the admin?

I havent made it to Sentry or testing yet. I know I should :blush:

there’s no way to do that with the library as it exists today, no – but if you set ZEALOT_RAISE = False in your settings, then it won’t raise any exceptions and it’ll log N+1s instead. so i’d suggest doing that and then searching your logs for “N+1 detected” to see what’s found!

1 Like

Thank you. I will try that. Maybe even post it back here.
Could I make it write a text file with the logs instrad?

yeah, that should be possible by configuring django’s logging! How to configure and use logging | Django documentation | Django

and i think you could use logger namespacing to only log zealot N+1 errors to a file.

i wouldn’t run zealot in production if your app is very performance-sensitive, though. i haven’t profiled it but it will add some minor overhead.

I tried it out. I worked just fine with:

if DEBUG:
    INSTALLED_APPS.append("zealot")
    MIDDLEWARE.append("zealot.middleware.zealot_middleware")
    ZEALOT_RAISE = False

I found a lot af N+1 which is good. Not being able to fix them right now and it being a small project is also fine. I will get back to fixing them. I might need a good guide like “fix N+1 like a pro” or something like that :grinning:

1 Like