Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 查询会减慢网站的运行速度,并且必须使它们执行得更快_Php_Sql_Wordpress_Performance - Fatal编程技术网

Php 查询会减慢网站的运行速度,并且必须使它们执行得更快

Php 查询会减慢网站的运行速度,并且必须使它们执行得更快,php,sql,wordpress,performance,Php,Sql,Wordpress,Performance,我在wordpress中有两个查询,大大减慢了我的网站速度。第一个是 global $wpdb; $tbl = $wpdb->prefix . "my_tournament_matches_events"; $result = $wpdb->get_var("SELECT count(*) FROM $tbl WHERE match_id = '$match' AND player_id = '$player' AND event_id = 'app'");

我在wordpress中有两个查询,大大减慢了我的网站速度。第一个是

   global $wpdb;
    $tbl = $wpdb->prefix . "my_tournament_matches_events";  
    $result = $wpdb->get_var("SELECT count(*) FROM $tbl WHERE match_id = '$match' AND player_id = '$player' AND event_id = 'app'");
    if($result) { return 'checked="checked"'; 
问题是,它会检查每一个球员(0.33秒)是否参加了比赛,这意味着每个队有20个查询(6.6秒,两个队=13,2秒)。我想知道我是否可以只通过两个查询(每个团队一个)就完成

第二个问题是

    $output.="<td colspan='2'>".my_fetch_data_from_id($homeplayer->player_id,'data_value')."</td>";
    foreach($events as $event){
        $evcount = $wpdb->get_results("SELECT count(*) as total_events 
                                       FROM $table2 
                                       WHERE match_id='$mid' 
                                       AND player_id='$homeplayer->player_id' 
                                       AND event_id='$event->id'" );
        $total= $evcount[0]->total_events;

        $output.="<td><input type='text' name=hm-$homeplayer->player_id-$event->id value='$total'/></td>";
     }
$output.=''.my_-fetch_-data_-from_-id($homeplayer->player_-id,'data_-value');
foreach($events作为$event){
$evcount=$wpdb->获取_结果(“选择计数(*)作为总_事件
从表2美元起
其中match_id=“$mid”
玩家id=“$homeplayer->玩家id”
事件_id='$event->id');
$total=$evcount[0]->事件总数;
$output.=“玩家id-$event->id值='$total'/>”;
}

在第二个查询中,它检查每个玩家,但由于我有大约11个事件,这意味着它执行11events*20players*2teams=440个查询。是否可以修改代码以减少查询数量和查询时间?第二次查询的平均时间约为0,3秒,因此每项改进都是显而易见的

我建议在数据库中添加两个索引。(和ofc检查是否存在)

这将大大加快速度。0.33s是一个缓慢的查询响应时间。

总之:

  • 限制1
    添加到查询中,这将确保如果查询找到结果,它不会继续查找其他结果
  • 避免在
    count
    中使用
    *
    ,而只在必填字段中计数
  • 尽可能在列中添加索引
对于可读性、一致性和某些错误,您可以更改单引号和双引号:

$output.='<td colspan="2">'.my_fetch_data_from_id( $homeplayer->player_id, 'data_value' ).'</td>';
$output.='<td><input type="text" name="hm-'.$homeplayer->player_id-$event->id.'"value="'.$total.'"/></td>';
              // Seems odd here otherwise ^
$output.=''.my_fetch_data_from_id($homeplayer->player_id,'data_value');
$output.='';
//否则在这里似乎很奇怪^

对于第一个查询,您可以将限制设置为1,这意味着如果发现结果,它将立即停止。您应该首先在SQL查询中转义变量(例如,
$match
)。避免使用
*
,而是使用列名。这里的完整场景是什么?玩家是否登录到游戏?你们如何确定一个玩家是否“在游戏中”?如果我先选择他参加,他是否在游戏中。然后,查询将一个名为app的事件添加到基中,而其他列将是players id,match id。您如何知道它们当前有哪些索引?如果查询执行的时间那么长,则很可能至少有一个表没有索引确保它是“可能的”,但这仍然是一个猜测,只需先询问注释,然后回答即可。您在一个您根本不知道的表上提供了
alter table
命令,这就是为什么我建议首先检查索引是否存在的原因。我们没有索引,因为表中的数据经常被移动到另一个数据库中,这不可能实现相同的功能
$output.='<td colspan="2">'.my_fetch_data_from_id( $homeplayer->player_id, 'data_value' ).'</td>';
$output.='<td><input type="text" name="hm-'.$homeplayer->player_id-$event->id.'"value="'.$total.'"/></td>';
              // Seems odd here otherwise ^