问题
有人知道为什么基于MyISAM的表没有GROUP BY优化吗?(我正在使用这个版本:5.1.49-3)
测试表
CREATE TABLE `_test2_innodb` (
`i` int(10) unsigned NOT NULL AUTO_INCREMENT,
`n` smallint(5) unsigned NOT NULL,
`t` int(10) unsigned NOT NULL,
`v` smallint(6) NOT NULL,
PRIMARY KEY (`i`),
KEY `i_n` (`n`),
KEY `i_t` (`t`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
CREATE TABLE `_test2_myisam` (
`i` int(10) unsigned NOT NULL AUTO_INCREMENT,
`n` smallint(5) unsigned NOT NULL,
`t` int(10) unsigned NOT NULL,
`v` smallint(6) NOT NULL,
PRIMARY KEY (`i`),
KEY `i_n` (`n`),
KEY `i_t` (`t`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
测试查询
SELECT MAX(i) FROM _test2_myisam GROUP BY n;
SELECT MAX(i) FROM _test2_innodb GROUP BY n;
结果
id, select_type, table, type, poss_keys, key, key_len, ref, rows, extra
1, SIMPLE, _test2_myisam , ALL, , , , , 19998, Using temporary; Using filesort
1, SIMPLE, _test2_innodb, index, , i_n, 2, , 20024, Using index
问题在于如果我使用MyISAM,将会进行全表扫描,在大型表上需要数小时的时间......而MySQL文档没有提到表引擎有不同的实现(http://dev.mysql.com/doc/refman/5.0/en/group-by-optimization.html)。有人知道为什么内部处理方式不同吗?
(注:不,切换到InnoDB不是一个好的解决方案) 谢谢