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?
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/
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.
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:
Use django-mysql-fix backend: https://pypi.python.org/pypi/django-mysql-fix
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With