问题
有谁知道为什么基于 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 不是一个好的解决方案)谢谢