Skip to content

Commit

Permalink
PERF: work around LIMIT breaking query plan
Browse files Browse the repository at this point in the history
(CTE is a optimisation boundary, so limit no longer has any effect on plan)
  • Loading branch information
SamSaffron committed Sep 10, 2014
1 parent c80ef9e commit ca2100d
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/models/topic_tracking_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ def self.report(user_ids, topic_id = nil)
new = TopicQuery.new_filter(Topic, "xxx").where_values.join(" AND ").gsub!("'xxx'", treat_as_new_topic_clause)

sql = <<SQL
WITH x AS (
SELECT u.id AS user_id,
topics.id AS topic_id,
topics.created_at,
Expand Down Expand Up @@ -163,7 +164,8 @@ def self.report(user_ids, topic_id = nil)
if topic_id
sql << " AND topics.id = :topic_id"
end
sql << " ORDER BY topics.bumped_at DESC LIMIT 500"

sql << " ORDER BY topics.bumped_at DESC ) SELECT * FROM x LIMIT 500"

SqlBuilder.new(sql)
.map_exec(TopicTrackingState, user_ids: user_ids, topic_id: topic_id)
Expand Down

0 comments on commit ca2100d

Please sign in to comment.