  
  #. The use of ``{{ emails|length }}`` calls ``QuerySet.__len__()``, filling
     out the rest of the cache without doing another query.
  
  #. The :ttag:`for` loop iterates over the already filled cache.
  
  In total, this code does either one or zero database queries. The only
  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> 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. </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 #. 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 #. Use of :ttag:`with` means that we store ``user.emails.all`` in a variable
   for later use, allowing its cache to be re-used.
 </s> add #. Storing ``user.emails.all()`` in the ``emails`` variable allows its result
   cache to be re-used. </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``.
#. The ``for`` loop iterates over the already filled cache.
  
  #. The use of ``{{ emails|length }}`` calls ``QuerySet.__len__()``, filling
     out the rest of the cache without doing another query.
  
#. The ``for`` loop iterates over the already filled cache.
  
  In total, this code does either one or zero database queries. The only
  deliberate optimization performed is the use of the :ttag:`with` tag. Using
  ``QuerySet.exists()`` or ``QuerySet.count()`` at any point would cause
  additional queries.
keep keep keep keep replace keep keep keep keep keep
 Changed "Don't overuse count() or exists()" example to Python. 
docs/topics/db/optimization.txt