为百万+行优化MySQL查询/表

时间:2014-11-22 01:02:33

标签: 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 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)

以下是查询的EXPLAIN输出:

+----+-------------+------------+--------+---------------+----------+---------+------+---    -----+----------------+
| 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    |
+----+-------------+------------+--------+---------------+----------+---------+------+--------+----------------+

这是表格:

CREATE TABLE `points_log` (
  `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  `nick` char(25) NOT NULL,
  `amount` decimal(8,4) NOT NULL,
  `stream_online` tinyint(1) NOT NULL,
  `modification_type` tinyint(3) unsigned NOT NULL,
  `dt` datetime NOT NULL,
  PRIMARY KEY (`id`,`dt`,`nick`),
  KEY `nick_idx` (`nick`),
  KEY `amount_idx` (`amount`),
  KEY `modification_type_idx` (`modification_type`),
  KEY `dt_idx` (`dt`),
  KEY `stream_online_idx` (`stream_online`)
) ENGINE=InnoDB AUTO_INCREMENT=866040 DEFAULT CHARSET=latin1
/*!50100 PARTITION BY RANGE ( YEAR(dt))
SUBPARTITION BY HASH ( MONTH(dt))
SUBPARTITIONS 12
(PARTITION p0 VALUES LESS THAN (2014) ENGINE = InnoDB,
 PARTITION p1 VALUES LESS THAN (2015) ENGINE = InnoDB,
 PARTITION p3 VALUES LESS THAN MAXVALUE ENGINE = InnoDB)

1 个答案:

答案 0 :(得分:1)

我唯一能想到的是索引:

points_log(dt, nick, amount)

如果您需要执行此操作,则可能需要创建摘要表。必须总结整个表并使用触发器维护汇总表。