summaryrefslogtreecommitdiff
path: root/docs/topics/db/optimization.txt
diff options
context:
space:
mode:
Diffstat (limited to 'docs/topics/db/optimization.txt')
-rw-r--r--docs/topics/db/optimization.txt41
1 files changed, 18 insertions, 23 deletions
diff --git a/docs/topics/db/optimization.txt b/docs/topics/db/optimization.txt
index 6063bc6c2a..baf8cfa268 100644
--- a/docs/topics/db/optimization.txt
+++ b/docs/topics/db/optimization.txt
@@ -1,11 +1,9 @@
-.. _topics-db-optimization:
-
============================
Database access optimization
============================
Django's database layer provides various ways to help developers get the most
-out of their databases. This documents gathers together links to the relevant
+out of their databases. This document gathers together links to the relevant
documentation, and adds various tips, organized under an number of headings that
outline the steps to take when attempting to optimize your database usage.
@@ -45,13 +43,13 @@ Use standard DB optimization techniques
We will assume you have done the obvious things above. The rest of this document
focuses on how to use Django in such a way that you are not doing unnecessary
work. This document also does not address other optimization techniques that
-apply to all expensive operations, such as :ref:`general purpose caching
-<topics-cache>`.
+apply to all expensive operations, such as :doc:`general purpose caching
+</topics/cache>`.
Understand QuerySets
====================
-Understanding :ref:`QuerySets <ref-models-querysets>` is vital to getting good
+Understanding :doc:`QuerySets </ref/models/querysets>` is vital to getting good
performance with simple code. In particular:
Understand QuerySet evaluation
@@ -101,36 +99,35 @@ Use ``iterator()``
When you have a lot of objects, the caching behaviour of the ``QuerySet`` can
cause a large amount of memory to be used. In this case,
-:ref:`QuerySet.iterator() <queryset-iterator>` may help.
+:meth:`~django.db.models.QuerySet.iterator()` may help.
Do database work in the database rather than in Python
======================================================
For instance:
-* At the most basic level, use :ref:`filter and exclude <queryset-api>` to
- filtering in the database to avoid loading data into your Python process, only
- to throw much of it away.
+* At the most basic level, use :ref:`filter and exclude <queryset-api>` to do
+ filtering in the database.
* Use :ref:`F() object query expressions <query-expressions>` to do filtering
against other fields within the same model.
-* Use :ref:`annotate to do aggregation in the database <topics-db-aggregation>`.
+* Use :doc:`annotate to do aggregation in the database </topics/db/aggregation>`.
If these aren't enough to generate the SQL you need:
Use ``QuerySet.extra()``
------------------------
-A less portable but more powerful method is :ref:`QuerySet.extra()
-<queryset-extra>`, which allows some SQL to be explicitly added to the query.
-If that still isn't powerful enough:
+A less portable but more powerful method is
+:meth:`~django.db.models.QuerySet.extra()`, which allows some SQL to be
+explicitly added to the query. If that still isn't powerful enough:
Use raw SQL
-----------
-Write your own :ref:`custom SQL to retrieve data or populate models
-<topics-db-sql>`. Use ``django.db.connection.queries`` to find out what Django
+Write your own :doc:`custom SQL to retrieve data or populate models
+</topics/db/sql>`. Use ``django.db.connection.queries`` to find out what Django
is writing for you and start from there.
Retrieve everything at once if you know you will need it
@@ -149,7 +146,7 @@ Understand :ref:`QuerySet.select_related() <select-related>` thoroughly, and use
* in view code,
-* and in :ref:`managers and default managers <topics-db-managers>` where
+* and in :doc:`managers and default managers </topics/db/managers>` where
appropriate. Be aware when your manager is and is not used; sometimes this is
tricky so don't make assumptions.
@@ -160,7 +157,7 @@ Use ``QuerySet.values()`` and ``values_list()``
-----------------------------------------------
When you just want a dict/list of values, and don't need ORM model objects, make
-appropriate usage of :ref:`QuerySet.values() <queryset-values>`.
+appropriate usage of :meth:`~django.db.models.QuerySet.values()`.
These can be useful for replacing model objects in template code - as long as
the dicts you supply have the same attributes as those used in the template, you
are fine.
@@ -168,7 +165,8 @@ are fine.
Use ``QuerySet.defer()`` and ``only()``
---------------------------------------
-Use :ref:`defer() and only() <queryset-defer>` if there are database columns you
+Use :meth:`~django.db.models.QuerySet.defer()` and
+:meth:`~django.db.models.QuerySet.only()` if there are database columns you
know that you won't need (or won't need in most cases) to avoid loading
them. Note that if you *do* use them, the ORM will have to go and get them in a
separate query, making this a pessimization if you use it inappropriately.
@@ -243,10 +241,7 @@ individual, use a bulk SQL UPDATE statement, via :ref:`QuerySet.update()
Note, however, that these bulk update methods cannot call the ``save()`` or ``delete()``
methods of individual instances, which means that any custom behaviour you have
added for these methods will not be executed, including anything driven from the
-normal database object :ref:`signals <ref-signals>`.
-
-Don't retrieve things you already have
-======================================
+normal database object :doc:`signals </ref/signals>`.
Use foreign key values directly
-------------------------------