Proven Techniques for Django Query Optimization and Better Performance

Posted By : Mohd Sakib | 18-Mar-2024

Django

Loading...

1: Use Select Related and Prefetch Related:

Django provides two handy methods for optimising queries involving ForeignKey and OneToOneField relationships: select_related and prefetch_related. These methods enable you to get related objects more efficiently, lowering the number of database searches.

# Example using select_related

queryset = MyModel.objects.select_related('related_model')

# Example using prefetch_related

queryset = MyModel.objects.prefetch_related('related_set')

2: Indexing:

Database indexing is crucial for fast query performance. Ensure that your database tables have appropriate indexes, especially on fields frequently used in filtering, ordering, or joining. Django provides the db_index option in model fields to create indexes.

class MyModel(models.Model):

name=models.CharField(max_length=100,db_index=True)

Also, Read Django REST vs FastAPI

3 Limit Querysets:

Avoid retrieving unnecessary data by using values() or only() or defer() to limit the fields fetched. Additionally, use iterator() for large querysets to fetch records one at a time and reduce memory consumption.

# Limit fields using values()
queryset = MyModel.objects.values('field1', 'field2')

# Use iterator for large querysets
queryset = MyModel.objects.all().iterator()


# using The only() method is used to select only the specified fields from the database.
queryset = MyModel.objects.only('field1', 'field2')


# using The defer() method is used to defer the loading of certain fields until they are actually accessed
queryset = MyModel.objects.defer('field5')


# we combine both in a one query set

queryset = MyModel.objects.only('field1', 'field2').defer('field5')


4 Caching:

Implement caching for frequently accessed or expensive queries. Django provides a caching framework that can be easily integrated to store and retrieve query results efficiently.

from django.core.cache import cache

key = 'my_model_cache_key'
queryset = cache.get(key)

if queryset is None:
queryset = MyModel.objects.all()
cache.set(key, queryset, timeout=3600) # Cache for one hour

Also, Read Django vs Flask

5. Monitor your performances

As we repeated throughout this article, optimization is a long game. This is not the first thing you should think about when coding a new feature —there is even the well-known mantra: avoid premature optimization—but rather a thing you should prepare. Observability is definitely a must-have to detect bottlenecks, timeouts, locks, and all those nasty problems that are inseparable from the success of your great product.

We have seen how to optimize at the query level, but you first need to identify queries you should work on.