mirror of
https://github.com/discourse/discourse.git
synced 2025-03-06 11:19:51 +00:00
PERF: Use joins for Post.for_mailing_list
instead of NOT IN
.
Joining on the topics table and then filtering the topics against a large set of ids is so much slower than doing a join on a sub query.
This commit is contained in:
parent
84eb9bc1c9
commit
6525613b89
@ -86,15 +86,16 @@ class Post < ActiveRecord::Base
|
|||||||
scope :with_topic_subtype, ->(subtype) { joins(:topic).where('topics.subtype = ?', subtype) }
|
scope :with_topic_subtype, ->(subtype) { joins(:topic).where('topics.subtype = ?', subtype) }
|
||||||
scope :visible, -> { joins(:topic).where('topics.visible = true').where(hidden: false) }
|
scope :visible, -> { joins(:topic).where('topics.visible = true').where(hidden: false) }
|
||||||
scope :secured, -> (guardian) { where('posts.post_type IN (?)', Topic.visible_post_types(guardian&.user)) }
|
scope :secured, -> (guardian) { where('posts.post_type IN (?)', Topic.visible_post_types(guardian&.user)) }
|
||||||
|
|
||||||
scope :for_mailing_list, ->(user, since) {
|
scope :for_mailing_list, ->(user, since) {
|
||||||
q = created_since(since)
|
q = created_since(since)
|
||||||
.joins(:topic)
|
.joins("INNER JOIN (#{Topic.for_digest(user, Time.at(0)).select(:id).to_sql}) AS digest_topics ON digest_topics.id = posts.topic_id") # we want all topics with new content, regardless when they were created
|
||||||
.where(topic: Topic.for_digest(user, Time.at(0))) # we want all topics with new content, regardless when they were created
|
.order('posts.created_at ASC')
|
||||||
|
|
||||||
q = q.where.not(post_type: Post.types[:whisper]) unless user.staff?
|
q = q.where.not(post_type: Post.types[:whisper]) unless user.staff?
|
||||||
|
q
|
||||||
q.order('posts.created_at ASC')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
scope :raw_match, -> (pattern, type = 'string') {
|
scope :raw_match, -> (pattern, type = 'string') {
|
||||||
type = type&.downcase
|
type = type&.downcase
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user