【发布时间】:2018-11-01 17:22:37
【问题描述】:
set @points := -1;
set @c := 1;
set @num := 0;
SELECT `player_id`,`name`,`firebase_token`,`highscore`,`fb_pic_url`,`profile_pic`
, @num := if(@points = `highscore`, @num, @num + @c) as rank
, @c := if(@points = `highscore`, @c+1, 1) as dummy
, @points := `highscore` as dummy2
FROM
(SELECT `Match_History`.`player_id`,`players`.`name`,`players`.`firebase_token`,`players`.`profile_pic`,`players`.`fb_pic_url`,`Match_History`.`highscore`
FROM `Match_History`,`players`
WHERE (`Match_History`.`player_id`,`Match_History`.`highscore`) IN
(
SELECT `player_id`, MAX(`highscore`)
FROM `Match_History`
WHERE `contest_id`=128
GROUP BY `player_id`
)
AND `Match_History`.`player_id`=`players`.`id`
GROUP BY `players`.`id`
ORDER BY `Match_History`.`highscore` DESC ,`Match_History`.`timestamp`
) t
ORDER BY `highscore` desc, `player_id` asc;
我正在使用上面的 mysql 查询从下面提到的表中创建排行榜:
Match_History:
idint(11) 非空,
player_id int(11) 非空,
contest_id int(11) 非空,
highscore int(11) 非空,
timestamptimestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
玩家:
id int(11) 非空,
name varchar(100) 默认为空,
profile_picvarchar(30) 默认为空,
fb_pic_urlvarchar(150) 默认为空,
firebase_tokenvarchar(500) 默认为空,
timestamptimestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
以上查询占用过多 CPU,我想对其进行优化。 我非常努力,但无法找到正确的方法来以更优化的方式获得相同的结果。 有人可以帮我吗?
【问题讨论】:
-
你不能使用连接吗?
-
使用 EXPLAIN 查看查询的潜在问题可能在哪里。
-
在DB接口或带入PHP时占用资源过多?
-
什么是解释,对不起,我不太擅长 mysql,但我正在努力改进。有人可以帮助我在这里如何使用 JOIN。根据我的研究,使用连接可以优化它
-
仅在数据库中执行需要一段时间
标签: mysql