comparison gpp/forums/feeds.py @ 388:c3231af55778

For #191; r410 is wrong: it is returning the wrong posts. Rework. MySQL is not using an index on our query, and it is taking 10+ seconds. Replace this slow query with a loop that loops over the public forums, then sorts and returns the posts.
author Brian Neal <bgneal@gmail.com>
date Sat, 19 Mar 2011 05:03:51 +0000
parents b15726767ab8
children 0398aae48807
comparison
equal deleted inserted replaced
387:b15726767ab8 388:c3231af55778
58 # 58 #
59 # This didn't work real well on InnoDb: 59 # This didn't work real well on InnoDb:
60 # items = Post.objects.filter( 60 # items = Post.objects.filter(
61 # topic__forum__in=Forum.objects.public_forums()) 61 # topic__forum__in=Forum.objects.public_forums())
62 # 62 #
63 # The where clause in the generated SQL looked like this: 63 # For some reason, MySQL wasn't using an index when "in" was used.
64 # WHERE `forums_topic`.`forum_id` IN ( ... ) 64 # So let's do this the hard way as a work-around:
65 # This was terrible for performance. This does much better:
66 # WHERE `forums_topic`.`id` IN ( ... )
67 #
68 # So we use Django's .extra() to force the above.
69 65
70 public_forum_ids = Forum.objects.public_forum_ids() 66 public_forum_ids = Forum.objects.public_forum_ids()
71 ids = ','.join(str(pub) for pub in public_forum_ids) 67 posts = []
68 for forum_id in public_forum_ids:
69 posts.extend(list(Post.objects.filter(
70 topic__forum__id=forum_id).order_by('-creation_date').select_related(depth=2)[:30]))
72 71
73 items = Post.objects.extra( 72 posts.sort(key=lambda x: x.creation_date, reverse=True)
74 where=['forums_topic.id in (%s)' % ids]) 73 return posts[:30]
75 74
76 else: 75 else:
77 items = Post.objects.filter(topic__forum__id=obj.id) 76 return Post.objects.filter(topic__forum__id=obj.id).order_by(
78 return items.order_by('-creation_date').select_related(depth=2)[:30] 77 '-creation_date').select_related(depth=2)[:30]
79 78
80 def item_title(self, item): 79 def item_title(self, item):
81 return item.topic.name 80 return item.topic.name
82 81
83 def item_description(self, item): 82 def item_description(self, item):