Part 1: Database Optimizations
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 how querysets work will lead to better code. Before any optimization, start by understanding these concepts:
- 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.
- Always limit the results you obtain from the database by specifying how many values should be returned.
- 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.
- Django performs caching in every queryset to minimize database access. Understanding how caching works will allow you to write better and more efficient code.
- Only retrieve what you need
- Retrieve everything at once if you think you will use it later
- Always perform database work in the database rather than in Python
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:
- database indexing
- select related vs. fetch related
- bulk method
- 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:
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: