mysql int 索引无效_mysql索引无效

真正索引有效的只有 C, 不知你的题是出自何处, 更有可能的是题目搞错了.

AB 完全用不到联合索引,因为索引是按记录顺序查找到的,name 在 id 后面, 无法不查 id 直接用 name查询.

创建表和数据

create table a1(

id int(11) not null,

name varchar(32) not null,

age int(11) not null,

INDEX (id,name)

);

insert into a1 (id, name, age) values(1,"Jack",29),(10, "test1", 10),(20,"test2",20),(30,"test3", 30),(40,"ddd",40),(50,"Jack",29);

insert into a1 select id+1, name, age from a1;

insert into a1 select id+1, name, age from a1;

insert into a1 select id+1, name, age from a1;

insert into a1 select id+1, name, age from a1;

insert into a1 select id+1, name, age from a1;

insert into a1 select id+1, name, age from a1;

insert into a1 select id+1, name, age from a1;

insert into a1 select id+1, name, age from a1;

select count(*) from a1;

分析索引

explain select * from a1 where name = 'Jack';

explain select * from a1 where name != 'Jack';

explain select * from a1 where id = 1 and name like 'J%';

explain select * from a1 where id <> 1;

explain select * from a1 where age <> 1;

作为比较, 可以看一下下面的结果

explain select name from a1 where name = 'Jack';

explain select name from a1 where name != 'Jack';

explain select name from a1 where id = 1 and name like 'J%';

explain select name from a1 where id <> 1;

explain select name from a1 where age <> 1;

如查返回结果type=ALL则意味着全表扫描, 有无索引已没太大意义.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值