find_each, not all


This great presentation at RailsConf 2012 by James Edward Grey II pointed out something I hadn’t taken into account. This:

User.all do |user| # do something... end

is bad mojo for potential large tables because it will attempt to load the entire table into memory. A better habit to get into is:

User.find_each do |user| # do something... end

This will have the same net result, but it will only pull 1000 records at a time into memory.