以下哪个查询的成本最低?

时间:2016-01-18 08:23:07

标签: ruby-on-rails ruby

以下哪个查询的费用最低?

一个。

def recent_followers
  self.followers.recent.includes(:user).collect {|f| f.user.name }.to_sentence
end

Select followers where user_id = 1

Select users where user_id in (2,3,4,5)

1 个答案:

答案 0 :(得分:0)

数据库查询总是比Ruby处理更快。

您的第一个选项使用collect,因为它必须在处理之前将整个集合加载到内存中。

您可以将您的第一次尝试重写为:

followers.recent.joins(:user).pluck('users.name') # no need for self, btw