batches.rb 3.2 KB
Newer Older
1

P
Pratik Naik 已提交
2
module ActiveRecord
X
Xavier Noria 已提交
3
  module Batches
4
    # Looping through a collection of records from the database
V
Vijay Dev 已提交
5 6
    # (using the +all+ method, for example) is very inefficient
    # since it will try to instantiate all the objects at once.
7
    #
V
Vijay Dev 已提交
8 9
    # In that case, batch processing methods allow you to work
    # with the records in batches, thereby greatly reducing memory consumption.
10
    #
O
Oscar Del Ben 已提交
11 12
    # The #find_each method uses #find_in_batches with a batch size of 1000 (or as
    # specified by the +:batch_size+ option).
P
Pratik Naik 已提交
13
    #
14 15 16
    #   Person.all.find_each do |person|
    #     person.do_awesome_stuff
    #   end
P
Pratik Naik 已提交
17 18 19 20 21
    #
    #   Person.where("age > 21").find_each do |person|
    #     person.party_all_night!
    #   end
    #
O
Oscar Del Ben 已提交
22
    #  You can also pass the +:start+ option to specify
23
    #  an offset to control the starting point.
P
Pratik Naik 已提交
24 25 26 27 28 29 30
    def find_each(options = {})
      find_in_batches(options) do |records|
        records.each { |record| yield record }
      end
    end

    # Yields each batch of records that was found by the find +options+ as
O
Oscar Del Ben 已提交
31
    # an array. The size of each batch is set by the +:batch_size+
P
Pratik Naik 已提交
32 33 34
    # option; the default is 1000.
    #
    # You can control the starting point for the batch processing by
O
Oscar Del Ben 已提交
35
    # supplying the +:start+ option. This is especially useful if you
P
Pratik Naik 已提交
36 37
    # want multiple workers dealing with the same processing queue. You can
    # make worker 1 handle all the records between id 0 and 10,000 and
O
Oscar Del Ben 已提交
38
    # worker 2 handle from 10,000 and beyond (by setting the +:start+
39
    # option on that worker).
P
Pratik Naik 已提交
40 41
    #
    # It's not possible to set the order. That is automatically set to
42 43 44
    # ascending on the primary key ("id ASC") to make the batch ordering
    # work. This also mean that this method only works with integer-based
    # primary keys. You can't set the limit either, that's used to control
45
    # the batch sizes.
P
Pratik Naik 已提交
46 47 48 49 50
    #
    #   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
51
    #
V
Vijay Dev 已提交
52
    #   # Let's process the next 2000 records
53 54 55
    #   Person.all.find_in_batches(start: 2000, batch_size: 2000) do |group|
    #     group.each { |person| person.party_all_night! }
    #   end
P
Pratik Naik 已提交
56
    def find_in_batches(options = {})
57 58
      options.assert_valid_keys(:start, :batch_size)

P
Pratik Naik 已提交
59 60
      relation = self

61
      unless arel.orders.blank? && arel.taken.blank?
A
Aaron Patterson 已提交
62 63
        ActiveRecord::Base.logger.warn("Scoped order and limit are ignored, it's forced to be batch order and batch size")
      end
64

65
      start = options.delete(:start).to_i
P
Pratik Naik 已提交
66 67
      batch_size = options.delete(:batch_size) || 1000

68
      relation = relation.reorder(batch_order).limit(batch_size)
J
Jon Leighton 已提交
69
      records = relation.where(table[primary_key].gteq(start)).to_a
P
Pratik Naik 已提交
70 71

      while records.any?
72
        records_size = records.size
73
        primary_key_offset = records.last.id
74

P
Pratik Naik 已提交
75 76
        yield records

77
        break if records_size < batch_size
78

79
        if primary_key_offset
80
          records = relation.where(table[primary_key].gt(primary_key_offset)).to_a
81 82 83
        else
          raise "Primary key not included in the custom select clause"
        end
P
Pratik Naik 已提交
84 85 86 87 88 89
      end
    end

    private

    def batch_order
90
      "#{quoted_table_name}.#{quoted_primary_key} ASC"
P
Pratik Naik 已提交
91 92
    end
  end
93
end