As a Django app goes Bigger, how to optimize SQL queries

(Allan Stockman Rugano ) #1

I’ve legacy code of a Django apps that I need to optimize for better viewing. I have moved some of the heavy loading of data in templates by using Javascript to query Django Rest Framework endpoints that provide Json.
But nevertheless, the applications kept feeling slow. So I started the optimization route. I firstly implemented caching, but lately I discovered how to optimize SQL queries (especially the extra duty tasks that might be done on DRF SerializerMethodField :face_with_raised_eyebrow: !) using prefetch_related, and select_related.

It’s been a huge improvement of my queries where I’ve seen for example, specs going from 110 Queries in 57.34Ms to 5 Queries in 10MS. I’ve used this tutorial.

But Django Debug Toolbar still shows me that there are some queries that are still duplicates/redundant. So a proper blog/tutorial on optimizing queries may be useful.