#27228: prefetch_related with select_related takes a lot of query
-----------------------------+---------------------------------------------
Reporter: aki33524 | Owner: nobody
Type: Bug | Status: new
Component: | Version: 1.10
Uncategorized |
Severity: Normal | Keywords: prefetch_related select_related
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+---------------------------------------------
mdoels.py
{{{#!python
class A(models.Model):
pass
class B(models.Model):
pass
class C(models.Model):
a = models.ForeignKey(A)
b = models.ForeignKey(B)
}}}
python manage.py shell
{{{#!python
for i in range(100):
a = A()
a.save()
b = B()
b.save()
C(a=a, b=b).save()
queryset = A.objects.prefetch_related('c_set')
for v in queryset:
print(v.c_set.count())
# 2 queries
queryset = C.objects.select_related()
for v in queryset:
print(v.a.id)
# 2 queries
l = A.objects.prefetch_related(Prefetch('c_set',
queryset=C.objects.select_related('b')))
for v in l:
print(v.c_set.filter(b__id=1))
# 100 queries
}}}
I run this code in Django1.10.1, but the last takes a lot of queries. I
think that the last can take only few queries.
--
Ticket URL: <https://code.djangoproject.com/ticket/27228>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/051.b74c8eb19f1f613a95e872daecfff1c6%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.