Several small ways to improve the efficiency of Django model
Source: Internet
Author: User
Django model efficiency is not very high, especially when doing a lot of database operations, if you only use Django to open the enterprise station or outsourcing projects, that can be small jump over, and you happen to be an efficiency freak or the efficiency of the program requirements are relatively high, then pay attention to the following several methods.
1. Count () Method:
We want to use the Count method to get the number of records that can be used in the following ways:
num = Info.objects.filter (' ... '). Count ()
Let's see how the Count method is written in the Django model module.
def count (self):
"""
Performs a SELECT COUNT () and returns the number of records as an
Integer.
If The QuerySet is already fully cached this simply returns the length
Of the cached results set to avoid multiple SELECT COUNT (*) calls.
"""
If Self._result_cache is not None and not self._iter:
Return Len (Self._result_cache)
Return Self.query.get_count (using=self.db)
From the above view, the Djang model count () to execute the SELECT COUNT () statement, in fact, is queried the next database, so if the record is more than the case,
The efficiency of querying a database is still relatively high.
For example, we can use the Len () method to find the length, and the iterations used are less effective.
info = Info.objects.filter (' ... ')
num = len (info)
2 Multi-use slices
For example, if we want to query the data, if you have a large amount of data, you do not limit the scope of the query, the cost of the system will be very large, such as you want to display pagination
News data, then you have to follow a page to show how much data, read how much data, rather than the first to read all the data, and then based on the criteria to display the data.
For example, if you want to display the first 10 news, follow the following method:
News = News.objects.all () [1:10]
Instead of:
News = News.objects.all ()
News = News[1:10]
Because
News = News.objects.all ()
News = News[1:10]
You read all the data in the database, so the efficiency is not very high.
The above points are a few ways to improve the efficiency of Django model, I hope you usually in the development of the Django project with the attention to some of the efficiency of things.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service