[Rails] each vs each_slice vs find_each vs find_in_batches vs in_batches

Tags
Rails
Engineering
Created
Oct 8, 2023 12:02 AM
Edited
Oct 7, 2023
Description
each vs each_slice vs find_each vs find_in_batches vs in_batches

each

When you call #each, all records will be loaded into memory.
  • Loop through an array
a = [ "a", "b", "c" ]
a.each {|x| print x, " -- " }

each_slice

  • Loop through an enumerable
  • Kind of like each but in batches
(1..10).each_slice(3) { |a| p a }
# outputs below
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
[10]

find_each

  • find_each loads batches (1000 by default) and yield single record in loop
    • Each record is just an ActiveRecord
  • ActiveRecord method
  • Basically, find_each = find_in_batches + each
  • Used when too many records that the client can’t load or process
When you call #find_each, records will be loaded into memory in batches of the given batch size.
Person.find_each(:conditions => "age > 21") do |person|
  person.party_all_night!
end

find_in_batches

  • find_in_batches loads batches and yield batches in loop
    • Each batch is an array
  • Used when too many records that the client can’t load or process
Person.where("age > 21").find_in_batches do |group|
  sleep(50) # Make sure it doesn't get too crowded in there!
  group.each { |person| person.party_all_night! }
end

in_batches

  • Yield ActiveRecord::Relation
  • Might be good if you need to process the entire batch at once but lack of memory
Person.where("age > 21").in_batches do |relation|
  relation.delete_all
  sleep(10) # Throttle the delete queries
end

Source