hi, you can try split the in statement, with less with "in".

forums = [1, 2, 3, 4, 5]
r = []
for f in forums :
    r.append(Post.objects.filter(topic__forum=f.id)....
return r

perhaps try to use the pure sql statement.
--
blog: http://chenxiaoyu.org



On Wed, Mar 23, 2011 at 8:22 AM, Brian Neal <bgn...@gmail.com> wrote:

> I apologize in advance as this is probably only tangentially related
> to Django.
>
> I have a forums type Django application. In my RSS feed class I had
> this Django model query:
>
> return Post.objects.filter(topic__forum__id=obj.id).order_by(
>                    '-creation_date').select_related(depth=2)[:30]
>
> It seemed to work okay, but was slow (2 seconds). I was able to reduce
> the time it took by more carefully extracting only the fields I needed
> with values_list().
>
> But then I changed it to use an "in" operation:
>
> forums = [1, 2, 3, 4, 5]
> return Post.objects.filter(topic__forum__in=forums).order_by(
>                    '-creation_date').select_related(depth=2)[:30]
>
> This query took over 40 seconds! Again, I can reduce this time using
> values_list and a smaller list of fields, but even then it is 4
> seconds or so.
>
> I did an EXPLAIN on each of the queries, see the link below. The top
> one is the fast one, and the bottom one is the slow one:
>
> http://dpaste.com/524697/
>
> I am not a MySQL expert and I need help interpreting the results. It
> looks like for whatever reason, on the slow one, MySQL identified a
> possible index, but then decided not to use it?
>
> I ended up working around it by doing the work in Python: I looped
> over all the forum ids in the "in" list, making the first query for
> each, and then had to combine and sort the results myself.
>
> I'm using InnoDB if that matters. Did I do something wrong or is this
> a MySQL issue?
>
> Thanks for any insights.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-users@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.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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.

Reply via email to