Django Performance Improvements

Part 1: Database Optimizations

Photo by Ivan Diaz on Unsplash

The main goal of optimization in Django projects is to make it fast to perform database queries by ensuring that your projects run by making the best use of system resources. A properly optimized database will reduce the response time hence a better user experience.

In this 4 part series, you will learn how to optimize the different areas of your Django application.

This part will focus on optimizing the database for speed in Django applications.

Understanding queries

Understanding how querysets work will lead to better code. Before any optimization, start by understanding these concepts:

  1. Querysets are lazy: you can write as many queries as you want, but Django will only query from the database when the query is evaluated.
  2. Always limit the results you obtain from the database by specifying how many values should be returned.
  3. In Django, you can evaluate your queryset by iteration, slicing, caching, and python methods such as len(), count(), e.t.c, so ensure that you make the best use of them.
  4. Django performs caching in every queryset to minimize database access. Understanding how caching works will allow you to write better and more efficient code.
  5. Only retrieve what you need
  6. Retrieve everything at once if you think you will use it later
  7. Always perform database work in the database rather than in Python

Query optimization

The database is the core of any application. Contrary to the belief, complexity doesn’t always guarantee efficiency. Postgresql is the preferred database for Django applications due to its open-source nature, and it’s also ideal for complex queries.

To optimize queries in any Django application, we will cover the following areas to perform database optimization, namely:

  1. database indexing
  2. Caching
  3. select related vs. fetch related
  4. bulk method
  5. RawSql
  6. Foreign keys

1. Database indexing

Database indexing is a technique used to speed up querying when retrieving records from a database. When working with large databases that churn lots of data, indexing is a non-negotiable practice to make your app faster.

As the application becomes significant, it might slow down, and users will notice since it will take considerably longer to obtain the requested data. To illustrate this, we will use an example of an ecommerce store that has a table product with the models shown below.

As the database grows, you might notice it takes a significant time to retrieve data. For example, assume you want to apply an index to the price column to speed up searches on the column.

price = models.DecimalField(decimal_places= 2,max_digits=8,db_index=True)

After you apply the index, you will need to run migrations so that the index is created.

It’s also important to note that if the table has a lot of rows, it will take longer to create the index. You can also create a single index for two fields:

2. Caching

Caching in databases is the best approach to fast responses. It ensures that few calls are made to the database, preventing database overload. A standard cache structure will look like this:


Django Performance Improvements was originally published in Better Programming on Medium, where people are continuing the conversation by highlighting and responding to this story.

0
(Visited 1 times, 1 visits today)