Optimizing slow Django REST Framework performance
At it’s root, the problem is called the “N+1 selects problem”; the database is queried once for data in a table (say,
Customers
), and then, one or more times per customer inside a loop to get, say,customer.country.Name
. Using the Django ORM, this mistake is easy to make. Using DRF, it is hard not to make... Eager loading is a common performance optimization that has application well beyond the Django REST Framework.
Any time you are querying nested relationships via an ORM, you should think about setting up the proper eager loading. In my experience, it is the most commonplace performance-related problem in modern small- and midsize web development.