Are you looking for an explanation between the approach where you load in all users at once:
# Loads all users into memory simultaneously
@users = User.all
@users.each do |user|
# ...
end
Where you could load them individually for a smaller memory footprint?
@user_ids = User.connection.select_values("SELECT id FROM users")
@user_ids.each do |user_id|
user = User.find(user_id)
# ...
end
The second approach would be slower since it requires N+1 queries for N users, where the first loads them all with 1 query. However, you need to have sufficient memory for creating model instances for each and every User record at the same time. This is not a function of "DB memory", but of application memory.
For any application with a non-trivial number of users, you should use an approach where you load users either individually or in groups. You can do this using:
@user_ids.in_groups_of(10) do |user_ids|
User.find_all_by_id(user_ids).each do |user|
# ...
end
end
By tuning to use an appropriate grouping factor, you can balance between memory usage and performance.