﻿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
19264	prefetch_one_level is slow	Simon Percivall	nobody	"`prefetch_one_level` in `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."	Cleanup/optimization	closed	Database layer (models, ORM)	1.4	Normal	duplicate		percivall@…	Accepted	0	0	0	0	0	0
