What you are doing is querying the database once for each user to get their ID, because of your q.user.id. This means a million separate queries. You will be better off getting the id directly off the quiz table:
User.objects.filter(id__in=[q.user_id for q in Quiz.objects.filter(score__gt=90)]) Note that the problem is not with the 'in' operation. Cheers, Cliff On Mon, 2010-05-24 at 05:15 -0700, omat wrote: > ops, a small correction. i meant: > > User.objects.filter(id__in=[q.user.id for q in > Quiz.objects.filter(score__gt=90)]) > > -- > oMat > > > On May 24, 3:12 pm, omat <o...@gezgin.com> wrote: > > Hi All, > > > > I have a Quiz model, which is related to the User model by a > > ForeignKey. I want to get a queryset of users that have a certain > > score. > > > > I want to avoid: > > > > User.objects.filter(id__in=[u.id for u in > > Quiz.objects.filter(score__gt=90)]) > > > > As there are about 1 million users, this is deadly slow. > > > > What is the optimal way of doing this? > > > > Thanks, > > oMat > > > > -- > > You received this message because you are subscribed to the Google Groups > > "Django users" group. > > To post to this group, send email to django-us...@googlegroups.com. > > To unsubscribe from this group, send email to > > django-users+unsubscr...@googlegroups.com. > > For more options, visit this group > > athttp://groups.google.com/group/django-users?hl=en. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.