Revert "PERF: avoid OR in complex query"

This reverts commit a8dc6daa38.
This commit is contained in:
Sam 2014-09-10 17:10:57 +10:00
parent 126dfa0ec7
commit 91dcc56fb3
1 changed files with 2 additions and 10 deletions

View File

@ -144,7 +144,7 @@ class TopicTrackingState
LEFT JOIN categories c ON c.id = topics.category_id LEFT JOIN categories c ON c.id = topics.category_id
WHERE u.id IN (:user_ids) AND WHERE u.id IN (:user_ids) AND
topics.archetype <> 'private_message' AND topics.archetype <> 'private_message' AND
(**COND**) AND ((#{unread}) OR (#{new})) AND
(topics.visible OR u.admin OR u.moderator) AND (topics.visible OR u.admin OR u.moderator) AND
topics.deleted_at IS NULL AND topics.deleted_at IS NULL AND
( category_id IS NULL OR NOT c.read_restricted OR category_id IN ( ( category_id IS NULL OR NOT c.read_restricted OR category_id IN (
@ -163,19 +163,11 @@ SQL
if topic_id if topic_id
sql << " AND topics.id = :topic_id" sql << " AND topics.id = :topic_id"
end end
sql << " ORDER BY topics.bumped_at DESC LIMIT 250" sql << " ORDER BY topics.bumped_at DESC LIMIT 500"
sql = wrap(sql.sub("**COND**", new), "A") << " UNION ALL " << wrap(sql.sub("**COND**", unread),"B")
SqlBuilder.new(sql) SqlBuilder.new(sql)
.map_exec(TopicTrackingState, user_ids: user_ids, topic_id: topic_id) .map_exec(TopicTrackingState, user_ids: user_ids, topic_id: topic_id)
end end
private
def self.wrap(query, name)
"SELECT * FROM (#{query}) #{name}"
end
end end