Efficient way of looping through a large amount of users? Hi I have a plugin that loops through users to find out their interests. Counts the number of interests in total for each interest and displays this in a widget on the dashboard with the interest name and amount of users with that interest. The code I have works but fails with a max execution time and only pulls out minimal results for each interest. Is there a way to do this more efficiently so the script does not timeout. This is what I have so far but I'm sure looping through 20k+ users and checking each interest field to see if the category id is in it is highly inefficient. Cheers.

public function getInterestRank()
{
    $criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Category);
    $criteria-&gt;group = 'interestTags';
    $category = $criteria-&gt;find();

    //Get all users
    $user_criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::User);
    $users = $user_criteria-&gt;find();

    $interestArray = array();

    foreach($category as $interest)
    {
        $title = $interest['title'];
        $count = 0;

        foreach($users as $user)
        {
            if($user['interests']) {
                if(in_array($interest['id'], $user['interests']))
                {
                    $count = $count+1;
                }
            }
        }
        $interestArray[$title] = $count;
    }

    arsort($interestArray);
    return ($interestArray);
}
You could just setup a criteria model in your “categories” for-loop, prepared to query for the users related to that category. And then use the total() method to get the count.

foreach($category as $interest) {
    $user_criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::User);
    $user_criteria-&gt;relatedTo = $interest-&gt;id;
    $count = $user_criteria-&gt;total();

    $interestArray[$interest-&gt;title] = $count;
}


Using the total() method is a lot faster than find(), the underlaying SQL which is necessary to get the amount of rows performs better and Craft also doesn’t have to populate the Entry models with all the data.

(Not sure why @LukePearce deleted his answer, where he basically suggested to do the same thing.)