0

例如,我有:

class School < ActiveRecord::Base
  has_many :students
end

one_school = School.first

使用之间是否存在速度差异:

Student.find :all, :conditions => { :first_name => "John", :school_id => one_school.id }

one_school.students.find :all, :conditions => { :first_name => "John" }

我想知道调用“one_school.students.find”是否会遍历所有学生记录,还是只会遍历属于 one_school 的学生记录?

这更多是关于性能的问题。我需要知道后一个查询在 Rails 中是否真的更快。

4

1 回答 1

0

这基本上是相同的(Railsjoin在这两种情况下都会使用 a),您可以在服务器控制台中查看查询及其性能并进行比较。为了提高性能,请确保您school_id在学生表上建立索引。

于 2013-05-28T09:32:44.983 回答