show create table 20130314t1
CREATE TABLE `20130314t1` (
`id` int(11) NOT NULL,
`col1` int(11) NOT NULL,
`col2` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `key1` (`col1`,`col2`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
其中表里插入了100w数据,col1和col2都是100以内的随机数.
进行如下的查询,
select COUNT(*) from 20130314t1 where col1 >19 and col1 <30 and col2 >30 and col2 <33;
这是在多列索引上进行的range scan,理论上mysql只能使用索引的一部分,即col1那部分,从查询计划来看,key_len是4
mysqlslap工具测试下,平均时间是0.178s
把查询改成
select COUNT(*) from 20130314t1 where col1 BETWEEN 20 and 29 and col2 >30 and col2 <33;
这个非常奇怪,理论上key_len应该还是4,因为5.1的文档说
引用
If the operator is >, =, <=, !=, <>, BETWEEN, or LIKE, the optimizer uses it but considers no more key parts.
结果key_le