optimization.txt 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. ============================
  2. Database access optimization
  3. ============================
  4. Django's database layer provides various ways to help developers get the most
  5. out of their databases. This document gathers together links to the relevant
  6. documentation, and adds various tips, organized under an number of headings that
  7. outline the steps to take when attempting to optimize your database usage.
  8. Profile first
  9. =============
  10. As general programming practice, this goes without saying. Find out :ref:`what
  11. queries you are doing and what they are costing you
  12. <faq-see-raw-sql-queries>`. You may also want to use an external project like
  13. 'django-debug-toolbar', or a tool that monitors your database directly.
  14. Remember that you may be optimizing for speed or memory or both, depending on
  15. your requirements. Sometimes optimizing for one will be detrimental to the
  16. other, but sometimes they will help each other. Also, work that is done by the
  17. database process might not have the same cost (to you) as the same amount of
  18. work done in your Python process. It is up to you to decide what your
  19. priorities are, where the balance must lie, and profile all of these as required
  20. since this will depend on your application and server.
  21. With everything that follows, remember to profile after every change to ensure
  22. that the change is a benefit, and a big enough benefit given the decrease in
  23. readability of your code. **All** of the suggestions below come with the caveat
  24. that in your circumstances the general principle might not apply, or might even
  25. be reversed.
  26. Use standard DB optimization techniques
  27. =======================================
  28. ...including:
  29. * Indexes. This is a number one priority, *after* you have determined from
  30. profiling what indexes should be added. Use :attr:`django.db.models.Field.db_index` to add
  31. these from Django.
  32. * Appropriate use of field types.
  33. We will assume you have done the obvious things above. The rest of this document
  34. focuses on how to use Django in such a way that you are not doing unnecessary
  35. work. This document also does not address other optimization techniques that
  36. apply to all expensive operations, such as :doc:`general purpose caching
  37. </topics/cache>`.
  38. Understand QuerySets
  39. ====================
  40. Understanding :doc:`QuerySets </ref/models/querysets>` is vital to getting good
  41. performance with simple code. In particular:
  42. Understand QuerySet evaluation
  43. ------------------------------
  44. To avoid performance problems, it is important to understand:
  45. * that :ref:`QuerySets are lazy <querysets-are-lazy>`.
  46. * when :ref:`they are evaluated <when-querysets-are-evaluated>`.
  47. * how :ref:`the data is held in memory <caching-and-querysets>`.
  48. Understand cached attributes
  49. ----------------------------
  50. As well as caching of the whole ``QuerySet``, there is caching of the result of
  51. attributes on ORM objects. In general, attributes that are not callable will be
  52. cached. For example, assuming the :ref:`example Weblog models
  53. <queryset-model-example>`:
  54. >>> entry = Entry.objects.get(id=1)
  55. >>> entry.blog # Blog object is retrieved at this point
  56. >>> entry.blog # cached version, no DB access
  57. But in general, callable attributes cause DB lookups every time::
  58. >>> entry = Entry.objects.get(id=1)
  59. >>> entry.authors.all() # query performed
  60. >>> entry.authors.all() # query performed again
  61. Be careful when reading template code - the template system does not allow use
  62. of parentheses, but will call callables automatically, hiding the above
  63. distinction.
  64. Be careful with your own custom properties - it is up to you to implement
  65. caching.
  66. Use the ``with`` template tag
  67. -----------------------------
  68. To make use of the caching behaviour of ``QuerySet``, you may need to use the
  69. :ttag:`with` template tag.
  70. Use ``iterator()``
  71. ------------------
  72. When you have a lot of objects, the caching behaviour of the ``QuerySet`` can
  73. cause a large amount of memory to be used. In this case,
  74. :meth:`~django.db.models.QuerySet.iterator()` may help.
  75. Do database work in the database rather than in Python
  76. ======================================================
  77. For instance:
  78. * At the most basic level, use :ref:`filter and exclude <queryset-api>` to do
  79. filtering in the database.
  80. * Use :ref:`F() object query expressions <query-expressions>` to do filtering
  81. against other fields within the same model.
  82. * Use :doc:`annotate to do aggregation in the database </topics/db/aggregation>`.
  83. If these aren't enough to generate the SQL you need:
  84. Use ``QuerySet.extra()``
  85. ------------------------
  86. A less portable but more powerful method is
  87. :meth:`~django.db.models.QuerySet.extra()`, which allows some SQL to be
  88. explicitly added to the query. If that still isn't powerful enough:
  89. Use raw SQL
  90. -----------
  91. Write your own :doc:`custom SQL to retrieve data or populate models
  92. </topics/db/sql>`. Use ``django.db.connection.queries`` to find out what Django
  93. is writing for you and start from there.
  94. Retrieve everything at once if you know you will need it
  95. ========================================================
  96. Hitting the database multiple times for different parts of a single 'set' of
  97. data that you will need all parts of is, in general, less efficient than
  98. retrieving it all in one query. This is particularly important if you have a
  99. query that is executed in a loop, and could therefore end up doing many database
  100. queries, when only one was needed. So:
  101. Use ``QuerySet.select_related()``
  102. ---------------------------------
  103. Understand :ref:`QuerySet.select_related() <select-related>` thoroughly, and use it:
  104. * in view code,
  105. * and in :doc:`managers and default managers </topics/db/managers>` where
  106. appropriate. Be aware when your manager is and is not used; sometimes this is
  107. tricky so don't make assumptions.
  108. Don't retrieve things you don't need
  109. ====================================
  110. Use ``QuerySet.values()`` and ``values_list()``
  111. -----------------------------------------------
  112. When you just want a dict/list of values, and don't need ORM model objects, make
  113. appropriate usage of :meth:`~django.db.models.QuerySet.values()`.
  114. These can be useful for replacing model objects in template code - as long as
  115. the dicts you supply have the same attributes as those used in the template, you
  116. are fine.
  117. Use ``QuerySet.defer()`` and ``only()``
  118. ---------------------------------------
  119. Use :meth:`~django.db.models.QuerySet.defer()` and
  120. :meth:`~django.db.models.QuerySet.only()` if there are database columns you
  121. know that you won't need (or won't need in most cases) to avoid loading
  122. them. Note that if you *do* use them, the ORM will have to go and get them in a
  123. separate query, making this a pessimization if you use it inappropriately.
  124. Also, be aware that there is some (small extra) overhead incurred inside Django when constructing a model with deferred fields. Don't be too aggressive in deferring fields without profiling as the database has to read most of the non-text, non-VARCHAR data from the disk for a single row in the results, even if it ends up only using a few columns. The `defer()` and `only()` methods are most useful when you can avoid loading a lot of text data or for fields that might take a lot of processing to convert back to Python. As always, profile first, then optimize.
  125. Use QuerySet.count()
  126. --------------------
  127. ...if you only want the count, rather than doing ``len(queryset)``.
  128. Use QuerySet.exists()
  129. ---------------------
  130. ...if you only want to find out if at least one result exists, rather than ``if
  131. queryset``.
  132. But:
  133. Don't overuse ``count()`` and ``exists()``
  134. ------------------------------------------
  135. If you are going to need other data from the QuerySet, just evaluate it.
  136. For example, assuming an Email class that has a ``body`` attribute and a
  137. many-to-many relation to User, the following template code is optimal:
  138. .. code-block:: html+django
  139. {% if display_inbox %}
  140. {% with user.emails.all as emails %}
  141. {% if emails %}
  142. <p>You have {{ emails|length }} email(s)</p>
  143. {% for email in emails %}
  144. <p>{{ email.body }}</p>
  145. {% endfor %}
  146. {% else %}
  147. <p>No messages today.</p>
  148. {% endif %}
  149. {% endwith %}
  150. {% endif %}
  151. It is optimal because:
  152. 1. Since QuerySets are lazy, this does no database if 'display_inbox' is False.
  153. #. Use of ``with`` means that we store ``user.emails.all`` in a variable for
  154. later use, allowing its cache to be re-used.
  155. #. The line ``{% if emails %}`` causes ``QuerySet.__nonzero__()`` to be called,
  156. which causes the ``user.emails.all()`` query to be run on the database, and
  157. at the least the first line to be turned into an ORM object. If there aren't
  158. any results, it will return False, otherwise True.
  159. #. The use of ``{{ emails|length }}`` calls ``QuerySet.__len__()``, filling
  160. out the rest of the cache without doing another query.
  161. #. The ``for`` loop iterates over the already filled cache.
  162. In total, this code does either one or zero database queries. The only
  163. deliberate optimization performed is the use of the ``with`` tag. Using
  164. ``QuerySet.exists()`` or ``QuerySet.count()`` at any point would cause
  165. additional queries.
  166. Use ``QuerySet.update()`` and ``delete()``
  167. ------------------------------------------
  168. Rather than retrieve a load of objects, set some values, and save them
  169. individual, use a bulk SQL UPDATE statement, via :ref:`QuerySet.update()
  170. <topics-db-queries-update>`. Similarly, do :ref:`bulk deletes
  171. <topics-db-queries-delete>` where possible.
  172. Note, however, that these bulk update methods cannot call the ``save()`` or ``delete()``
  173. methods of individual instances, which means that any custom behaviour you have
  174. added for these methods will not be executed, including anything driven from the
  175. normal database object :doc:`signals </ref/signals>`.
  176. Use foreign key values directly
  177. -------------------------------
  178. If you only need a foreign key value, use the foreign key value that is already on
  179. the object you've got, rather than getting the whole related object and taking
  180. its primary key. i.e. do::
  181. entry.blog_id
  182. instead of::
  183. entry.blog.id