mysql索引失效问题

1、单个主键

CREATE TABLE `user_details` (
  `id` bigint(11) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(50) DEFAULT NULL,
  `user_phone` varchar(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) 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、联合主键

drop table if exists `user_details`;
CREATE TABLE `user_details` (
  `id` bigint(11) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(50) DEFAULT NULL,
  `user_phone` varchar(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) 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估计使用全表扫描要比使用索引快,则不使用索引

你可能感兴趣的:(Java,Mysql)