遍历循环时,在循环内访问MySQL效率更高/更低? [英] When iterating through loops, is it more/less efficient to access a MySQL within loop?

查看:289
本文介绍了遍历循环时,在循环内访问MySQL效率更高/更低?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个PHP脚本来从Web上抓取数据.最终结果,我想将所有抓取的数据妥善保存并保存在mysql数据库中.

I'm writing a PHP script to scrape data from the web. End-result, I want to have all the scraped data tucked and formatted nicely in a mysql database.

但是由于PHP脚本中有多个循环和数组要迭代,所以我的直觉是为了提高速度和效率,最好不要保持对mysql数据库的循环访问(每次插入数据) -围绕循环)-而是将数据存储在PHP中的临时数组中,然后仅在脚本的末尾,将数组一次转储到mysql中.

But as there are multiple loops and arrays to iterate through within the PHP script, my gut feeling is that for speed and efficiency, it would be best NOT to keep loop-cycling access to the mysql database (inserting data on each go-around of the loops) -- instead, storing the data in temporary arrays within PHP, and then only at the end of the script, dumping the arrays into mysql in one go.

你怎么说?

推荐答案

是的,通常在查询数量方面是最小的.但是,您的脚本受网络io(下载速度)的限制,而不是数据库的限制.操作系统下载数据时,大多数时间您的脚本都处于睡眠状态.因此,您发出的任何查询都相对较少.

yes, generally being minimal when it comes to the number of querys is performant. But, your script is limited by network io(download speed), not the database. Your script will be asleep most of the time while the os downloads the data. Any queries you issue would be relatively infrequent because of that.

我的建议是使其正常工作.

my advice is to just get it working.

这篇关于遍历循环时,在循环内访问MySQL效率更高/更低?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆