#19029: Reuse QuerySet._result_cache during IN clause
-------------------------------------+-------------------------------------
     Reporter:  mattrobenolt         |      Owner:  nobody
         Type:                       |     Status:  new
  Cleanup/optimization               |    Version:  1.4
    Component:  Database layer       |   Keywords:  orm, queryset,
  (models, ORM)                      |  result_cache, optimization,
     Severity:  Normal               |  subquery
 Triage Stage:  Unreviewed           |  Has patch:  0
Easy pickings:  0                    |      UI/UX:  0
-------------------------------------+-------------------------------------
 If I have a `QuerySet` that has been evaluated previously, and shove it
 into an `__in` clause, a subquery is still generated.

 Expected result:[[BR]]
 * Reuse a list of primary keys instead, to prevent the database from
 performing the subquery.

 {{{#!python
 cities = City.objects.all()
 list(cities)  # Force evaluation of cities to populate cache
 print cities._result_cache is not None  # Verify that the cache is filled
 Event.objects.filter(venue__city__in=cities)  # This should use just the
 pks instead of a subquery
 }}}

 I began digging through the ORM, and found the source of the problem being
 that the origin `QuerySet` object is being clone()'d, causing it's
 `_result_cache` to be `None`.

 The simple solution for now is to not pass a `QuerySet` into an IN clause
 if we know the set of ids. Can be remedied by something like `[m.pk for m
 in qs]`, or just `list(qs)`.

 For the latter solution, I think the documentation should be updated to
 reflect this distinction to avoid unexpected results and performance
 degradation.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/19029>
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 post to this group, send email to django-updates@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to