Slow foreach loop
I am having a trouble with foreach loop and MySQL queries. The loop is extremely slow while pulling out the data from MySQL database.
Here is the example code:
As you can see, I have class Report that uses class MySQL for handling mysql things like queries etc, and then I am using that class Report inside foreach loop to pull out data. The problem is that it takes about 50 seconds to get data for 10 users with some statistics. I have to say that the database itself is huge, so I understand it might be the issue, but after trying some basic debugging, I found out that the slowdown is happening inside the foreach loop, because that's where it hangs every time it gets executed. So I am wondering if there is a better way of doing this, because I am using foreach loop, while loop inside functions, and all together its very slow.
$this->db->query = "SELECT id,Name FROM Users WHERE Creator=12 AND id!=22 ORDER BY Name ASC";
while($row = $this->db->FetchArray())
$users = new Report();
foreach ($users->getUsers() as $row)
$id = $row['id'];
$number_of_new_tickets = $users->getNumbers($id,"new","$dates$dates%");
Any help appreciated.
What is your $users->getNumbers() do? Could that be the slow down within your foreach loop?
Have you tested the foreach loop with just regular data and see if there is a slow down without calling to another method or function inside the loop?