2

我有一个纸牌游戏,用户根据他们赢得的游戏数量排名。总体评级是预先计算的,以便能够快速加载,但我在排名计算方面存在问题。

排名计算如下:

SET @userRank = 0;

UPDATE t_ratings AS r
JOIN
  (
    SELECT
        userId, (@userRank := @userRank + 1) as rank
        FROM (
        SELECT
            r.userId,
            r.solvedCount,
            r.playedCount
       FROM
        t_ratings AS r                        
    ORDER BY r.solvedCount DESC , r.playedCount ASC) AS t
  ) AS rt
  ON rt.userId = r.userId
SET r.rank = rt.rank

但最近我有时开始收到以下错误:

Deadlock found when trying to get lock; try restarting transaction

因此我想知道是否有更好的方法来计算用户等级以避免死锁?

4

1 回答 1

1

这就是我在 SQL Server 中的做法......不确定 mySQL 是否具有窗口函数,因为我不是 mySQL 人。

With    cte As
(
        Select  Rank() Over (ORDER BY r.solvedCount DESC , r.playedCount ASC) As [rank], 
                userID
        From    t_ratings As r
)
Update  tr
Set     [rank] = c.[rank]
From    t_ratings tr
Join    cte c
        On  tr.userID = c.userID
于 2012-12-20T02:54:49.487 回答