SQL分组和计数,并尽量避免临时和filesort

时间:2014-03-18 05:23:55

标签: mysql performance database-performance

我有两个匹配和用户的表。

我正试图找到让顶级国家参加比赛的方法,我有这个SQL:

select 
  distinct(user.country), 
  count(*) as counter 
from matches
  left join user on matches.user_id = user.id 
where 
  matches.`date` between '2014-01-01' and '2014-03-15' 
group by user.country 
order by counter DESC
limit 10

问题是我正在“使用where;使用临时;使用文件排序”并且sql在m3.medium RDS亚马逊服务器中花费大约8秒(不错的一个!)

我有user.country索引。两个表都是InnoDB。

有什么改进的想法吗?

表:

CREATE TABLE `user` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `nick` varchar(32) DEFAULT NULL,
  `email` varchar(128) DEFAULT NULL,
  `password` varchar(40) DEFAULT NULL,
  `country` char(2) DEFAULT '',
  PRIMARY KEY (`id`),
  KEY `country` (`country`),
) ENGINE=InnoDB AUTO_INCREMENT=254183 DEFAULT CHARSET=utf8;

CREATE TABLE `matches` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(11) unsigned DEFAULT NULL,
  `date` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `user_id` (`user_id`),
  KEY `date` (`date`)
) ENGINE=InnoDB AUTO_INCREMENT=2593195 DEFAULT CHARSET=utf8;

EXPLAIN给出:

+----+-------------+---------+--------+-----------------+---------+---------+----------------------------+---------+------------------------------------------------+
| id | select_type | table   | type   | possible_keys   | key     | key_len | ref                        | rows    | Extra                                        |
+----+-------------+---------+--------+-----------------+---------+---------+----------------------------+---------+----------------------------------------------+
|  1 | SIMPLE      | matches | ALL    | date            | NULL    | NULL    | NULL                       | 2386708 | Using where; Using temporary; Using filesort |
|  1 | SIMPLE      | user    | eq_ref | PRIMARY,country | PRIMARY | 4       | matches.user_id            |       1 | NULL                                         |
+----+-------------+---------+--------+-----------------+---------+---------+----------------------------+---------+----------------------------------------------+

编辑:更改为内部联接:

+----+-------------+----------+-------+------------------------------+-----------------+---------+------------------+--------+----------------------------------------------+
| id | select_type | table    | type  | possible_keys                | key             | key_len | ref              | rows   | Extra                                        |
+----+-------------+----------+-------+------------------------------+-----------------+---------+------------------+--------+----------------------------------------------+
|  1 | SIMPLE      | user    | index | PRIMARY,country              | country         | 7       | NULL             | 234262 | Using index; Using temporary; Using filesort |
|  1 | SIMPLE      | matches | ref   | user_id,date                 | user_id         | 5       | user.id          |      5 | Using where                                  |
+----+-------------+----------+-------+------------------------------+-----------------+---------+------------------+--------+----------------------------------------------+

0 个答案:

没有答案