减慢php页面的功能

时间:2012-03-23 04:55:42

标签: php function slowdown

我有一个问题,我正在编写一个脚本,它一切正常,直到突然它需要主页1.2分钟加载,在很多评论和取消注释后我发现以下功能使一切变慢:< / p>

function toFollow(){
    $sql = "SELECT id FROM tofollow WHERE enabled = '1'";
    if (!$result = mysql_query($sql)) {
        return 'A error occured: ' . mysql_error();
    }
    while ($row = mysql_fetch_assoc($result)) {
        $users[] = $row['id'];
    }
    return $users;
}

更新

我在运行的同一个脚本上发现了问题所在:

foreach(toFollow() as $user){
    $connection->post('friendships/destroy', array('user_id' => $user));
    $count++;
}

所以,我刚把它改成了:

$tofollow = toFollow();    
foreach($tofollow as $user){
    $connection->post('friendships/destroy', array('user_id' => $user));
    $count++;
}

它有效!! (我仍然不明白问题是什么)

谢谢大家!!

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

如果尚未存在索引,请在enabled上创建索引。

有关索引的更多信息,请访问:How does database indexing work?