我如何优化此代码?

时间:2012-11-02 02:34:19

标签: php mysql optimization

好的基本上我设置了一个查询来连接两个表并获取所有信息,然后我使用一些循环来显示我的网页的某个部分。缓慢的例子在这里:http://sage-scape.com/hiscores/compare.ws?user1=kyle 现在我的代码有点大但可能是问题,我将两个函数组合在一起因为我认为这会加快这个过程,所以它没有太大帮助。以下是代码的编写方式:

    function compare($user1) {
    include "mysql.ws";
    $user1 = str_replace(" ", "_", $user1);
    $query = mysql_query("SELECT a.*, b.* FROM skills a JOIN activities b ON(a.playerName LIKE b.playerName) WHERE a.playerName LIKE '".fixName($user1)."' LIMIT 1") or die(mysql_error());

    $row = mysql_fetch_array($query);
    $overall['lvl'] = 0;
    $overall['xp'] = 0;
    for ($i = 1; $i <=25; $i++){
        $xptype = findType($i)."xp";  
        $overall["lvl"] += getLevelForXP($row[$xptype]);
        $overall["xp"] += $row[$xptype];
    }
        //updateDonator($user1);

    echo'<div class="OrnamentalBoxBg"><div class="OrnamentalBoxContent">
        <div class="header">
        <div class="left">
        <span class="columnLevel">Level</span>
        <span class="columnTotal">Total XP</span>
        <span class="columnRank">Rank</span>
        </div></div>
        <div class="row" data-skill="combat" data-member="false">
        <div class="left">
        <a href="http://sage-scape.com/hiscores/overall.ws?category_type=0&amp;table=0&amp;rank='.findRank($user1,"0").'#'.findRank($user1,"0").'">
        <span class="columnLevel">
        '.dots($overall["lvl"]).'
        </span>
        </a>
        <a href="http://sage-scape.com/hiscores/overall.ws?category_type=0&amp;table=0&amp;rank='.findRank($user1,"0").'#'.findRank($user1,"0").'">
        <span class="columnTotal">
        '.dots($overall["xp"]).'
        </span>
        </a>
        <a href="http://sage-scape.com/hiscores/overall.ws?category_type=0&amp;table=0&amp;rank='.findRank($user1,"0").'#'.findRank($user1,"0").'">
        <span class="columnRank">
        '.findRank($user1,"0").'
        </span>
        </a>
        </div>
        <div class="middle">
        <img src="http://sage-scape.com/hiscores/img/overall.png?2" alt="" title="">
        <span class="statName"><span><span><span>Overall</span></span></span></span>
        </div>
        </div>
    ';
    for ($i = 1; $i <=25; $i++){
        $xptype = findType($i)."xp";  
        if($i == 25){
            echo'
                <div class="row last-row" data-skill="'.findSkill($i).'" data-member="'.member($i).'"> ';

        } else {
            echo'
                <div class="row" data-skill="'.findSkill($i).'" data-member="'.member($i).'"> ';
        }
        echo'
            <div class="left">
            <a href="http://sage-scape.com/hiscores/overall.ws?category_type=0&amp;table='.$i.'&amp;rank='.findRank($user1,"$i").'#'.findRank($user1,"$i").'">
            <span class="columnLevel">';
            if($i == 25){
                echo'
                    '.$row["Dungeoneeringlvl"].'
                    </span>';
            }else{
                echo'
                    '.getLevelForXP($row[$xptype],"$i").'
                    </span>';
            }
            echo'
            </a>
            <a href="http://sage-scape.com/hiscores/overall.ws?category_type=0&amp;table='.$i.'&amp;rank='.findRank($user1,"$i").'#'.findRank($user1,"$i").'">
            <span class="columnTotal">
            '.dots($row["$xptype"],"$i").'
            </span>
            </a>
            <a href="http://sage-scape.com/hiscores/overall.ws?category_type=0&amp;table='.$i.'&amp;rank='.findRank($user1,"$i").'#'.findRank($user1,"$i").'">
            <span class="columnRank">
            '.findRank($user1,"$i").'
            </span>
            </a>
            </div>
            <div class="middle">
            <img src="http://sage-scape.com/hiscores/img/xp_'.fixName(findType($i)).'_total.png?2" alt="" title="" />
            <span class="statName"><span><span><span>'.findType($i).'</span></span></span></span>
            </div>
            </div>
        ';
    }
    echo'
    </div></div>
<div class="OrnamentalBoxBottom"></div>
</div>
<div class="OrnamentalBox Width923 SegmentTwo" id="activityStats">
<div class="OrnamentalBoxTop"></div>
<div class="OrnamentalBoxTitle"><div class="Centre1"><div class="Centre2">
<h3 class="Gradient NoFlourish Centre"><span class="spacing" aria-hidden="true">Activity Stats</span>
<span class="G0">Activity Stats</span>
<span class="G1" aria-hidden="true">Activity Stats</span>
<span class="G2" aria-hidden="true">Activity Stats</span>
<span class="G3" aria-hidden="true">Activity Stats</span>
<span class="G4" aria-hidden="true">Activity Stats</span>
<span class="G5" aria-hidden="true">Activity Stats</span>
<span class="G6" aria-hidden="true">Activity Stats</span>
</h3></div></div></div>
<div class="FlatHeader playerNames">
<div class="left">
'.BBCode($user1).'</div>';
    if(!empty($_GET["user2"])){ 
        echo'
            <div class="right">
            '.BBCode($_GET["user2"]).'
            </div>
            ';
    }
echo'
</div>
<div class="playerAvatars">
<div class="left">
<img src="http://services.runescape.com/m=avatar-rs/'.$user11.'/chat.png?cachebust=1342804665991" alt="" title="">
</div>';

if(!empty($_GET["user2"])){ 
    echo'
        <div class="right">
        <img src="http://services.runescape.com/m=avatar-rs/'.$_GET["user2"].'/chat.png?cachebust=1342804665991" alt="" title="">
        </div>
            ';
}
echo'
</div>';
    echo'
        <div class="OrnamentalBoxBg"><div class="OrnamentalBoxContent">
        <div class="header">
        <div class="left">
        <span class="columnScore">Score</span>
        <span class="columnRank">Rank</span>
        </div>
        </div>';
    for ($i = 0; $i < 7; $i++){
        $xptype = findActivityP($i);  
        if($i == 6){
            echo'
                <div class="row last-row">';
        } else {
            echo'
                <div class="row">';
        }
        echo'
        <div class="left">
        <a href="http://sage-scape.com/hiscores/overall.ws?category_type=1&amp;table='.$i.'&amp;rank='.findARank($user1,"$i").'#'.findARank($user1,"$i").'">
        <span class="columnScore">
        '.$row["$xptype"].'
        </span>
        </a>
        <a href="http://sage-scape.com/hiscores/overall.ws?category_type=1&amp;table='.$i.'&amp;rank='.findARank($user1,"$i").'#'.findARank($user1,"$i").'">
        <span class="columnRank">
        '.findARank($user1,"$i").'
        </span>
        </a>
        </div>
        <div class="middle">
        <img src="http://sage-scape.com/hiscores/img/'.findActivityP($i).'.png" alt="" title="">
        <span class="statName"><span><span><span>'.findActivity($i).'</span></span></span></span>
        </div>
        </div>
        ';
    }

}

技能表包含51列playerName和25种不同的xp和lvl技能。活动包含8列用户和一些活动。我有几个循环,所以代码不是太大,但有人可以帮我优化这个,所以它会加载更快?

1 个答案:

答案 0 :(得分:1)

我的第一个想法是:衡量措施措施!!!

如果没有测量各个部分,您不知道哪个部分很慢。先尝试一下。

一般而言,您获取一些数据,然后搜索以显示适当的值。当你知道什么是缓慢的,如果它是数据量

  1. 考虑存储那些被查找的值(缓存)
  2. 您是否可以使用其他资源(例如数据库)进行查找并返回刚刚渲染的原始平面数据?
  3. 您是仅获取所需数据还是仅将数据库加载到内存中以便再将其丢弃?
  4. 说真的 - 做一些剖析。