On Mar 23, 9:55 pm, Javier Guerra Giraldez <jav...@guerrag.com> wrote: > On Wed, Mar 23, 2011 at 8:49 PM, Brian Neal <bgn...@gmail.com> wrote: > > items = Post.objects(filter=forum__topic__in=forums).order_by('- > > topic__update_date', '-update_date').select_related(# as before)[:30] > > > But this had the same result as before. I checked the resulting SQL, > > and it looked right, so I did an explain and it was pretty much as > > before. It decided not to use the index. > > bummer :-( > > > So I broke it up into two queries: > > > topic_ids = list(Topic.objects.filter(forum__in=forums).order_by( > > '-update_date').values_list('id', flat=True)[:30]) > > items = Post.objects.filter(topic__in=topic_ids).select_related(# as > > before)[:30] > > did you try: > > items = > Post.objects.filter(topic__in=Topic.objects.filter(forum__in=forums).order_ > by('-update_date')[:30]).select_related(# > as before)[:30] > > IOW: use the first queryset for the '__in' clause, no need to flatten > to a list of IDs. that allows the ORM to weave a subselect > (seehttp://docs.djangoproject.com/en/1.3/ref/models/querysets/#in). > unfortunately, the docs also warn about the less-than-ideal MySQL > optimizer, so in many cases it turns to be faster to do just like you > did. (i guess that specially in these limited-size queries).
I did try that. In fact, just remove the list() around the Topic query from the code I posted above and that was what I tried first. I got a DatabaseError with an elaborate message about how this type of nested query or something wasn't supported by the version of MySQL I had. Sorry I didn't save the exact error message. Again, many thanks. BN -- 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.