  
  #. Since QuerySets are lazy, this does no database queries if 'display_inbox'
     is False.
  
  #. Use of :ttag:`with` means that we store ``user.emails.all`` in a variable
     for later use, allowing its cache to be re-used.
  
  #. The line ``{% if emails %}`` causes ``QuerySet.__bool__()`` to be called,
     which causes the ``user.emails.all()`` query to be run on the database, and
     at the least the first line to be turned into an ORM object. If there aren't
     any results, it will return False, otherwise True.
 
</s> remove #. The line ``{% if emails %}`` causes ``QuerySet.__bool__()`` to be called,
   which causes the ``user.emails.all()`` query to be run on the database, and
   at the least the first line to be turned into an ORM object. If there aren't
   any results, it will return False, otherwise True.
 </s> add #. The line ``if emails`` causes ``QuerySet.__bool__()`` to be called, which
   causes the ``user.emails.all()`` query to be run on the database. If there
   aren't any results, it will return ``False``, otherwise ``True``. </s> remove #. Since QuerySets are lazy, this does no database queries if 'display_inbox'
   is False.
 </s> add #. Since QuerySets are lazy, this does no database queries if
   ``display_emails`` is ``False``. </s> remove #. The use of ``{{ emails|length }}`` calls ``QuerySet.__len__()``, filling
   out the rest of the cache without doing another query.
 </s> add #. The use of ``len(emails)`` calls ``QuerySet.__len__()``, reusing the result
   cache. </s> remove For example, assuming an Email model that has a ``body`` attribute and a
many-to-many relation to User, the following template code is optimal:

.. code-block:: html+django

   {% if display_inbox %}
     {% with emails=user.emails.all %}
       {% if emails %}
         <p>You have {{ emails|length }} email(s)</p>
         {% for email in emails %}
           <p>{{ email.body }}</p>
         {% endfor %}
       {% else %}
         <p>No messages today.</p>
       {% endif %}
     {% endwith %}
   {% endif %}

 </s> add For example, assuming an Email model that has a ``subject`` attribute and a
many-to-many relation to User, the following code is optimal::

    if display_emails:
        emails = user.emails.all()
        if emails:
            print('You have', len(emails), 'emails:')
            for email in emails:
                print(email.subject)
        else:
            print('You do not have any emails.') </s> remove deliberate optimization performed is the use of the :ttag:`with` tag. Using
``QuerySet.exists()`` or ``QuerySet.count()`` at any point would cause
additional queries.
 </s> add deliberate optimization performed is using the ``emails`` variable. Using
``QuerySet.exists()`` for the ``if`` or ``QuerySet.count()`` for the count
would each cause additional queries.
#. Storing ``user.emails.all()`` in the ``emails`` variable allows its result
   cache to be re-used.
  
  #. Since QuerySets are lazy, this does no database queries if 'display_inbox'
     is False.
  
#. Storing ``user.emails.all()`` in the ``emails`` variable allows its result
   cache to be re-used.
#. Storing ``user.emails.all()`` in the ``emails`` variable allows its result
   cache to be re-used.
  
  #. The line ``{% if emails %}`` causes ``QuerySet.__bool__()`` to be called,
     which causes the ``user.emails.all()`` query to be run on the database, and
     at the least the first line to be turned into an ORM object. If there aren't
     any results, it will return False, otherwise True.
keep keep keep keep replace replace keep keep keep keep keep
 Changed "Don't overuse count() or exists()" example to Python. 
docs/topics/db/optimization.txt