Ticket #15361: 15361-3.diff
File 15361-3.diff, 3.8 KB (added by , 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 132 132 </topics/db/sql>`. Use ``django.db.connection.queries`` to find out what Django 133 133 is writing for you and start from there. 134 134 135 Retrieve individual objects using a unique, indexed column 136 ========================================================== 137 138 There 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. 142 First, the query will be quicker because of the underlying database index. 143 Also, the query could run much slower if multiple objects match the lookup; 144 having a unique constraint on the column guarantees this will never happen. 145 146 So using the :ref:`example Weblog models <queryset-model-example>`:: 147 148 >>> entry = Entry.objects.get(id=10) 149 150 will be quicker than: 151 152 >>> entry = Entry.object.get(headline="News Item Title") 153 154 because ``id`` is indexed by the database and is guaranteed to be unique. 155 156 Doing the following is potentially quite slow: 157 158 >>> entry = Entry.objects.get(headline__startswith="News") 159 160 First of all, `headline` is not indexed, which will make the underlying 161 database fetch slower. 162 163 Second, the lookup doesn't guarantee that only one object will be returned. 164 If the query matches more than one object, it will retreive and transfer all of 165 them from the database. This penalty could be substantial if hundreds or 166 thousands of records are returned. The penalty will be compounded if the 167 database lives on a separate server, where network overhead and latency also 168 play a factor. 169 135 170 Retrieve everything at once if you know you will need it 136 171 ======================================================== 137 172 -
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 2 2 3 3 from datetime import datetime 4 4 5 from django.core.exceptions import ObjectDoesNotExist 5 from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned 6 6 from django.db.models.fields import Field, FieldDoesNotExist 7 7 from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature 8 8 from django.utils.six import PY3 … … class ModelTest(TestCase): 128 128 b = Article.objects.get(pk=a.id) 129 129 self.assertEqual(a, b) 130 130 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 131 165 def test_object_creation(self): 132 166 # Create an Article. 133 167 a = Article(