SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Thread: Slow PHP

  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Slow PHP

    I'v got a problem someone might know how to solve.
    I have a rating system on my flash based homepage and use php to get the values from the database.
    The problem is that it takes a while until all the values been loaded, and the flash movie wont proceed until all tha value been loaded. I have cosulted with some flash pro:s and they said that I wont be able to solve this in flash.

    So my question is: Is there a way to speed up the process when the php gets the data from the database? Like use multiple php files which do the same thing or something else..?

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    What is slowing it all down?
    Getting the data from the database? Then you might try to optimize your queries.
    Sending the data from the server to the client? Don't know how to solve that, but maybe someone else does
    Another reason?

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah it's getting the data that takes time.
    I have a couple of hundred objekts in flash that all calls the value through this php file:

    Code PHP:
    <?php
    $table = "Rating_System";
     
    $name = $_POST['sendName'];
     
     
    $votes_sum = $_POST['sendValue'];
     
    //----------------Connect to database-------------------//
    $con = mysql_connect("host","user","pass");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    //-----------------Select  DB-------------------//
    mysql_select_db("db", $con);
     
    //-------------Calculate average------------//
    $result_avg= mysql_query("SELECT votes_sum/votes_total AS votes_avg FROM $table  WHERE id='$name' "); 
     
    $average=mysql_result($result_avg, 0);
    //---set decimals---//
    $average = number_format($average, 1);
     
    //echo $average;
    echo "average=$average";  
     
    mysql_close($con);
    ?>

  4. #4
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    701
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)
    1. Comment out the mysql stuff and just hard code a $average=27 or something. This will verify that mysql is indeed the culprit.

    2. Add the connect statement back in. This will check to see if connecting is taking an abnormally long time.

    3. From the mysql command prompt, issue a describe Rating_System; command and post the results. This will confirm that id is properly indexed. it's also a bit confusing that you have votes_sum and a votes_total columns in the table and another $votes_sum variable in your php code.

    4. Does this query end up being called hundreds of times per page? If so then moving the connection code to it's own file so it only gets called once will help.

  5. #5
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    The query seems simple and straight forward to me. My guess is, it's the hundreds of flash objects that try to access the DB that are slowing things down.
    All these hundreds of objects are making the call simultaniously?

  6. #6
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah, they all calls the script at the same time, but they only do it once.
    ahundiak, I didnt really followed you on those steps..

  7. #7
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Don't you have the possibility to call the script once, get all the data you need for all the flash objects, and then distribute the data to the objects client side? Don't know if that's possible, I don't know anything about flash, but it would sure speed things up a lot.

  8. #8
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hehe, yeah I'm not sure if thats is possible either. Well I guess I have to work around this somehow.. I was just hoping that there was a simple way to speed up the retrieving of data from an database.

  9. #9
    SitePoint Guru
    Join Date
    Jul 2005
    Location
    Orlando
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The simple way is to not use hundred's of queries when one query will do. Making a 100 connections to the database and issuing a 100 queries is horribly wasteful when you could do it all with one connection and one query.

  10. #10
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    701
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by lammspillning View Post
    yeah, they all calls the script at the same time, but they only do it once.
    ahundiak, I didnt really followed you on those steps..
    The first thing to do is to make sure that it really is a mysql issue. It would be a shame if you spent time optimizing the query only to find out that something else was slowing things up. The fact that you are generating "hundreds" of flash objects per page is a bit of a red flag.

    Ultimately your php is doing this:
    // query the database then
    echo "average=$average";

    So make a new php file and just give it one line:
    <?php echo "average=22"; ?>

    And see what kind of performance you get. If the page is still way too slow then you know the problem lies on the flash side of things and not the php side.

    Based on the results of this step you can then decide where your optimization efforts are best spent.


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
  •