﻿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
16902	select_related() results in a poor perfomance	Ivan Virabyan	nobody	"Consider this code:

{{{
s = time()
list(Comment.objects.all()[:500])
list(User.objects.all()[:500])
print 'separate queries', time() - s

s = time()
list(Comment.objects.select_related('author')[:500])
print 'select_related', time() - s
}}}

The result is surprising:

{{{
separate queries 0.126932859421
select_related 0.276528120041
}}}

As you can see, using select_related makes things two times slower. And it is not a query time, query time is just a few milliseconds. So I dived into implementation of get_cached_row, and found that everything is recalculated there for each row, even though most of the information may be calculated only once (outside the loop).

So I've made a patch, and after that version of query with select_related had nearly the same performance as with separate queries."	Cleanup/optimization	closed	Database layer (models, ORM)	dev	Normal	fixed	select_related, get_cached_row, perfomance		Ready for checkin	1	0	0	0	0	0
