-2

I have a problem, im working on a script and it was all going fine until suddenly it takes the main page 1.2 min to load, after A LOT of commenting and uncommenting i found out the following function was making everything slow:

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;
}

UPDATE:

I found out what the problem was, on the same script i run:

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

So, I just changed it to:

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

And it works!! (I still dont understand what the problem was)

Thanks everyone!!

Any suggestions?

bx3
  • 13
  • 3
  • **there is absolutely nothing wrong with this function itself** You have to provide at least information on what is the size of the table tofollow and $users array? – Your Common Sense Mar 23 '12 at 04:59
  • 2
    The problem was that you were calling the toFollow() function on each iteration of the foreach loop. – sikander Mar 23 '12 at 05:22

1 Answers1

0

Create an index on enabled if one doesn't exist yet.

More information about indexes here: How does database indexing work?

Community
  • 1
  • 1
sikander
  • 2,286
  • 16
  • 23
  • 1
    Indexes actually help speed up data retrieval significantly so this was not a bad suggestion given the little information the user had originally posted. Anyways... – sikander Mar 23 '12 at 05:23