IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

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

    mckee发表于 2015-08-04 17:16:28
    love 0

    准备数据:

    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';

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

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

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

    结果:
    mysql-idx4.png
    字符串索引中,复合类型查询索引失效。



沪ICP备19023445号-2号
友情链接