以下是三个连续查询及其基准性能:
ids = @Company.projects.submitted.uniq.collect(&:person_id)
1.370000 0.060000 1.430000 ( 3.763946)
@persons = Person.where("id IN (?)", ids)
0.030000 0.000000 0.030000 ( 0.332878)
@emails = @persons.collect(&:email).reject(&:blank?)
16.550000 1.640000 18.190000 (128.002465)
ids
包含近 10000 个 ID,在运行我看到的最后一个查询时:
SELECT "persons".* FROM "persons" WHERE (id in (121,142,173,178...14202))
(*1000s ->) User Load (13.0ms) SELECT "users".* FROM "users" WHERE "users"."roleable_type" = 'Person' AND "users"."roleable_id" = 121 LIMIT 1
Indexes on User:
add_index "users", ["roleable_id", "roleable_type"], :name => "index_users_on_roleable_id_and_roleable_type"
add_index "users", ["roleable_type", "roleable_id"], :name => "index_users_on_roleable_type_and_roleable_id"
我该如何解决这里发生的事情?