创建表
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(30) NOT NULL DEFAULT '',
`age` tinyint(4) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `idx_name` (`name`),
KEY `idx_age` (`age`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
插入数据
insert into users(name,age) values('vilay',50),('2',40);
执行分析计划
explain explain select * from users where age='40';
虽然右侧带了单引号,但是40只有数字,可以直接转换为数字,所以虽然会有隐式转换,但是不影响使用索引
另一个分析计划
explain select * from users where name=2;
name是字符串,2是数字,两侧不一致,会有隐式转换,cast(index_filed as signed),然后和2进行比较,导致全表扫描。
所以在写数据库语句的时候,类型一定要注意。