Which is more expensive? For loop or database call?

前端 未结 7 1239
我在风中等你
我在风中等你 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:47

    I think it depends upon what you are doing. There isn't enough information in your statement.

    The above answers are true, the less you access the database the better (usually). So you should try to do a specific operation in as few database calls as possible. The only exception to this, I would think, would be in cases where your application is faster than the database, perhaps in doing complex data transformations, or in using a very inefficient database.

    Usually, letting the database do data transformations in set form is usually faster than doing them programmatically using a cursor in a for-loop. If this isn't something your'e familiar doing, I suggest learning a bit more SQL or getting a good how-to book like SQL Cookbook (note: I'm not affiliated with O'Reilly, that book was just extremely helpful for me.)

提交回复
热议问题