Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging slow Django Admin views [closed]

Tags:

python

django

Some of my views in Django Admin take too long to load up. What is the best way to debug a Django Admin view to see what is chewing up cycles?

like image 679
Ernest Avatar asked Oct 27 '12 23:10

Ernest


3 Answers

I too have seen this with particular models in the Django Admin if their tables are very, very large (several million records will do the trick) and the table engine is MySQL's InnoDB. It took me a while to figure out workarounds to get my Django admin humming again for tables with 100M+ records. The root problem ended up being expensive COUNT(*) queries on page load and also poorly constructed searching queries whenever I used one of my search_fields from the ModelAdmin.

I documented all my solutions here, so that they might one day help a fellow Django-er in distress: http://craiglabenz.me/2013/06/12/how-i-made-django-admin-scale/

like image 80
Craig Labenz Avatar answered Nov 07 '22 17:11

Craig Labenz


As rantanplan commented, django debug toolbar is the easiest way to start profiling (shows all queries executed on page load, their EXPLAIN, their time taken to execute, etc). You could also have a look at a question regarding profiling a slow django installation here: How to profile django application with respect to execution time?

That question mentions the use of hotshot, which is also referenced on Django's Wiki under profiling django.

like image 21
Dan LaManna Avatar answered Nov 07 '22 17:11

Dan LaManna


If you use Django with MySQL then there is a bug in MySQL with INNER JOINs optimization. If you try to use foreign keys in Admin.list_display Django will generate query with ordering and INNER JOINs which are extremely slow in MySQL.

There are two solutions to that:

  1. Use django-mysql-fix backend: https://pypi.python.org/pypi/django-mysql-fix

  2. Override get_query_set in AdminChangeList - remove select_related and set prefetch_related fields - more details are in my other answer here: https://stackoverflow.com/a/23097385/1178806

like image 1
Vlad Frolov Avatar answered Nov 07 '22 17:11

Vlad Frolov