PERF: Perform only one category update when creating a new topic (#19361)

When under extremely high load, it has been observed that updating the categories table when creating a new topic can become a bottleneck.

This change will reduce the two updates to one when a new topic is created within a category, and therefore should help with performance when under extremely high load.
This commit is contained in:
jbrw 2022-12-07 14:35:13 -05:00 committed by GitHub
parent 16e3bc3ff4
commit 64b781dacf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -425,8 +425,11 @@ class PostCreator
def track_latest_on_category def track_latest_on_category
return unless @post && @post.errors.count == 0 && @topic && @topic.category_id return unless @post && @post.errors.count == 0 && @topic && @topic.category_id
if @post.is_first_post?
Category.where(id: @topic.category_id).update_all(latest_topic_id: @topic.id, latest_post_id: @post.id)
else
Category.where(id: @topic.category_id).update_all(latest_post_id: @post.id) Category.where(id: @topic.category_id).update_all(latest_post_id: @post.id)
Category.where(id: @topic.category_id).update_all(latest_topic_id: @topic.id) if @post.is_first_post? end
end end
def ensure_in_allowed_users def ensure_in_allowed_users