PERF: Help postgres make use of index in `Post.summary`.

This commit is contained in:
Guo Xiang Tan 2018-06-21 13:26:26 +08:00
parent 5cef4e281b
commit ac80360bea
1 changed files with 16 additions and 7 deletions

View File

@ -349,13 +349,22 @@ class Post < ActiveRecord::Base
# percent rank has tons of ties # percent rank has tons of ties
where(topic_id: topic_id) where(topic_id: topic_id)
.where(["(post_number = 1) or id in ( .where([
"id = ANY(
(
SELECT posts.id
FROM posts
WHERE posts.topic_id = #{topic_id.to_i}
AND post_number = 1
) UNION
(
SELECT p1.id SELECT p1.id
FROM posts p1 FROM posts p1
WHERE p1.percent_rank <= ? AND WHERE p1.percent_rank <= ?
p1.topic_id = #{topic_id} AND p1.topic_id = #{topic_id.to_i}
ORDER BY p1.percent_rank ORDER BY p1.percent_rank
LIMIT ? LIMIT ?
)
)", )",
SiteSetting.summary_percent_filter.to_f / 100.0, SiteSetting.summary_percent_filter.to_f / 100.0,
SiteSetting.summary_max_results SiteSetting.summary_max_results