Re: how to avoid "in" query for large sets?

2010-05-24 Thread Euan Goddard
I've found the best way to solve these problems is to use a values_list queryset and inject the result of the into the outer query. Django is smart and doesn't actually evaluate the values_list query and instead injects that as a sub query in the SQL. However in this case can't you simply do: Use

Re: how to avoid "in" query for large sets?

2010-05-24 Thread J. Cliff Dyer
I believe you're confused. Your reply is to Tomasz, not to me. Also, my solution requires no model changes. Go back and look at it again. Cheers, Cliff On Mon, 2010-05-24 at 06:58 -0700, omat wrote: > @cliff: you are right, but i am writing an extension to an existing > app. i want to use th

Re: how to avoid "in" query for large sets?

2010-05-24 Thread Tom Evans
On Mon, May 24, 2010 at 3:36 PM, Tom Evans wrote: > On Mon, May 24, 2010 at 3:23 PM, omat wrote: >> ops, this doesn't work, because the the Quiz model is pointing to the >> User model, and i want to filter on the quiz model and get the >> matching User instances. but in the example in the docs, t

Re: how to avoid "in" query for large sets?

2010-05-24 Thread Tom Evans
On Mon, May 24, 2010 at 3:23 PM, omat wrote: > ops, this doesn't work, because the the Quiz model is pointing to the > User model, and i want to filter on the quiz model and get the > matching User instances. but in the example in the docs, the query is > on the parent model. > > quiz_qs = Quiz.ob

Re: how to avoid "in" query for large sets?

2010-05-24 Thread omat
ops, this doesn't work, because the the Quiz model is pointing to the User model, and i want to filter on the quiz model and get the matching User instances. but in the example in the docs, the query is on the parent model. quiz_qs = Quiz.objects.filter(score__gt=90) User.objects.filter(quiz__in=q

Re: how to avoid "in" query for large sets?

2010-05-24 Thread omat
@cliff: you are right, but i am writing an extension to an existing app. i want to use the models as-is if possible. i found the part in the docs: inner_qs = Blog.objects.filter(name__contains='Cheddar') entries = Entry.objects.filter(blog__in=inner_qs) thanks. On May 24, 4:22 pm, Tomasz Ziel

Re: how to avoid "in" query for large sets?

2010-05-24 Thread Tomasz Zieliński
On 24 Maj, 08:58, Daniel Roseman wrote: > > User.objects.filter(id__in=Quiz.objects.filter(score__gt=90)) > Nice thing, is it documented somewhere (I think I haven't this before) ? -- Tomasz Zielinski http://pyconsultant.eu -- You received this message because you are subscribed to the Googl

Re: how to avoid "in" query for large sets?

2010-05-24 Thread Daniel Roseman
On May 24, 1:12 pm, omat 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 th

Re: how to avoid "in" query for large sets?

2010-05-24 Thread J. Cliff Dyer
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)])

Re: how to avoid "in" query for large sets?

2010-05-24 Thread omat
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 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 ce

how to avoid "in" query for large sets?

2010-05-24 Thread omat
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.