Control Django DB Performance using django-test-query-counter
A library is introduced in this post to help maintain the database performance in Django Apps through testing.. It works by measuring queries run during unit tests, and also by offering by a command that compares test query counts from two different tests runs and provides detailed query information such as stacktraces. It aims to detect possible Database performance issues as soon as possible.
When a problem arises that slows down your application, it’s not uncommon to relegate performance and deal with the problem. The same code can produce twice as many queries as before. Some common reasons behind that are:
- New requirements which create the need to retrieve information about related records
- Usage of different instances of QuerySet objects for the same query.
- Lack of select_related, prefetch_related, or subqueries in the original queryset.
- Lack of caching capabilities like
@cached_property, and Django cache framework.