使用select子查询的更新查询在mysql 5.7上运行极慢,但在5.5上运行超快

时间:2018-12-26 20:10:20

标签: php mysql sql mysql-5.7 mysql-5.5

我有一个数字海洋测试服务器,该服务器具有ubuntu 14.04和mysql 5.5版。另一方面,我的生产服务器是ubuntu 16.04,而mysql版本是5.7。我有这个更新查询

UPDATE paid_cricduel.fantasy_teams t 
join (select ft.fantasy_team_id, 
             ft.team_captain_id, 
             sum((case 
               when ft.team_captain_id = ccp.cricket_contest_player_id then 2
               when ft.team_vc_id = ccp.cricket_contest_player_id then 1.5
               else 1
             end) *  ccp.player_points) as temp_total 
from fantasy_teams as ft 
join fantasy_team_players ftp on ft.fantasy_team_id=ftp.fantasy_team_id
join cricket_contest_players as ccp on   ftp.cricket_contest_player_id = ccp.cricket_contest_player_id 
where ft.cricket_contest_id=? 
group by ft.fantasy_team_id) temp 
set total_points = temp.temp_total
where temp.fantasy_team_id = t.fantasy_team_id

此查询在我的测试服务器中运行的时间为0.3秒,但在我的生产环境中至少需要25秒。有时甚至超过50秒。我已经尝试过在堆栈溢出中提到的其他解决方案,但是似乎没有任何效果。

查询的作用是从另一个表中获取球员得分,然后从另一个表中选择团队球员。对您的球员的所有点求和。另外,如果该球员是您的队长,则该分数乘以1.5;如果是副队长,则该分数乘以1.2,否则就按原样添加他们。

我在total_points列和fantasy_team_id上也有索引,但是我删除了它们以查看是否会产生影响,但没有。

有帮助吗?

0 个答案:

没有答案