【问题标题】:Eager load Rails 4 - still multiple queries急切加载 Rails 4 - 仍然是多个查询
【发布时间】:2015-11-10 14:24:48
【问题描述】:

我有这个代码:

class User < ActiveRecord::Base
   has_many :people
end

class Person < ActiveRecord::Base
   belongs_to :user
end

def map_people people
   people.map {|person|
     map_person(person)
   }
end

def map_person person
   {
     :person_id => person.id,
     :user_id => person.user.id,
     :name => person.user.name
   }
end

当我跑步时:

map_people(Person.with_deleted.includes(:user))

我来了

 Person Load (1.4ms)  SELECT `people`.* FROM `people`
 User Load (2.5ms)  SELECT `users`.* FROM `users` WHERE `users`.`deleted_at` IS NULL AND `users`.`id` IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 76, 78, 79, 80, 81, 83, 84, 85, 87, 88, 89, 90, 91, 92, 75, 93, 94, 95, 96, 97, 98, 99, 100, 101, 104, 105, 106, 107, 108, 109, 112, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 127, 128, 135, 142, 143)  ORDER BY users.updated_at DESC
 User Load (0.4ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1  ORDER BY users.updated_at DESC LIMIT 1
 User Load (0.4ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 2  ORDER BY users.updated_at DESC LIMIT 1
 User Load (0.5ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 3  ORDER BY users.updated_at DESC LIMIT 1
 User Load (0.6ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 3  ORDER BY users.updated_at DESC LIMIT 1
 User Load (0.6ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 2  ORDER BY users.updated_at DESC LIMIT 1
 User Load (0.6ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 2  ORDER BY users.updated_at DESC LIMIT 1
 User Load (0.4ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1  ORDER BY users.updated_at DESC LIMIT 1

还有更多 SELECT 'users'.* FROM ..... 查询

Hos 是否可以通过一个查询加载所有内容?

谢谢!

大卫

【问题讨论】:

  • 看起来你在使用 Acts As Paranoid,amirite?
  • 是的,我愿意。当我使用 Person.includes(:user) 以及使用 Person.with_deleted.includes(:user) 时,它的作用相同
  • 据我所知,它应该可以工作。下班后我会尝试复制它。你能告诉我 Rails 版本和数据库吗?
  • 当然,Rails 4.2.4 和 MySQL。谢谢!
  • 嗨@AFaderDarkly,您还有其他想法吗?

标签: mysql ruby-on-rails ruby-on-rails-4


【解决方案1】:

我会尝试使用 eager_load。这将首先将所有人拉入内存(使用一个查询),然后您可以遍历每个人,每次调用 map_person 方法。

这应该可行:

def map_people
  all_people = Person.eager_load(:user).with_deleted.to_a
  all_people.map { |person| map_person(person) }
end

如果您愿意,也可以将 people 数组传递给方法,如下所示:

def map_people(people)
  people.map { |person| map_person(person) }
end

all_people = Person.eager_load(:user).with_deleted.to_a
map_people(all_people)

【讨论】:

  • 我刚刚将 People.eager_load.. 更改为 Person.eager_load,我仍然收到相同的多个数据库请求..
  • 哎呀,抱歉的意思是人而不是人。尝试“.to_a”而不是“.all”。这行得通吗?
【解决方案2】:

尝试运行

map_people(Person.with_deleted.preload(:user))

顺便说一句,关于这个主题有一篇不错的文章:

http://blog.arkency.com/2013/12/rails4-preloading/

【讨论】:

    【解决方案3】:

    对不起,我的错误(类人):

      def user
         @user ||= user_id ? User.with_deleted.find(user_id) : nil
      end
    

    所以它总是再次加载

    【讨论】:

      猜你喜欢
      • 2014-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多