为什么这个查询在限制为一个用户ID时会变慢,但如果没有那么快?

时间:2016-06-23 20:19:38

标签: mysql performance correlated-subquery

当我在MySQL DB上运行此查询时,它会在200毫秒内返回:

SELECT Players.id, Players.level, Players.firstName, Players.lastname, RankedPlayers.rank, Players.points
FROM Players
INNER JOIN
(
    SELECT
    id,
    @curRank := @curRank + 1 AS rank
    FROM Players p, (SELECT @curRank := 0) r
    ORDER BY points DESC
) as RankedPlayers
    ON RankedPlayers.id = Players.id
ORDER BY rank;

但是当我添加WHERE player.id = 231145时,需要 9秒

SELECT Players.id, Players.level, Players.firstName, Players.lastname, RankedPlayers.rank, Players.points
FROM Players
INNER JOIN
(
    SELECT
    id,
    @curRank := @curRank + 1 AS rank
    FROM Players p, (SELECT @curRank := 0) r
    ORDER BY points DESC
) as RankedPlayers
    ON RankedPlayers.id = Players.id
WHERE Players.id  = 231145
ORDER BY rank;

0 个答案:

没有答案