简体   繁体   English

代码添加减慢了Page Down

[英]Code Addition is Slowing Page Down

My client ordered another addition to the script, but I can't figure out how to fix the slowdown? 我的客户订购了该脚本的另一个添加项,但是我不知道如何解决该问题? The table has about 50,000 rows. 该表大约有50,000行。

 while($stats = mysql_fetch_array($get_stats)) {
  if ($stats['ip'] == gethostbyaddr($stats['ip'])) { // new code
   $is_undef = "Yes";            // causing problems
  } else { $is_undef = "No"; }      // end new code

 echo "<tr><td>" . date("d M Y g:i a ", strtotime($stats['date'])) . "</td><td>" .
       $stats['ip'] . "</td><td>" .
          parse_url_domain($stats['ref_url']) . "</td><td>" .
             $is_undef . "</td></tr>";
 }

This is the query: 这是查询:

 $get_stats = mysql_query("SELECT * FROM visitors WHERE site='$_GET[site]' AND date >= '$start_date' AND date <= '$end_date' ");

I think that you might have an issue with "gethostbyaddr". 我认为您可能与“ gethostbyaddr”有关。 Looping over that 50k times is going to be REALLY slow. 循环超过50k次将非常慢。

Also, not that it is relevant to the question, but you might want to think about sql injection a little bit. 另外,这与问题无关,但是您可能需要考虑一下sql注入。 I hope that isn't the actual query that you are running. 我希望这不是您正在运行的实际查询。 If so, someone can simply drop your table. 如果是这样,有人可以简单地将您的桌子放下。

我建议您在将每个地址添加到数据库表之前进行此检查(即每次查看一次而不是每次查看数据50,000次!)

I guess gethostbyaddr() is slow because it fails to find a host for the IP address. 我猜gethostbyaddr()很慢,因为它找不到IP地址的主机。 What's the purpose of the following expression anyway? 以下表达式的目的是什么?

$stats['ip'] == gethostbyaddr($stats['ip'])

Assume $stats['ip'] is 127.0.0.1 and the corresponding host is localhost . 假设$stats['ip']127.0.0.1 ,对应的主机为localhost So you'd compare 127.0.0.1 (an IP address) to localhost (a host name). 因此,您需要将127.0.0.1 (一个IP地址)与localhost (一个主机名)进行比较。

from the comments at http://php.net/gethostbyaddr : http://php.net/gethostbyaddr的评论中:

"Just wanted to let everyone know that gethostbyaddr() takes more than 20 seconds to respond if the IP address is not listed in DNS." “只是想让所有人都知道,如果IP地址未在DNS中列出,gethostbyaddr()会花费20秒钟以上的时间做出响应。”

I'd say looking these hostnames up in a loop is probably slowing you down. 我想说的是,循环查找这些主机名可能会减慢您的速度。

also the variables you're sending to that query need to be properly escaped or you're really asking for trouble. 您发送给该查询的变量也需要正确地转义,否则您确实会遇到麻烦。

DNS requests can take quite a long time, especially across 50k records. DNS请求可能会花费很长时间,尤其是在5万条记录中。 If your client demands having the hostname instead of IP address for records, you might want to run some kind of background process to cache the hostnames instead of looking them up every page load. 如果您的客户端要求使用主机名而不是IP地址来进行记录,则可能需要运行某种后台进程来缓存主机名,而不是在每次页面加载时都查找主机名。

also, most ISPs use blocks of ip addresses, so you could start to build tables which track ip ranges and hostmasks for ISPs to cut out the DNS lookups 同样,大多数ISP使用IP地址块,因此您可以开始建立表来跟踪ISP的IP范围和主机掩码,以切断DNS查找

I think the gethostbyaddr() call is slowing you down. 我认为gethostbyaddr()调用使您减速。

See http://us3.php.net/manual/en/function.gethostbyaddr.php#88920 参见http://us3.php.net/manual/zh/function.gethostbyaddr.php#88920

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM