Skip to content

Commit

Permalink
Adds to Batch processing documentation [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
alessapereyra committed May 28, 2012
1 parent 21fee1b commit 2bef137
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions activerecord/lib/active_record/relation/batches.rb
Expand Up @@ -2,20 +2,28 @@


module ActiveRecord module ActiveRecord
module Batches module Batches
# Yields each record that was found by the find +options+. The find is # Looping through a collection of records from the database
# performed by find_in_batches with a batch size of 1000 (or as # (using the +all+ method, for example) could be too straneous to your
# memory if you have large amounts of them since it will try
# to instantiate all the objects of it at once.
#
# If that's the case, batch processing methods allow you to still work
# with all the records found by the find +options+ but using groups of
# a batch size (defaulting to 1000) at a time, greatly reducing the use of memory.
#
# The find_each method performs by using +find_in_batches+ with a batch size of 1000 (or as
# specified by the <tt>:batch_size</tt> option). # specified by the <tt>:batch_size</tt> option).
# #
# Example: # Person.all.find_each do |person|
# person.do_awesome_stuff
# end
# #
# Person.where("age > 21").find_each do |person| # Person.where("age > 21").find_each do |person|
# person.party_all_night! # person.party_all_night!
# end # end
# #
# Note: This method is only intended to use for batch processing of # If needed, you can also send the <tt>:start</tt> option to specify
# large amounts of records that wouldn't fit in memory all at once. If # an offset to control the starting point.
# you just need to loop over less than 1000 records, it's probably
# better just to use the regular find methods.
def find_each(options = {}) def find_each(options = {})
find_in_batches(options) do |records| find_in_batches(options) do |records|
records.each { |record| yield record } records.each { |record| yield record }
Expand All @@ -39,12 +47,15 @@ def find_each(options = {})
# primary keys. You can't set the limit either, that's used to control # primary keys. You can't set the limit either, that's used to control
# the batch sizes. # the batch sizes.
# #
# Example:
#
# Person.where("age > 21").find_in_batches do |group| # Person.where("age > 21").find_in_batches do |group|
# sleep(50) # Make sure it doesn't get too crowded in there! # sleep(50) # Make sure it doesn't get too crowded in there!
# group.each { |person| person.party_all_night! } # group.each { |person| person.party_all_night! }
# end # end
#
# # Let's process the next 2000 party guys
# Person.all.find_in_batches(start: 2000, batch_size: 2000) do |group|
# group.each { |person| person.party_all_night! }
# end
def find_in_batches(options = {}) def find_in_batches(options = {})
options.assert_valid_keys(:start, :batch_size) options.assert_valid_keys(:start, :batch_size)


Expand Down

0 comments on commit 2bef137

Please sign in to comment.