mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
cache reply_to_user_id in post to eliminate a very nasty n+1 query
This commit is contained in:
16
db/migrate/20130319122248_add_reply_to_user_id_to_post.rb
Normal file
16
db/migrate/20130319122248_add_reply_to_user_id_to_post.rb
Normal file
@@ -0,0 +1,16 @@
|
||||
class AddReplyToUserIdToPost < ActiveRecord::Migration
|
||||
def up
|
||||
# caching this column makes the topic page WAY faster
|
||||
add_column :posts, :reply_to_user_id, :integer
|
||||
execute 'UPDATE posts p SET reply_to_user_id = (
|
||||
SELECT u.id from users u
|
||||
JOIN posts p2 ON p2.user_id = u.id AND
|
||||
p2.post_number = p.reply_to_post_number AND
|
||||
p2.topic_id = p.topic_id
|
||||
)'
|
||||
end
|
||||
|
||||
def down
|
||||
remove_column :posts, :reply_to_user_id
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user