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/
Written by Oswaldo Ferreira
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Rails
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#