From ba3078e0980d82cee0064a18c51407dcc4202b25 Mon Sep 17 00:00:00 2001 From: Dan Ungureanu Date: Thu, 28 Oct 2021 11:30:30 +0300 Subject: [PATCH] PERF: Use different column for better query plan (#14748) Using topics.id provides a better query plan than posts.topic_id which speeds up search by almost 50%. --- app/models/post.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index 5a8488e2ada..7120db505e8 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -86,8 +86,8 @@ class Post < ActiveRecord::Base scope :private_posts_for_user, ->(user) do where( - "posts.topic_id IN (#{Topic::PRIVATE_MESSAGES_SQL_USER}) - OR posts.topic_id IN (#{Topic::PRIVATE_MESSAGES_SQL_GROUP})", + "topics.id IN (#{Topic::PRIVATE_MESSAGES_SQL_USER}) + OR topics.id IN (#{Topic::PRIVATE_MESSAGES_SQL_GROUP})", user_id: user.id ) end