prefetch_one_level is slow
|Reported by:||Simon Percivall||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||1.4|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
django.db.models.query.py does a lot of I think unnecessary extra work.
For every obj in instances a
QuerySet is created for the related many to many by doing
getattr(obj, attname).all(). This
QuerySet however is never really used, instead its
_prefetched_objects_cache is filled with the prefetched items.
qs = getattr(obj, attname).all() qs._result_cache = vals obj._prefetched_objects_cache[cache_name] = qs
If instead you'd simply create a
QuerySet you save having to call
clone for every obj, which is very expensive.
qs = QuerySet(getattr(obj, attname).model) qs._result_cache = vals obj._prefetched_objects_cache[cache_name] = qs
For a function like
prefetch_related whose main purpose is to speed up a certain case of use, speed seems like a good quality. Maybe I've misunderstood an edge case here, but if not, this is an easy optimization that gives quite the speedup.
All modeltests on 1.4 pass with this change, by the way.