索引的使用及索引性能分析

一、索引的使用

① 验证索引效率

在未建立索引前,执行SQL语句,查看SQL的耗时

SELECT *FROM tb_sku WHERE sn = '100000003145001 ';

针对字段创建索引

create index idx_sku sn on tb_sku(sn) ;

然后再次执行相同的SQL语句,再次查看SQL的耗时

SELECT *FROM tb_sku WHERE sn = '100000003145007 ';

② 最左前缀法则

如果索引了多列(联合索引),要遵守最左前缀法则。最左前缀法则指的是查询从索引的最左列开始,并且不跳过索引中的列。如果跳跃某一列,索引将部分失效(后面的字段索引失效)。
explain select * from tb_user where profession= '软件工程' and age = 31 and status = '0';
explain select * from tb_user where profession= '软件工程' and age = 31;
explain select *from tb_user where profession= '软件工程";
explain  select * from tb_user where age = 31 and status = '0';
explain select * from tb_user where status = 'O';

③ 范围查询

联合索引中,出现范围查询(>,<),范围查询右侧的列索引失效
explain select * from tb_user where profession= '软件工程' and age > 30 and status = '0';
explain select * from tb_user where profession= "软件工程' and age >= 30 and status = '0';

④ 索引列运算

不要再索引列上进行运算操作,否则索引将失效。
explain select * from tb_user where substring(phone,10,2) = '15';

⑤ 字符串不加引号

字符串类型字段使用时,不加引号,索引将失效。
explain select * from tb_user where profession="软件工程' and age = 31 and status =o;
explain select * from tb_user where phone = 17799990015;

⑥ 模糊查询

如果仅仅是尾部模糊匹配,索引不会失效。如果是头部模糊匹配,索引失效。
explain select * from tb_user where profession like '软件%';
explain select* from tb_user where profession like '%工程";
explain select * from tb_user where profession like '%工%';

⑦ or连接的条件

用or分割开的条件,如果or前的条件中的列有索引,而后面的列中没有索引,那么涉及的索引都不会被用到。
explain select* from tb_user where id =10 or age = 23;
explain select * from tb_user where phone ='17799990017' or age = 23;
由于age没有索引,所以即使id、phone有索引,索引也会失效。所以需要针对于age也要建立索引。

⑧ 数据分布影响

如果MySQL评估使用索引比全表更慢,则不使用索引。
select * from tb_user where phone >='17799990005";
select * from tb_user where phone >='17799990015';

⑨ SQL提示

SQL提示,是优化数据库的一个重要手段,简单来说,就是在SQL语句中加入一些人为的提示来达到优化操作的目的。
use index:
explain select * from tb_user use index(idx_user_pro) where profession='软件工程';
ignore index:
explain select * from tb_user ignore index(idx_user_pro) where profession= 软件工程';
force index:
explain select * from tb_user force index(idx_user_pro) where profession= "软件工程";

⑩ 覆盖索引

尽量使用覆盖索引(查询使用了索引,并且需要返回的列,在该索引中已经全部能够找到),减少select *。
explain select id, profession from tb_user where profession= '软件工程' and age =31 and status = 'o'
explain select id,profession,age, status from tb_user where profession ="软件工程' and age=31 and status = 'O';
explain select id,profession,age,status, name from tb_user where profession="软件工程' and age=31 and status = '0';
explain select * from tb_user where profession='软件工程' and age =31 and status = '0' ;
using index condition :查找使用了索引,但是需要回表查询数据
using where; using index:查找使用了索引,但是需要的数据都在索引列中能找到,所以不需要回表查询数据。

1️⃣1️⃣前缀索引

当字段类型为字符串(varchar , text等)时,有时候需要索引很长的字符串,这会让索引变得很大,查询时,浪费大量的磁盘IO,影响查询效率。此时可以只将字符串的一部分前缀,建立索引,这样可以大大节约索引空间,从而提高索引效率。

语法:

create index idx xxxx on table_name(column(n)) ;

前缀长度

可以根据索引的选择性来决定,而选择性是指不重复的索引值〈基数)和数据表的记录总数的比值,索引选择性越高则查询效率越高,
唯一索引的选择性是1,这是最好的索引选择性,性能也是最好的
select count(distinct email) / count(*) from tb_user ;
select couint(distinct substring(email, 1,5))/ count(*) from tb_user ;

1️⃣2️⃣ 单列索引与联合索引

单列索引:即一个索引只包含单个列。
联合索引:即一个索引包含了多个列。
在业务场景中,如果存在多个查询条件,考虑针对于查询字段建立索引时,建议建立联合索引,而非单列索引。

单列索引情况;

explain select id, phone, name from tb_user where phone = '17799990010' and name = '韩信';
多条件联合查询时,MySQL优化器会评估哪个字段的索引效率更高,会选择该索引完成本次查询。

为了直观地说明这个概念,我们用(name,age)这个联合索引来分析。

可以看到,索引项是按照索引定义里面出现的字段顺序排序的。

当你的逻辑需求是查到所有名字是“张三”的人时,可以快速定位到 ID4,然后向后遍历得到所有

需要的结果。

二、索引设计原则

1.针对于数据量较大,且查询比较频繁的表建立索引。

2针对于常作为查询条件(where)、排序(order by)、分组(group by)操作的字段建立索引。

3.尽量选择区丹度高的列作为索引,尽量建立唯一索引,区分度越高,使用索引的效率越高。

4.如果是字符串类型的字段,字段的长度较长,可以针对于字段的特点,建立前缀索引。

5、尽量使用联合索引,减少单列索引,查询时,联合索引很多时候可以覆盖索引,节省存储空间,避免回表,提高查询效率。

6.要控制索引的数量,索引并不是多多益善,索引越多,维护索引结构的代价也就越大,会影响增删改的效率

7. 如果索引列不能存储NULL值,请在创建表时使用NOT NULL约束它。当优化器知道每列是否包含NULL值时它可以更好地确定哪个索引最有效地用于查询。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

自xing且乐观~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值