Simple Rails Performance Tip – ActiveRecord Batch Retrieval
Users.each do |user|puts user.nameend
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.nameend
I have almost stopped using “.each” these days and let “.find_each” do the job.







