哪个给了我更好的性能 Activerecord find_by 或 where?

Ank*_*itG 2 activerecord ruby-on-rails

假设我有模型(帐户),其中包含以下列 account_id、user_id、party_id、date 和 name

我执行:

Account.find_by_account_id_and_user_id_and_party_id_and_date_and_name(1,2,3,"2012-03-12","Venus")
Run Code Online (Sandbox Code Playgroud)

和

   Account.where(:account_id => 1, :user_id => 2, :party_id => 3,:date => "2012-03-28",:name => "Venus").limit(1).first
Run Code Online (Sandbox Code Playgroud)

哪个会给我更好的表现?

Ver*_*cus 5

where为您提供略微更好的性能。find_by通过method_missing魔法实现;method_missing需要先调用对象的方法表以查看是否存在现有方法,如果没有则调用method_missing,而where只是正确找到方法。

要查看这方面的实际基准,请查看这篇文章。