Ticket #15361: 15361-3.diff

File 15361-3.diff, 3.8 KB (added by Tim Graham, 12 years ago)
  • docs/topics/db/optimization.txt

    diff --git a/docs/topics/db/optimization.txt b/docs/topics/db/optimization.txt
    index 772792d..02fa22c 100644
    a b Write your own :doc:`custom SQL to retrieve data or populate models  
    132132</topics/db/sql>`. Use ``django.db.connection.queries`` to find out what Django
    133133is writing for you and start from there.
    134134
     135Retrieve individual objects using a unique, indexed column
     136==========================================================
     137
     138There are two reasons to use a column with
     139:attr:`~django.db.models.Field.unique` or
     140:attr:`~django.db.models.Field.db_index` when using
     141:meth:`~django.db.models.query.QuerySet.get` to retrieve individual objects.
     142First, the query will be quicker because of the underlying database index.
     143Also, the query could run much slower if multiple objects match the lookup;
     144having a unique constraint on the column guarantees this will never happen.
     145
     146So using the :ref:`example Weblog models <queryset-model-example>`::
     147
     148  >>> entry = Entry.objects.get(id=10)
     149
     150will be quicker than:
     151
     152  >>> entry = Entry.object.get(headline="News Item Title")
     153
     154because ``id`` is indexed by the database and is guaranteed to be unique.
     155
     156Doing the following is potentially quite slow:
     157
     158  >>> entry = Entry.objects.get(headline__startswith="News")
     159
     160First of all, `headline` is not indexed, which will make the underlying
     161database fetch slower.
     162
     163Second, the lookup doesn't guarantee that only one object will be returned.
     164If the query matches more than one object, it will retreive and transfer all of
     165them from the database. This penalty could be substantial if hundreds or
     166thousands of records are returned. The penalty will be compounded if the
     167database lives on a separate server, where network overhead and latency also
     168play a factor.
     169
    135170Retrieve everything at once if you know you will need it
    136171========================================================
    137172
  • tests/modeltests/basic/tests.py

    diff --git a/tests/modeltests/basic/tests.py b/tests/modeltests/basic/tests.py
    index d96c60b..e2d356c 100644
    a b from __future__ import absolute_import, unicode_literals  
    22
    33from datetime import datetime
    44
    5 from django.core.exceptions import ObjectDoesNotExist
     5from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
    66from django.db.models.fields import Field, FieldDoesNotExist
    77from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature
    88from django.utils.six import PY3
    class ModelTest(TestCase):  
    128128        b = Article.objects.get(pk=a.id)
    129129        self.assertEqual(a, b)
    130130
     131        # Create a very similar object
     132        a = Article(
     133            id=None,
     134            headline='Area man programs in Python',
     135            pub_date=datetime(2005, 7, 28),
     136        )
     137        a.save()
     138
     139        self.assertEqual(Article.objects.count(), 2)
     140
     141        # Django raises an Article.MultipleObjectsReturned exception if the
     142        # lookup matches more than one object
     143        self.assertRaisesRegexp(
     144            MultipleObjectsReturned,
     145            "get\(\) returned more than one Article -- it returned 2!",
     146            Article.objects.get,
     147            headline__startswith='Area',
     148        )
     149
     150        self.assertRaisesRegexp(
     151            MultipleObjectsReturned,
     152            "get\(\) returned more than one Article -- it returned 2!",
     153            Article.objects.get,
     154            pub_date__year=2005,
     155        )
     156
     157        self.assertRaisesRegexp(
     158            MultipleObjectsReturned,
     159            "get\(\) returned more than one Article -- it returned 2!",
     160            Article.objects.get,
     161            pub_date__year=2005,
     162            pub_date__month=7,
     163        )
     164
    131165    def test_object_creation(self):
    132166        # Create an Article.
    133167        a = Article(
Back to Top