我有一个有 300 万行和 6 列的表。
表结构:
| Sample | CREATE TABLE `sample` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`FileMD5` varchar(32) NOT NULL,
`NoCsumMD5` varchar(32) NOT NULL,
`SectMD5` varchar(32) NOT NULL,
`SectNoResMD5` varchar(32) NOT NULL,
`ImpMD5` varchar(32) NOT NULL,
`Overlay` tinyint(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`ID`),
KEY `FileMD5` (`FileMD5`),
KEY `NoCsumMD5` (`NoCsumMD5`)
) ENGINE=InnoDB AUTO_INCREMENT=3073630 DEFAULT CHARSET=latin1 |
临时表值:
mysql> SHOW VARIABLES LIKE 'tmp_table_size';
+----------------+----------+
| Variable_name | Value |
+----------------+----------+
| tmp_table_size | 16777216 |
+----------------+----------+
1 row in set (0.00 sec)
mysql> SHOW VARIABLES LIKE 'max_heap_table_size';
+---------------------+----------+
| Variable_name | Value |
+---------------------+----------+
| max_heap_table_size | 16777216 |
+---------------------+----------+
1 row in set (0.00 sec)
我的查询
mysql> explain SELECT NoCsumMD5,Count(FileMD5)
FROM Sample GROUP BY NoCsumMD5
HAVING Count(FileMD5) > 10 ORDER BY Count(FileMD5) Desc ;
+----+-------------+--------+-------+---------------+-----------+---------+------+---------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+-------+---------------+-----------+---------+------+---------+---------------------------------+
| 1 | SIMPLE | Sample | index | NULL | NoCsumMD5 | 34 | NULL | 2928042 | Using temporary; Using filesort |
+----+-------------+--------+-------+---------------+-----------+---------+------+---------+---------------------------------+
如何优化此查询。即使在 10 分钟后,它也不会产生任何输出。
我觉得我已经索引了正确的列并为临时表提供了足够的内存。