Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
D
django
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
django
Commits
feaf9f27
Kaydet (Commit)
feaf9f27
authored
Kas 02, 2012
tarafından
Tim Graham
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fixed #15361 - Documented performance considerations for QuerySet.get()
Thanks mmcnickle for the patch.
üst
92fc263a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
70 additions
and
1 deletion
+70
-1
optimization.txt
docs/topics/db/optimization.txt
+35
-0
tests.py
tests/modeltests/basic/tests.py
+35
-1
No files found.
docs/topics/db/optimization.txt
Dosyayı görüntüle @
feaf9f27
...
@@ -132,6 +132,41 @@ Write your own :doc:`custom SQL to retrieve data or populate models
...
@@ -132,6 +132,41 @@ Write your own :doc:`custom SQL to retrieve data or populate models
</topics/db/sql>`. Use ``django.db.connection.queries`` to find out what Django
</topics/db/sql>`. Use ``django.db.connection.queries`` to find out what Django
is writing for you and start from there.
is writing for you and start from there.
Retrieve individual objects using a unique, indexed column
==========================================================
There are two reasons to use a column with
:attr:`~django.db.models.Field.unique` or
:attr:`~django.db.models.Field.db_index` when using
:meth:`~django.db.models.query.QuerySet.get` to retrieve individual objects.
First, the query will be quicker because of the underlying database index.
Also, the query could run much slower if multiple objects match the lookup;
having a unique constraint on the column guarantees this will never happen.
So using the :ref:`example Weblog models <queryset-model-example>`::
>>> entry = Entry.objects.get(id=10)
will be quicker than:
>>> entry = Entry.object.get(headline="News Item Title")
because ``id`` is indexed by the database and is guaranteed to be unique.
Doing the following is potentially quite slow:
>>> entry = Entry.objects.get(headline__startswith="News")
First of all, `headline` is not indexed, which will make the underlying
database fetch slower.
Second, the lookup doesn't guarantee that only one object will be returned.
If the query matches more than one object, it will retrieve and transfer all of
them from the database. This penalty could be substantial if hundreds or
thousands of records are returned. The penalty will be compounded if the
database lives on a separate server, where network overhead and latency also
play a factor.
Retrieve everything at once if you know you will need it
Retrieve everything at once if you know you will need it
========================================================
========================================================
...
...
tests/modeltests/basic/tests.py
Dosyayı görüntüle @
feaf9f27
...
@@ -2,7 +2,7 @@ from __future__ import absolute_import, unicode_literals
...
@@ -2,7 +2,7 @@ from __future__ import absolute_import, unicode_literals
from
datetime
import
datetime
from
datetime
import
datetime
from
django.core.exceptions
import
ObjectDoesNotExist
from
django.core.exceptions
import
ObjectDoesNotExist
,
MultipleObjectsReturned
from
django.db.models.fields
import
Field
,
FieldDoesNotExist
from
django.db.models.fields
import
Field
,
FieldDoesNotExist
from
django.test
import
TestCase
,
skipIfDBFeature
,
skipUnlessDBFeature
from
django.test
import
TestCase
,
skipIfDBFeature
,
skipUnlessDBFeature
from
django.utils
import
six
from
django.utils
import
six
...
@@ -128,6 +128,40 @@ class ModelTest(TestCase):
...
@@ -128,6 +128,40 @@ class ModelTest(TestCase):
b
=
Article
.
objects
.
get
(
pk
=
a
.
id
)
b
=
Article
.
objects
.
get
(
pk
=
a
.
id
)
self
.
assertEqual
(
a
,
b
)
self
.
assertEqual
(
a
,
b
)
# Create a very similar object
a
=
Article
(
id
=
None
,
headline
=
'Area man programs in Python'
,
pub_date
=
datetime
(
2005
,
7
,
28
),
)
a
.
save
()
self
.
assertEqual
(
Article
.
objects
.
count
(),
2
)
# Django raises an Article.MultipleObjectsReturned exception if the
# lookup matches more than one object
self
.
assertRaisesRegexp
(
MultipleObjectsReturned
,
"get
\
(
\
) returned more than one Article -- it returned 2!"
,
Article
.
objects
.
get
,
headline__startswith
=
'Area'
,
)
self
.
assertRaisesRegexp
(
MultipleObjectsReturned
,
"get
\
(
\
) returned more than one Article -- it returned 2!"
,
Article
.
objects
.
get
,
pub_date__year
=
2005
,
)
self
.
assertRaisesRegexp
(
MultipleObjectsReturned
,
"get
\
(
\
) returned more than one Article -- it returned 2!"
,
Article
.
objects
.
get
,
pub_date__year
=
2005
,
pub_date__month
=
7
,
)
def
test_object_creation
(
self
):
def
test_object_creation
(
self
):
# Create an Article.
# Create an Article.
a
=
Article
(
a
=
Article
(
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment