当前位置:   article > 正文

mysql索引失效问题_mysql bigint 索引失效

mysql bigint 索引失效

1、单个主键

  1. CREATE TABLE `user_details` (
  2. `id` bigint(11) NOT NULL AUTO_INCREMENT,
  3. `user_name` varchar(50) DEFAULT NULL,
  4. `user_phone` varchar(11) DEFAULT NULL,
  5. PRIMARY KEY (`id`)
  6. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

#创建索引
alter table user_details add index user_name_index(`user_name`);

desc user_details;

insert into user_details values(1,'yushengjun1','15921009245');
insert into user_details values(2,'yushengjun2','15921009245');
insert into user_details values(3,'yushengjun3','15921009245');
insert into user_details values(4,'yushengjun4','15921009245');
insert into user_details values(5,'yushengjun5','15921009245');
insert into user_details values(6,'yushengjun6','15921009245');

#主键索引
EXPLAIN select * from user_details WHERE id=1;
#主键索引
EXPLAIN select * from user_details WHERE id='1';

#普通索引
EXPLAIN select * from user_details WHERE user_name='15921009245';

#不走索引
EXPLAIN select * from user_details WHERE user_name =15921009245;

#不走索引
EXPLAIN select * from user_details WHERE user_phone='15921009245';

#主键索引模糊查询百分号开头不走索引
EXPLAIN select * from user_details WHERE id like '%sss';

#主键索引模糊查询百分号结束不走索引
EXPLAIN select * from user_details WHERE id like 'sss%';

#普通索引模糊查询百分号开头不走索引
EXPLAIN select * from user_details WHERE user_name like '%sss';

#走索引user_name_index,普通索引模糊查询百分号结束走索引
EXPLAIN select * from user_details WHERE user_name like 'sss%';

#走索引PRIMARY
EXPLAIN select * from user_details WHERE id=1 and user_name='yushengjun1';

#走索引PRIMARY,user_name_index
EXPLAIN select * from user_details WHERE id=1 or user_name='yushengjun1';

#走索引PRIMARY,user_name_index
EXPLAIN select * from user_details WHERE user_name='yushengjun1' and user_phone='aaaa';

#不走索引
EXPLAIN select * from user_details WHERE user_name='yushengjun1' or user_phone='aaaa';

2、联合主键

  1. drop table if exists `user_details`;
  2. CREATE TABLE `user_details` (
  3. `id` bigint(11) NOT NULL AUTO_INCREMENT,
  4. `user_name` varchar(50) DEFAULT NULL,
  5. `user_phone` varchar(11) DEFAULT NULL,
  6. PRIMARY KEY (`id`)
  7. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

desc user_details;

alter table user_details add index user_name_phone_index(`user_name`, `user_phone`);

insert into user_details values(1,'yushengjun1','15921009245');
insert into user_details values(2,'yushengjun2','15921009245');
insert into user_details values(3,'yushengjun3','15921009245');
insert into user_details values(4,'yushengjun4','15921009245');
insert into user_details values(5,'yushengjun5','15921009245');
insert into user_details values(6,'yushengjun6','15921009245');

#走索引user_name_phone_index
explain select * from user_details where user_name = 'aaa';

#不走索引
explain select * from user_details where user_phone = 'aaa';

#走索引user_name_phone_index
explain select * from user_details where user_name = 'aaa' and user_phone = 'aaa';

#走索引user_name_phone_index
explain select * from user_details where user_phone = 'aaa' and user_name = 'aaa';

explain select * from user_details where user_name like 'aaa&' and user_phone = 'aaa';

#不走索引
explain select * from user_details where user_name = 'aaa' or user_phone = 'aaa';

 3、索引失效的几种情况:

索引无法存储null值

前导模糊查询不能利用索引(like '%XX'或者like '%XX%')

1.如果条件中有or,即使其中有条件带索引也不会使用(这也是为什么尽量少用or的原因)

要想使用or,又想让索引生效,只能将or条件中的每个列都加上索引

2.对于多列索引,不是使用的第一部分,则不会使用索引

3.like查询以%开头

4.如果列类型是字符串,那一定要在条件中将数据使用引号引用起来,否则不使用索引

5.如果mysql估计使用全表扫描要比使用索引快,则不使用索引

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/381427
推荐阅读
相关标签
  

闽ICP备14008679号