查询时间太长了

问题描述 投票:0回答:1

我有以下代码,需要25秒才能在屏幕上显示数据。

有什么办法可以让它与 isset 而不是 in_array ?

我认为这可以快很多!

if (!$matches[0]['match_id']) $matches = array();

for ($i=0; $i<count($matches); $i++) {

    if (in_array($matches[$i]['match_id'],$validMatches)) {

    $match_stats = $db->get_by_fields('player_match_stats', array('match_id'=>$matches[$i]['match_id'], 'player_id'=>$pid));
        if($match_stats['points']!='') $ret[0]++;
        $ret[1] = $ret[1] + $match_stats['points'];
        $ret[2] = $ret[2] + $match_stats['threepoints'];
    }
}

if ($ret[0] != 0) {
    $ret[3] = $ret[1] / $ret[0];
    $ret[3] = number_format($ret[3], 1);
}

return $ret;
php mysql
1个回答
0
投票

我的一些注意点。

  1. 不要用counter来代替in_array for 循环。它每次迭代都会计算长度。如果你有数组,可以用foreach代替。

  2. 使用 array_intersect 检索出所有有效的匹配信息,从 matches 数组之前的循环。

  3. 将你的循环移到查询之外,并使用 WHERE id IN (12,13,15,16,...,77). 然后迭代你的结果。

© www.soinside.com 2019 - 2024. All rights reserved.