4

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.

ashanbrown
  • 717
  • 8
  • 18
  • if you like an approach to automatically solve (vs identify) the n+1 queries issue in django have a look at this https://github.com/tolomea/django-auto-prefetch. It would be great to have this feature in Django by default. – Paolo Dec 14 '21 at 10:59

4 Answers4

6

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, ...

Carsten
  • 539
  • 4
  • 10
1

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.

François Constant
  • 5,531
  • 1
  • 33
  • 39
  • Thanks. The toolbar definitely helps but I'm really looking to find an automated way to identify these and ideally make it hard for developers to add new n+1 queries to my project. – ashanbrown Jan 12 '16 at 17:37
1

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.

0

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.