In general, which is more expensive? A double-nested for loop and one call to a database or a call to a database for each of N items in only one for loop?
Not looki
In general, anything done in memory (for loop) is faster than the same thing done over a network (database call). However:
for i = 1 to num_users
get user from database
end
will be slower than
get users 1 to num_users from database (in one query)
because it's the number of times you ask the database for something that really matters.