diff --git a/app/models/post.rb b/app/models/post.rb index 0d6252e19..b13e2d780 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -251,8 +251,23 @@ class Post < ActiveRecord::Base order('sort_order desc, post_number desc') end - def self.summary - where(["(post_number = 1) or (percent_rank <= ?)", SiteSetting.summary_percent_filter.to_f / 100.0]).limit(SiteSetting.summary_max_results) + def self.summary(topic_id=nil) + # PERF: if you pass in nil it is WAY slower + # pg chokes getting a reasonable plan + topic_id = topic_id ? topic_id.to_i : "posts.topic_id" + + # percent rank has tons of ties + where(["post_number = 1 or id in ( + SELECT p1.id + FROM posts p1 + WHERE p1.percent_rank <= ? AND + p1.topic_id = #{topic_id} + ORDER BY p1.percent_rank + LIMIT ? + )", + SiteSetting.summary_percent_filter.to_f / 100.0, + SiteSetting.summary_max_results + ]) end def update_flagged_posts_count diff --git a/lib/topic_view.rb b/lib/topic_view.rb index 6a440eade..b9ffd6434 100644 --- a/lib/topic_view.rb +++ b/lib/topic_view.rb @@ -342,7 +342,7 @@ class TopicView # Filters if @filter == 'summary' - @filtered_posts = @filtered_posts.summary + @filtered_posts = @filtered_posts.summary(@topic.id) @contains_gaps = true end