如果这个问题太具体,我提前道歉,但我认为这是一个相当典型的场景:join
并且group by
陷入了数据库和解决它的最佳方法。我的具体问题是我需要根据以下内容创建记分牌:
- 播放 (userid,gameid,score) 40M 行
- 游戏 (gameid) 100K 行
- app_games (appid,gameid) 即游戏被分组到应用程序中,并且应用程序的总分是所有相关游戏的总和 <20 行
用户可以玩多次,并记录他们在每场比赛中的最佳成绩。制定查询很容易,我已经做了几个变体,但是在负载下它们有一个令人讨厌的趋势,即在“复制临时表”中锁定 30-60 秒。
我能做些什么?是否有我应该调整的服务器变量,或者有没有办法重新制定查询以使其更快?我正在使用的查询的派生版本如下(减去用户表连接来获取名称):
select userID,sum(score) as cumscore from
(select userID, gameID,max(p.score) as score
from play p join app_game ag using (gameID)
where ag.appID = 1 and p.score>0
group by userID,gameID ) app_stats
group by userid order by cumscore desc limit 0,20;
或作为临时表:
drop table if exists app_stats;
create temporary table app_stats
select userID,gameID,max(p.score) as score
from play p join app_game ag using (gameID)
where ag.appID = 1 and p.score>0
group by userid,gameID;
select userID,sum(score) as cumscore from app_stats group by userid
order by cumscore desc limit 0,20;
我的索引如下:
show indexes from play;
+-------+------------+----------------------+--------------+------------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+----------------------+--------------+------------------+-----------+-------------+----------+--------+------+------------+---------+
| play | 0 | PRIMARY | 1 | playID | A | 38353712 | NULL | NULL | | BTREE | |
| play | 0 | uk_play_uniqueID | 1 | uniqueID | A | 38353712 | NULL | NULL | YES | BTREE | |
| play | 1 | play_score_added | 1 | dateTimeFinished | A | 19176856 | NULL | NULL | YES | BTREE | |
| play | 1 | play_score_added | 2 | score | A | 19176856 | NULL | NULL | | BTREE | |
| play | 1 | fk_playData_game | 1 | gameID | A | 76098 | NULL | NULL | | BTREE | |
| play | 1 | user_hiscore | 1 | userID | A | 650062 | NULL | NULL | YES | BTREE | |
| play | 1 | user_hiscore | 2 | score | A | 2397107 | NULL | NULL | | BTREE | |
+-------+------------+----------------------+--------------+------------------+-----------+-------------+----------+--------+------+------------+---------+