Commit ad636880 authored by Luke Plant's avatar Luke Plant
Browse files

[1.1.X] Created a 'DB optimization' topic, with cross-refs to relevant sections.

Also fixed #10291, which was related, and cleaned up some inconsistent doc labels.

Backport of r12229 from trunk


git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@12230 bcc190cf-cafb-0310-a4f2-bffc1f526a37
parent 9041b1ad
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -3,6 +3,8 @@
FAQ: Databases and models
=========================

.. _faq-see-raw-sql-queries:

How can I see the raw SQL queries Django is running?
----------------------------------------------------

+2 −1
Original line number Diff line number Diff line
@@ -70,7 +70,8 @@ The model layer
    * **Other:**
      :ref:`Supported databases <ref-databases>` |
      :ref:`Legacy databases <howto-legacy-databases>` |
      :ref:`Providing initial data <howto-initial-data>`
      :ref:`Providing initial data <howto-initial-data>` |
      :ref:`Optimize database access <topics-db-optimization>`

The template layer
==================
+24 −9
Original line number Diff line number Diff line
@@ -66,6 +66,18 @@ You can evaluate a ``QuerySet`` in the following ways:
      iterating over a ``QuerySet`` will take advantage of your database to
      load data and instantiate objects only as you need them.

    * **bool().** Testing a ``QuerySet`` in a boolean context, such as using
      ``bool()``, ``or``, ``and`` or an ``if`` statement, will cause the query
      to be executed. If there is at least one result, the ``QuerySet`` is
      ``True``, otherwise ``False``. For example::

          if Entry.objects.filter(headline="Test"):
             print "There is at least one Entry with the headline Test"

      Note: *Don't* use this if all you want to do is determine if at least one
      result exists, and don't need the actual objects. It's more efficient to
      use ``exists()`` (see below).

.. _pickling QuerySets:

Pickling QuerySets
@@ -302,7 +314,7 @@ a model which defines a default ordering, or when using
ordering was undefined prior to calling ``reverse()``, and will remain
undefined afterward).

.. _querysets-distinct:
.. _queryset-distinct:

``distinct()``
~~~~~~~~~~~~~~
@@ -336,6 +348,8 @@ query spans multiple tables, it's possible to get duplicate results when a
    ``values()`` call.


.. _queryset-values:

``values(*fields)``
~~~~~~~~~~~~~~~~~~~

@@ -616,7 +630,7 @@ call, since they are conflicting options.
Both the ``depth`` argument and the ability to specify field names in the call
to ``select_related()`` are new in Django version 1.0.

.. _extra:
.. _queryset-extra:

``extra(select=None, where=None, params=None, tables=None, order_by=None, select_params=None)``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -1043,17 +1057,18 @@ Example::

If you pass ``in_bulk()`` an empty list, you'll get an empty dictionary.

.. _queryset-iterator:

``iterator()``
~~~~~~~~~~~~~~

Evaluates the ``QuerySet`` (by performing the query) and returns an
`iterator`_ over the results. A ``QuerySet`` typically reads all of
its results and instantiates all of the corresponding objects the
first time you access it; ``iterator()`` will instead read results and
instantiate objects in discrete chunks, yielding them one at a
time. For a ``QuerySet`` which returns a large number of objects, this
often results in better performance and a significant reduction in
memory use.
`iterator`_ over the results. A ``QuerySet`` typically caches its
results internally so that repeated evaluations do not result in
additional queries; ``iterator()`` will instead read results directly,
without doing any caching at the ``QuerySet`` level. For a
``QuerySet`` which returns a large number of objects, this often
results in better performance and a significant reduction in memory

Note that using ``iterator()`` on a ``QuerySet`` which has already
been evaluated will force it to evaluate again, repeating the query.
+1 −1
Original line number Diff line number Diff line
@@ -353,7 +353,7 @@ without any harmful effects, since that is already playing a role in the
query.

This behavior is the same as that noted in the queryset documentation for
:ref:`distinct() <querysets-distinct>` and the general rule is the same:
:ref:`distinct() <queryset-distinct>` and the general rule is the same:
normally you won't want extra columns playing a part in the result, so clear
out the ordering, or at least make sure it's restricted only to those fields
you also select in a ``values()`` call.
+1 −0
Original line number Diff line number Diff line
@@ -16,3 +16,4 @@ model maps to a single database table.
   managers
   sql
   transactions
   optimization
Loading