Simple Rails Performance Tip – Active Record Batch Retrieval

Users.each do |user|
  puts user.name
end

Ever wondered about how expensive a simple “.each” on an ActiveRecord object collection is. This essentially means for every iteration of the loop, there is going to be a database fetch. Imagine looping over a collection that has 10,000 records (forget millions..). Yes, I have made use of this very same thing a bunch of times without realising the kind of performance implications this could have. Not until last week I discovered that there was a very less-expensive way to do this.

I was perusing the Rails guides casually looking for something else when I hit upon this. “.find_each”. What does this do? It makes looping over a collection of Active record objects much much cheaper by performing a batch retreival of the database records and caching them. The batch size can be altered by passing in a batch_size param like this “.find_each(:batch_size => 500”).

Users.find_each(:batch_size => 200) do |user|
  puts user.name
end

I have almost stopped using “.each” these days and let “.find_each” do the job.

Advertisements

One comment

  1. Pingback: Tweets that mention Simple Rails Performance Tip – Active Record Batch Retrieval « Intellisenze -- Topsy.com


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s