Method: OccamsRecord::Batches::CursorHelpers#find_in_batches_with_cursor

Defined in:
lib/occams-record/batches/cursor_helpers.rb

#find_in_batches_with_cursor(batch_size: 1000, use_transaction: true) {|OccamsRecord::Results::Row| ... } ⇒ Enumerator

Loads records in batches of N and yields each batch to a block (if given). If no block is given, returns an Enumerator.

NOTE Unlike find_in_batches, batches are loaded using a cursor, which offers better performance. Postgres only. See the docs for OccamsRecord::Cursor for more details.

Parameters:

  • batch_size (Integer) (defaults to: 1000)

    fetch this many rows at once

  • use_transaction (Boolean) (defaults to: true)

    Ensure it runs inside of a database transaction

Yields:

Returns:

  • (Enumerator)

    will yield each record



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/occams-record/batches/cursor_helpers.rb', line 43

def find_in_batches_with_cursor(batch_size: 1000, use_transaction: true)
  enum = Enumerator.new { |y|
    cursor.open(use_transaction: use_transaction) { |c|
      c.each_batch(batch_size: batch_size) { |batch|
        y.yield batch
      }
    }
  } 
  if block_given?
    enum.each { |batch| yield batch }
  else
    enum
  end
end