哪个更贵?对于循环还是数据库调用? [英] Which is more expensive? For loop or database call?
本文介绍了哪个更贵?对于循环还是数据库调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
通常,哪个更贵?双重嵌套的for循环和仅在一个for循环中对N个项目中的每一个都调用数据库或对数据库的调用?
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 looking for an answer down to microseconds, just a general idea of which direction I should take.
TIA。
推荐答案
通常,您访问数据库的次数越少越好。造成这种情况的原因很多,包括:
In general, the fewer times you hit the database, the better. There are a number of reasons for this, including:
- 如果数据库可以尝试从中获取所有内容,则将能够更好地进行优化。一次
- 您消除了多次与数据库通信的所有开销,这可能是非常重要的
这篇关于哪个更贵?对于循环还是数据库调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文