您的位置:首页 > 数据库 > MySQL

测试mysql查询中参数整形和字符串类型错误索引使用情况

2015-08-04 00:00 1006 查看
准备数据:
CREATE TABLE `test_idx` (
  `i` int(10) NOT NULL,
  `s` varchar(10) NOT NULL,
  KEY `i` (`i`),
  KEY `s` (`s`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

insert into test_idx(i, s) values (1,'1'),(2,'2'),(3,'3'),(4,'4'),(5,'5'),(6,'6'),(7,'7'),(8,'8'),(9,'9');


测试1:
explain select * from test_idx where i = 1;
explain select * from test_idx where i = '1';


结果:



数字索引,带上引号仍然能够使用索引。
测试2:
explain select * from test_idx where s = 1;
explain select * from test_idx where s = '1';


结果:



字符串索引,不带引号,索引失效。
测试3:
explain select * from test_idx where i in(1,2);
explain select * from test_idx where i in(1,'2');


结果:



数字索引中,复合类型查询索引失效。
测试4:
explain select * from test_idx where s in('1','2');
explain select * from test_idx where s in(1,'2');


结果:



字符串索引中,复合类型查询索引失效。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: