Last Updated: February 25, 2016
·
602
· oswaldoferreira

Querying a big amount of Rails DB Objects "at once"

Let's suppose you want to make a bad ass query, like 1.000.000 User objects. The find_each method should make the work. It slices the query on 1000 by 1000 stacks, greatly reducing amount of memory consumption.

User.find_each do |user|
   user.eat_bacon
end

If you're interested on changing the query stack amount, you should use find_in_batches method

User.find_in_batches(start: 500, batch_size: 500) do |group|
    group.each { |user| user.eat_bacon }
end

You can control the starting point for the batch processing by supplying the :start option. This is especially useful if you 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 worker 2 handle from 10,000 and beyond (by setting the :start option on that worker).

More information: http://api.rubyonrails.org/