PERF: avoid left joins and distinct on posts_for query

Previously this was extremely expensive due to the `distict` and
`left join` clauses, this eliminates it and makes query much faster
This commit is contained in:
Sam 2018-11-12 13:26:41 +11:00
parent e17a13ce19
commit d1684a1652
1 changed files with 2 additions and 1 deletions

View File

@ -193,7 +193,8 @@ class Group < ActiveRecord::Base
def posts_for(guardian, opts = nil)
opts ||= {}
result = Post.includes(:topic, user: :groups, topic: :category)
result = Post.joins(:topic, user: :groups, topic: :category)
.preload(:topic, user: :groups, topic: :category)
.references(:posts, :topics, :category)
.where(groups: { id: id })
.where('topics.archetype <> ?', Archetype.private_message)