我使用mysqlDB在xampp下运行脚本。
我检查域名是否有ip。
问题是,我必须检查来自MySQL_DB的100000多个域名。
函数"gethostbyname“运行良好,但我的解决方案太慢了。
while($row = mysqli_fetch_array($db_res)) { // get the DB domainnames entrys
if (empty($row['status'])) {
$items[] = $row['domainnames'];
}
foreach ($items AS $domain) {
if ( gethostbyname($domain) != $domain ) {
do somthing.....
}
}
}我怎样才能更快地得到它?
发布于 2019-04-14 20:01:31
看起来当你的while循环迭代时,它使用上一次迭代中的$items --这将会浪费时间--所以请尝试这个版本(将foreach放入if:
while($row = mysqli_fetch_array($db_res)) { // get the DB domainnames entrys
if (empty($row['status'])) {
$items[] = $row['domainnames'];
foreach ($items AS $domain) {
if ( gethostbyname($domain) != $domain ) {
do somthing.....
}
}
}
}发布于 2019-04-14 20:21:47
在while()循环中使用foreach()循环根本不是一个好主意。想想看。当您迭代结果集时,$items会不断膨胀--这意味着foreach()将不得不越来越长地工作。
最后,如果需要为脚本中的下一个任务处理gethostbyname()值,则应该在第一次向表中插入条目的同时存储该值--也许新列可以为host。
明智的做法不是调用gethostbyname() 100000次;在选择它时要准备好它的值。
除了上面的逻辑之外,我不认为有必要声明一个具有单个元素/字符串的数组,然后迭代它。
实际上,您的查询应该包含一个WHERE子句,该子句排除具有null/0/blank status值的行,并包括具有与$domain匹配的host (新列)值的行,这样php就不必担心任何限定/取消条件。
foreach ($db_res as $row) { // yes, you can simply iterate the result object
// do whatever with the associative string elements (e.g. $row['domainnames'])
// ...you know this is a string and not an array, right? -^^^^^^^^^^^^^^^^^^^
}发布于 2019-04-14 21:26:25
谢谢你的回答。在你的帮助下,我能够将程序减少到:
while($row = mysqli_fetch_array($db_res))
{
$domain = $row['domainnames'];
if ( gethostbyname($domain) != $domain ) {
do somthing.....;
}
else{
do somthing.....;
}
}感觉有点快,但还不够。@mickmackusa我现在只捕获空的"status“字段:
$db_res = mysqli_query ($db_link, "select domainnames FROM domaintable WHERE status = ''")https://stackoverflow.com/questions/55674893
复制相似问题