Skip to content

Commit

Permalink
Revert "PERF: avoid OR in complex query"
Browse files Browse the repository at this point in the history
This reverts commit a8dc6da.
  • Loading branch information
SamSaffron committed Sep 10, 2014
1 parent 126dfa0 commit 91dcc56
Showing 1 changed file with 2 additions and 10 deletions.
12 changes: 2 additions & 10 deletions app/models/topic_tracking_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def self.report(user_ids, topic_id = nil)
LEFT JOIN categories c ON c.id = topics.category_id
WHERE u.id IN (:user_ids) AND
topics.archetype <> 'private_message' AND
(**COND**) AND
((#{unread}) OR (#{new})) AND
(topics.visible OR u.admin OR u.moderator) AND
topics.deleted_at IS NULL AND
( category_id IS NULL OR NOT c.read_restricted OR category_id IN (
Expand All @@ -163,19 +163,11 @@ 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 250"

sql = wrap(sql.sub("**COND**", new), "A") << " UNION ALL " << wrap(sql.sub("**COND**", unread),"B")
sql << " ORDER BY topics.bumped_at DESC LIMIT 500"

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

end

private

def self.wrap(query, name)
"SELECT * FROM (#{query}) #{name}"
end

end

0 comments on commit 91dcc56

Please sign in to comment.