Django Performance Optimization

Source: Internet
Author: User

2. Use a separate static file server

3. Disable KeepAlive (if the server does not provide static file services, such as large file downloads)

4. Use

5. Use to load associated table data

6. Filter unnecessary fields for query

7. Use

8. Load the compiled Template

 

 django.template  django.http myview_template = loader.get_template(     HttpResponse(myview_template.render( context ))

 

Where programmers like to go: http://www.lailu8.com

The Django data layer provides various ways to optimize data access. A large amount of optimization work for a project is generally done later. Early optimization is the "source of all evil", which was summarized by our predecessors, not without reason. If you understand Django's optimization skills in advance and pay a little attention during the development process, you will save a lot of work in the future.

I. Using standard database optimization technology:

Traditional database optimization technologies are profound and profound. Different databases have different optimization techniques, but the focus is still on rules. Here is a digress. Let's talk about two common points:

Index: adds indexes to key fields to improve the performance, such as adding indexes to fields associated with tables and fields with high search frequencies. Django supports adding indexes to fields when creating objects. For more information, see Django. db. models. Field. db_index. According to experience, Django should think about the structure of the table before establishing the object, and try to think about the extensibility of the subsequent table as much as possible to avoid the structure of the subsequent table being completely invisible.

When appropriate field types are used, do not set the text type for the fields originally configured in varchar. It is not important to make small details. As soon as the data volume goes up, hundreds of millions of data will be stored, small fields may be a big problem.


2. Understand QuerySets of Django:

Understanding the QuerySets object of Django plays a vital role in optimizing simple programs. QuerySets are cached. Once retrieved, it will stay in the memory for a period of time and try to reuse it as much as possible. A simple example:

Learn about cache attributes:
>>> Entry = Entry. objects. get (id = 1)
>>> Entry. blog # The first time the blog entity is retrieved, it is used to access the database
>>> Entry. blog # reuse for the second time. It is the entity in the cache and no longer accesses the database.


But the following examples are different,
>>> Entry = Entry. objects. get (id = 1)

>>> Entry. authors. all () # The first all function queries the database.

>>> Entry. authors. all () # The second all function queries the database.

All and count exists call functions (which need to connect to the database for processing results). Note that the code in the template does not allow parentheses in the template. However, if such calls are used, if you connect to the database, do not connect to the database to process the results with cached data. Note that if you call a function for a custom object attribute, you must add a cache policy.

Use the with tag of the template:
The variable used multiple times in the template should use the with label to regard it as the cache behavior of the variable.

Iterator () using QuerySets ():
Generally, QuerySets call iterator before caching. When a large number of object lists are obtained and used only once, the caching behavior consumes valuable memory. At this time, iterator () can help you, iterator () only calling iterator saves the cache step and significantly reduces memory usage. For more information, see related documents.


The work of the three databases is handed over to the database itself for computation. Do not use Python for processing:

1. Use filter and exclude to filter unwanted records. These two are the most common statements, and they are equivalent to the SQL where statement.

2. Use the F () expression in the same object to filter other fields.

3. Use annotate to aggregate databases.

Do not use the python language to filter and filter the above types of data. For the same results, python is highly complicated to process and inefficient, wasting memory.

Use QuerySet. extra ():
Although extra has poor scalability, It is very powerful. If you need to add additional attributes to an object, it is also a good way to implement it through extra.

Use native SQL statements:
If you find that Django's ORM cannot meet your needs, and extra does not help, use native SQL statements and Djangoango. db. connection. queries to implement what you need.


4. If necessary, extract the data you need at one time:

When a single action (such as the same page) needs to connect to the database multiple times, it is best to retrieve all the required data at a time to reduce the number of database connections. QuerySet. select_related () and prefetch_related () are recommended for this requirement ().

On the contrary, do not extract what you don't need. In the templates template, only a few fields of the object rather than all are needed. In this case, QuerySet. values () and values_list () are useful to you. They only take the fields you need and return the dict dictionary and list type items. They can be used in templates, this reduces memory consumption and improves performance.

Same as QuerySet. defer () and only () are also very helpful for improving performance. A single entity may have many fields, some of which contain a lot of metadata, such as the blog body, many characters. When Django acquires an object (some python type conversion is performed during entity extraction), we can delay processing of a large number of metadata fields and only process the required key fields, querySet. defer () comes in handy. In a function, you can pass in the fields that require delayed processing, while only () and defer () are opposite functions.

Use QuerySet. count () instead of len (queryset). Although the results of the two processes are the same, the former performs a lot better. Similarly, when a record exists, QuerySet. exists () is much better than if queryset.

Of course, if the same result already exists in the cache, do not abuse the count (), exists (), all () function.


5. Reduce the number of database connections:

When QuerySet. update () and delete () are used, these two functions can batch process multiple records and use them as appropriate to get twice the result with half the effort. If possible, separate data entries to update and delete.

When obtaining a foreign key for a one-time Association record, you can directly retrieve the attribute of the Association table instead of the Association attribute, for example:

Entry. blog. id

Better

Entry. blog_id


Good at batch insert records, such:
Entry. objects. bulk_create ([
Entry (headline = "Python 3.0 Released "),
Entry (headline = "Python 3.1 Planned ")
])
Better
Entry. objects. create (headline = "Python 3.0 Released ")
Entry. objects. create (headline = "Python 3.1 Planned ")
The former connects to the database only once, while the latter connects twice.

Pay attention to similar actions, such as the many-to-many relationship,
My_band.members.add (me, my_friend)
Better
My_band.members.add (me)
My_band.members.add (my_friend)

Why, let's take a look at it from the perspective of batch processing.

 

0. Use the relative path in the configuration

For some reason, projects may often be migrated back and forth. If this possibility is not planned in advance, this is definitely a tricky problem. Rob Hudson has an excellent technique to ensure that your Django project can be easily migrated back and forth during deployment. Just write a few lines of code in your configuration file (settings. py.

123456 import osBASE_DIR = os.path.dirname(os.path.abspath(__file__)) TEMPLATE_DIRS = (    BASE_DIR + '/templates',)

 

1. Use the {% url %} tag

Try to replace the hard-coded href with the backward compatible {% url %} tag, which is the same as the url with an absolute path (of course it is best not to do this. Those links will not be affected when your Django project is migrated. For example, we have a view. the about function points to the about page R' ^ about/$ 'to {% url views. about as about_url %} and use the variable {about_url} to replace the absolute URL address.) Although it is not the most advanced technique, it is indeed worth applying to Django projects.

Photo by Cloudzilla.

 

2. Try to apply Django admin to the PHP project.

One of Django's greatest features is that it has become a user verification system for Django's core functions. It is easy to install and mainly used for user authentication and other necessary configurations. This cool user system is even recommended to be applied to your PHP project, here is a good solution for Jeff Croft about why Django can be used as a system management module in any application language.

 

3. Use an Independent Media Server

It is not a big problem to place static files on the same server as the Django project in the development environment, but it should not be used in the production environment. Why? Efficiency issues. Jacbian.org provides a reasonable explanation. Processing static files through an independent server can effectively improve the performance. If you do not want to buy a server, Amazon S3 is cheaper.

 

4. Use the Debugger Toolbar

Debugging tools are indispensable for any language. They can speed up development and point out potential defects. Rob Hudson developed a django debugging tool that is very useful to developers.

 

5. Use Django unit test

Use unit tests to ensure that your code changes are the same as expected without disrupting any old code for backward compatibility. A powerful feature of Django is that it can easily write unit tests. Django can also directly use python text testing and unit testing. The Django document provides a detailed tutorial and sample code about how to perform unit tests to make the code run correctly and remove annoying bugs.

 

6. Use a speed query card

There are two pages of High-Speed scanning cards. In the Django document, you may need to find things for half a day at a glance here. It contains the following topics:

Template:

Template tags and options

Template filter and optional

Quick query of date formatting syntax

Model:

Fields and options

Options for common domains

Metadata Option

Model Management Option

Form:

Domain and optional

Common domain options

Standard Error Message key value

 

7. Use Django-chunks

Apart from using Django's Rich Text Editor to create blocks, Django-chunks is also used in templates, which is an essential tool for reusing code blocks.

 

8. Use Memcache

If performance has become a tricky issue in your Django project, you will need to use some caching policies. However, Django provides many choices for caching. At present, the best is undoubtedly Memcache. It is very simple to install memcache with Django. If you use the cmemcache module. After the module is installed, you only need to modify one configuration line, and your Django page becomes brisk.

 

9. Django is not as exciting as action

After reading this article, if you still do not fully understand the power of Django, the reason for using Django in your next project is: it can save time for different software designs. Jeff Croft explains why using Django to create a project is more efficient than you have designed. Django allows you to expand your Web site without worrying about the design, code, and database compatibility. It will work very well.

 

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.