优化MySQL查询/表的百万+;排
我是MySQL新手,我正在尝试优化以下查询和/或表。我们非常感谢您的帮助,使这一点更加有效和任何解释 查询:优化MySQL查询/表的百万+;排,mysql,Mysql,我是MySQL新手,我正在尝试优化以下查询和/或表。我们非常感谢您的帮助,使这一点更加有效和任何解释 查询: SELECT SQL_CACHE nick as viewer, CONVERT(rank, UNSIGNED) as rank, CONVERT(FLOOR(amount), UNSIGNED) as amount FROM ( SELECT @rank:=@rank+1 AS rank, nick, amount FROM (
SELECT SQL_CACHE
nick as viewer,
CONVERT(rank, UNSIGNED) as rank,
CONVERT(FLOOR(amount), UNSIGNED) as amount
FROM (
SELECT @rank:=@rank+1 AS rank,
nick,
amount
FROM (
SELECT nick,
SUM(amount) as amount
FROM points_log WHERE dt >= NOW()-INTERVAL 1 hour
GROUP BY nick
) as t1,
(SELECT @rank := 0) t2
ORDER BY amount DESC
) as t3
WHERE nick='PrestonConnors';
输出:
+----------------+------+--------+
| viewer | rank | amount |
+----------------+------+--------+
| prestonconnors | 521 | 13 |
+----------------+------+--------+
1 row in set (1.73 sec)
以下是查询的解释输出:
+----+-------------+------------+--------+---------------+----------+---------+------+--- -----+----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+--------+---------------+----------+---------+------+--------+----------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 5408 | Using where |
| 2 | DERIVED | <derived4> | system | NULL | NULL | NULL | NULL | 1 | Using filesort |
| 2 | DERIVED | <derived3> | ALL | NULL | NULL | NULL | NULL | 5408 | |
| 4 | DERIVED | NULL | NULL | NULL | NULL | NULL | NULL | NULL | No tables used |
| 3 | DERIVED | points_log | index | dt_idx | nick_idx | 25 | NULL | 784143 | Using where |
+----+-------------+------------+--------+---------------+----------+---------+------+--------+----------------+
我能想到的唯一一件事是关于:
points_log(dt, nick, amount)
如果需要这方面的性能,可能需要创建一个汇总表。必须汇总整个表,并使用触发器维护汇总表。我认为您不需要这些转换,也可以使用单个子查询。
points_log(dt, nick, amount)