PERF: Bypass AR and execute SQL directly.

This commit is contained in:
Guo Xiang Tan 2017-09-12 14:05:25 +08:00
parent 18142d8abf
commit d3a409456f
1 changed files with 15 additions and 6 deletions

View File

@ -1224,12 +1224,21 @@ SQL
end end
def pm_with_non_human_user? def pm_with_non_human_user?
Topic.private_messages sql = <<~SQL
.joins("LEFT JOIN topic_allowed_groups ON topics.id = topic_allowed_groups.topic_id") SELECT 1 FROM topics
.where("topic_allowed_groups.topic_id IS NULL") LEFT JOIN topic_allowed_groups ON topics.id = topic_allowed_groups.topic_id
.where("topics.id = ?", self.id) WHERE topic_allowed_groups.topic_id IS NULL
.where("(SELECT COUNT(*) FROM topic_allowed_users WHERE topic_allowed_users.topic_id = ? AND topic_allowed_users.user_id > 0) = 1", self.id) AND topics.archetype = :private_message
.exists? AND topics.id = :topic_id
AND (
SELECT COUNT(*) FROM topic_allowed_users
WHERE topic_allowed_users.topic_id = :topic_id
AND topic_allowed_users.user_id > 0
) = 1
SQL
result = Topic.exec_sql(sql, private_message: Archetype.private_message, topic_id: self.id)
result.ntuples != 0
end end
private private