首页 > 其他 > 详细

索引优化:避免索引失效

时间:2019-08-13 00:41:44      阅读:185      评论:0      收藏:0      [点我收藏+]

一、索引失效的十大原因

 

技术分享图片
create table staffs(
    id int primary key auto_increment,
    name varchar(24) not null default ‘‘ comment 姓名,
    age int not null default 0 comment 年龄,
    pos varchar(20) not null default ‘‘ comment 职位,
    add_time timestamp not null default current_timestamp comment 入职时间
    )charset utf8 comment 员工记录表;



insert into staffs(name,age,pos,add_time) values (z3,22,manager,now());
insert into staffs(name,age,pos,add_time) values (z2,23,dev,now());
insert into staffs(name,age,pos,add_time) values (2000,21,dev,now());
Query OK, 1 row affected (0.15 sec)

alter table staffs add index idx_staffs_nameAgePos(name,age,pos);




mysql> select *from staffs;
+----+------+-----+---------+---------------------+
| id | name | age | pos     | add_time            |
+----+------+-----+---------+---------------------+
|  1 | z3   |  22 | manager | 2019-08-12 23:06:53 |
|  2 | z2   |  23 | dev     | 2019-08-12 23:07:35 |
|  3 | 2000 |  21 | dev     | 2019-08-12 23:07:58 |
+----+------+-----+---------+---------------------+
需要的表结构

1、全值匹配我最爱

#建立几个复合索引字段,最好就用上几个字段。且按照顺序来用。

 

 

 

2、最佳左前缀原则

#最前缀法则,必须有车头,中间车厢不能断

 

技术分享图片

 

 

 

3、不在索引列上做任何操作(计算,函数,(自动/手动)类型转换),会导致索引失效而进行全表扫描

 

技术分享图片

 

 

 

4、存储引擎不能使用索引中  范围条件右边的列(范围之后全失效,不包括本身)

 

技术分享图片

 

索引优化:避免索引失效

原文:https://www.cnblogs.com/pdun/p/11343318.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!