ashanbrown
ashanbrown

Reputation: 747

how to automatically identify n+1 queries in a django app?

Is there any tool, plugin or technique that I can use to help identify n+1 queries on a Django application? There is a gem called bullet for Rails that identifies n+1 queries and logs or pops up warnings in a number of ways but I haven't been able to find anything similar for Django. I'd be open to guidance on how to write my own plugin if no one knows of an existing solution.

Upvotes: 4

Views: 3824

Answers (4)

Tidiane Dia
Tidiane Dia

Reputation: 11

dj-tracker does exactly this. It keeps track of all the queries your application makes and you can see/filter in the dashboard all the requests where an N+1 situation was detected.

Upvotes: 0

Carsten
Carsten

Reputation: 549

nplusone does this

Auto-detecting the n+1 queries problem in Python

https://github.com/jmcarp/nplusone

comes with proper django support! also integrates with flask, vanilla wsgi, ...

Upvotes: 6

Derek Haynes
Derek Haynes

Reputation: 33

Scout, an APM product that supports Django apps, identifies expensive N+1 queries in production.

Here's how to use it:

  1. Install the scout-apm Python package (MIT license) and provide your Scout API key. The API key is found in the Scout web UI.

  2. Deploy your app, confirm Scout is receiving data, then check back in an hour or so. Scout analyzes every web request, checking for N+1s, and then displays the worst performers on a dashboard (screenshot).

  3. Select an N+1 you're interested in to reveal a transaction of the request that triggered the N+1. This includes the raw SQL of the query and a backtrace to the LOC that is triggering the query (screenshot).

An advantage to Scout over a development tool like Bullet: most development databases have a small amount of data, so the true impact of an N+1 is frequently unknown. Scout identifies just those N+1s that are consuming significant time, which can help focus your efforts.

Upvotes: 1

François Constant
François Constant

Reputation: 5496

I don't know any plugin that would find them automatically and warn you.

I personally use the Django Debug Toolbar: https://github.com/django-debug-toolbar/django-debug-toolbar

It shows the number of queries ran on a page and you can view them.

Upvotes: 1

Related Questions