Which is more expensive? For loop or database call?

前端 未结 7 1220
我在风中等你
我在风中等你 2020-12-29 05:13

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

7条回答
  •  [愿得一人]
    2020-12-29 05:35

    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.

提交回复
热议问题