Rails で find_each、find_in_batches を使ってみる
Ruby Ruby on Rails
Published: 2019-08-04

やったこと

Rails で大量のデータを処理するときに使う

  • find_each
  • find_in_batches

を使ってみます。

確認環境

$ ruby --version
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin17]

$ rails --version
Rails 5.2.3

調査

find_each

$ rails c
...

irb(main):005:0> Task.find_each(batch_size: 1){ |task| p task }
  Task Load (0.1ms)  SELECT  "tasks".* FROM "tasks" ORDER BY "tasks"."id" ASC LIMIT ?  [["LIMIT", 1]]
#<Task id: 4, created_at: "2019-06-12 12:50:53", updated_at: "2019-07-14 16:01:58", name: "name2">
  Task Load (0.1ms)  SELECT  "tasks".* FROM "tasks" WHERE "tasks"."id" > ? ORDER BY "tasks"."id" ASC LIMIT ?  [["id", 4], ["LIMIT", 1]]
#<Task id: 5, created_at: "2019-06-12 12:52:31", updated_at: "2019-06-12 12:52:31", name: "name1">
  Task Load (0.1ms)  SELECT  "tasks".* FROM "tasks" WHERE "tasks"."id" > ? ORDER BY "tasks"."id" ASC LIMIT ?  [["id", 5], ["LIMIT", 1]]
=> nil

find_in_batches

$ rails c
...

irb(main):006:0> Task.find_in_batches(batch_size: 1){ |task| p task }
  Task Load (0.1ms)  SELECT  "tasks".* FROM "tasks" ORDER BY "tasks"."id" ASC LIMIT ?  [["LIMIT", 1]]
[#<Task id: 4, created_at: "2019-06-12 12:50:53", updated_at: "2019-07-14 16:01:58", name: "name2">]
  Task Load (0.1ms)  SELECT  "tasks".* FROM "tasks" WHERE "tasks"."id" > ? ORDER BY "tasks"."id" ASC LIMIT ?  [["id", 4], ["LIMIT", 1]]
[#<Task id: 5, created_at: "2019-06-12 12:52:31", updated_at: "2019-06-12 12:52:31", name: "name1">]
  Task Load (0.1ms)  SELECT  "tasks".* FROM "tasks" WHERE "tasks"."id" > ? ORDER BY "tasks"."id" ASC LIMIT ?  [["id", 5], ["LIMIT", 1]]
=> nil

参考