Performance optimization time!



  • Argh, editor ate my post and I don't feel like retyping the whole thing.  It really hates safari.


    Short story: I was told to look at a slow server (Load average hovering between 50-55 constantly!).  I couldn't find the problem, went to work on another problem, and ran into the load problem.  It was caused by this running every time a page was viewed:

    <?php
    include './config.php';

    $conn = mysql_connect($host, $user, $password) or die("Could not Connect");
    $db = mysql_select_db($db) or die("Could not Select Database");

    $query = mysql_query("Select * from content where id>37");
    for($i=0; $i<mysql_num_rows($query);$i++)
    {
            $id = mysql_result($query, $i, 'id');
            $intro = trim(mysql_result($query, $i, 'intro'));
            $full = trim(mysql_result($query, $i, 'full'));
            $intro = str_replace("'", "\'", $intro);
            $full = str_replace("'", "\'", $full);
            $intro = str_replace('"', '"', $intro);
            $full = str_replace('"', '"', $full);
            $intro = str_replace("\n", "<br />", $intro);
            $full = str_replace("\n", "<br />", $full);
            echo $id . "<br />";
            echo $full;
            if($intro != "") $q = mysql_query("update content set intro='". $intro. "' where id='". $id. "'") or die('DANGER WILL ROBINSON in intro: ' . mysql_error());
            if($full != "") $q = mysql_query("update content set full='". $full. "' where id='". $id. "'") or die('DANGER WILL ROBINSON in full: ' . mysql_error());
    }
    mysql_close($conn);

    ?>



  • Oh, the pain, the pain.



  • Yeah, sure why not fix the db data, every singe page load, instead of not corrupting it in the first place?
     



  • NOTES: Server load very low (<10). We can take advantage of the free time by using it to optimise the database instead.


Log in to reply