In Rails 3.2, like "pluck_in_batches" for a very large table

I have a massive Foo table, from which I need to pull out all the values ​​in a specific Foo.who field.

The array has millions of rows, but only a few thousand different values ​​in the column who.

If the table were smaller, I would just use Foo.pluck(:who)

If I use Foo.find_in_batches do |a_batch|, each set is an array of Foo records, not a collection of activerecord records of Foo records, so I can't use .pluck()AFAIK either, the only way to extract a column whois through something like .map(&:who)that iterates through the array.

Is there a way to rip a column whoout of a Foo package in packages that do not require repeating over each element of each batch to retrieve the column who?

+5
source share
2 answers

Try the following:

Foo.select(:id, :who).find_in_batches do |a_batch|
  ...
end
+2
source

In Rails 5, you can use:

Foo.in_batches do |relation|
  values = relation.pluck(:id, :name, description)
  ...
end

Upd: to prevent memory leaks, use:

Foo.uncached do
  Foo.in_batches do |relation|
    values = relation.pluck(:id, :name, description)
    ...
  end
end
0
source

All Articles