开发者

How Can I Optimize these queries?

I have 2 areas that I know are process whores but I don't know how to better them.

The 2 places in question are shown here

http://i.imgur.com/Kb5Bv.png

And the corresponding queries

Area 1

$nresult = mysql_query("SELECT time
                       FROM log
                       ORDER BY time
                       LIMIT 1") or die(mysql_error);

$nr = mysql_fetch_assoc($nresult);

$aresult = mysql_query("SELECT *
                       FROM log") or die(mysql_error);

$an = mysql_num_rows($aresult);

$aresult = mysql_query("SELECT AVG(players)
                       FROM log") or die(mysql_error);

$ap = mysql_fetch_assoc($aresult);

$average = round($an/((time()-$nr['time'])/60/60), 2);

echo "<p class=\"tf2\"><span>{$an}</span> items since " . date("F j, Y", $nr['time']) . " at " . date("g:i a", $nr['time']) . ".<br /><span>" . $average . "</span> items received per hour (<span>" . round($average / $ap['AVG(players)'], 2) . "</span>/player/hour).<br />Showing <span>{$_SESSION['limit']}</span> items per page.</p>";

Area 2

if (!empty($_GET['s']) && !empty($_GET['t']) && !empty($_GET['m'])&& !empty($_GET['q'])) {

    if ($_GET['m'] != 'all') {

        $m = "AND method = {$_GET['m']}";
    }
    else {

        $m = '';
    }

    if ($_GET['q'] != 'all') {

        $q = "AND quality_id = {$_GET['q']}";
    }
    else {

        $q = '';
    }

    if ($_GET['s'] == ' ') {

        $s = '1=1';
    }
    else {

        $s = "{$_GET['t']} LIKE '%" . mysql_real_escape_string($_GET['s']) . "%'";
    }

    $num_result = mysql_query("SELECT *
                              FROM log
                              LEFT JOIN item_definition
                              on log.item = item_definition.item_definition_desc
                         开发者_运维百科     LEFT JOIN method
                              on log.method = method.method_id
                              LEFT JOIN item
                              on item_definition.item_definition_id = item.item_id
                              LEFT JOIN server
                              on log.server = server.ip
                              LEFT JOIN quality
                              on log.quality = quality.quality_id
                              WHERE {$s}
                              {$m}
                              {$q}")
                              or die(mysql_error());
}
else {

    $num_result = mysql_query("SELECT *
                              FROM log") or die (mysql_error());
}

$total = mysql_num_rows($num_result);

echo "<center>" . $total . " results</center><br />";

The live page is here http://www.tf2items.711clan.net/

As you can see, it takes a good amount of time for it to load.

Any tips are appreciated.


I don't see any LIMIT/OFFSET commands.. maybe this gives you a boost if you use paging in your shop. Or you could also try to cache it somewhere (if the search filters don't change)..


Replace the second query with SELECT COUNT(*) FROM log, since you are getting the row count and ignoring the row data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜