id,summary,reporter,owner,description,type,status,component,version,severity,resolution,keywords,cc,stage,has_patch,needs_docs,needs_tests,needs_better_patch,easy,ui_ux 19029,Reuse QuerySet._result_cache during IN clause,Matt Robenolt,Matt Robenolt,"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.",Cleanup/optimization,closed,"Database layer (models, ORM)",1.4,Normal,wontfix,"orm, queryset, result_cache, optimization, subquery",Florian Apolloner charette.s@…,Design decision needed,1,0,0,0,0,0