SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jul 2005
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Slow foreach loop

    Hi,

    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:

    PHP Code:
    class Report
    {
    ........
    function 
    getUsers()
    {
        
    $this->db->query "SELECT id,Name FROM Users WHERE Creator=12 AND id!=22 ORDER BY Name ASC";
        
    $this->db->Query();
        
    $result=array();
        while(
    $row $this->db->FetchArray()) 
        {
            
    $result[]=$row;
        }
        return 
    $result;
    }
    ...........
    }

    $users = new Report();
    foreach (
    $users->getUsers() as $row)
    {
        
    $id $row['id'];
        
    $number_of_new_tickets $users->getNumbers($id,"new","$dates[0]$dates[1]%");
        .....................

    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.

    Any help appreciated.

  2. #2
    SitePoint Addict
    Join Date
    Sep 2002
    Posts
    225
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •