I'm trying to update weekly ranking scores for players, but any query I have tried just runs to timeout. There are around 100k rows in the table. My table players_weekly_rankings looks like this:
player_id | ranking_points | yearweek | ranking_pos
22 | 1676 | 2020/01 | 1
12 | 1620 | 2020/01 | 2
45 | 1620 | 2020/01 | 2
53 | 1544 | 2020/01 | 4
25 | 1644 | 2020/02 | 1
21 | 1555 | 2020/02 | 2
etc.
So the ranking_pos column is the one being updated.
And the query that never finishes and runs to timeout:
update players_weekly_ranking
set ranking_pos = (
select count(distinct ranking_points) + 1
from (SELECT ranking_points, yearweek FROM players_weekly_ranking) w2
where w2.yearweek = players_weekly_ranking.yearweek and w2.ranking_points > players_weekly_ranking.ranking_points
)
And the EXPLAIN as requested below (this test tebale has only 2000 records, but actual table near 100k)
With upto couple of thousand rows, it finishes in two minutes, but anything above that, timeout is reached.
Is there more optimized way to do this, so the query wouldn't run to timeout? Thanks!