首页 > 数据库 >MySQL 索引失效场景总结

MySQL 索引失效场景总结

时间:2024-03-31 12:45:24浏览次数:15  
标签:customer name idx 索引 key MySQL 失效 id

查询条件有 or

假设在 customer_name 字段设置了普通索引,执行以下 sql:

# type: ref, possible_keys: idx_customer_name, key: idx_customer_name
# idx_customer_name 索引生效
explain select id, customer_name, company_name from t_user_info where customer_name = 'test_name'

# type: ref, possible_keys: idx_customer_name, key: idx_customer_name
# idx_customer_name 索引生效
explain select id, customer_name, company_name from t_user_info where customer_name = 'test_name' and company_name = 'test_name'

# type: all, possible_keys: idx_customer_name, key: null
# idx_customer_name 索引不生效,使用全表扫描
explain select id, customer_name, company_name from t_user_info where customer_name = 'test_name' or company_name = 'test_company'

like 查询以 % 开头

假设在 customer_name 字段设置了普通索引,执行以下 sql:

# type: all, possible_keys: null, key: null
# idx_customer_name 索引不生效
explain select id, customer_name, company_name from t_user_info where customer_name like '%name'

# type: range, possible_keys: idx_customer_name, key: idx_customer_name
# idx_customer_name 索引生效
explain select id, customer_name, company_name from t_user_info where customer_name like 'test%'

如果希望以 % 开头仍使用索引,则需要使用覆盖索引,即只查询带索引字段的列

# type: index, possible_keys: null, key: idx_customer_name
# idx_customer_name 索引生效
# id 是主键,idx_customer_name 构成的 b+tree 除了有 customer_name,也包含用于指向对应行的 id
explain select id, customer_name from t_user_info where customer_name like '%name'

索引列参与运算

假设 id 字段为主键,执行以下 sql:

# type: const, possible: primary, key: primary
# idx_id 索引生效
explain select id, customer_name, company_name from t_user_info where id = 2

# type: all, possible: null, key: null
# idx_id 索引不生效
explain select id, customer_name, company_name from t_user_info where id + 1 = 2

索引列使用函数

假设在 customer_name 字段设置了普通索引,执行以下 sql:

# type: ref, possible_keys: idx_customer_name, key: idx_customer_name
# idx_customer_name 索生效
explain select id, customer_name, company_name from t_user_info where customer_name = '查理一世'

# type: all, possible_keys: null, key: null
# idx_customer_name 索引不生效
explain select id, customer_name, company_name from t_user_info where substr(customer_name, 1, 3) = '查理一'

类型转换

假设在 customer_name 字段设置了普通索引,执行以下 sql:

# type: all, possible_keys: idx_customer_name, key: null
# idx_customer_name 索引不生效
explain select id, customer_name, company_name from t_user_info where customer_name = 10

这是因为 mysql 会自动对字段执行类型转换函数,如上 sql 相当于

select id, customer_name, company_name from t_user_info where cast(customer_name as signed) = 10

两列做比较

如果两个列数据都有索引,但在查询条件中对两列数据进行了对比操作,则会导致索引失效

假设在 customer_name、company_name 字段设置了普通索引,执行以下 sql,仅作示例:

# type: range, possible_keys: idx_customer_name, key: idx_customer_name
# idx_customer_name 索引生效
explain select id, customer_name, company_name from t_user_info where customer_name > '查理一世'

# type: all, possible_keys: null, key: null
# idx_customer_name 索引生效
explain select id, customer_name, company_name from t_user_info where customer_name > company_name

联合索引不满足最左匹配原则

联合索引遵从最左匹配原则,所谓最左匹配原则,就是如果 SQL 语句用到了联合索引中的最左边的索引,那么这条 SQL 语句就可以利用这个联合索引去进行匹配。值得注意的是,当遇到范围查询(>、<、between、like)时就会停止匹配

假设对 a、b、c 字段建立联合索引 idx_a_b_c,执行 sql 如下:

# type: ref, possible_keys: idx_a_b_c, key: idx_a_b_c, ref: const
# idx_a_b_c 索引生效,a 字段能用到索引
explain select * from test_table where a = 1

# type: ref, possible_keys: idx_a_b_c, key: idx_a_b_c, ref: const, const
# idx_a_b_c 索引生效,a、b 字段能用到索引
explain select * from test_table where a = 1 and b = 2

# type: ref, possible_keys: idx_a_b_c, key: idx_a_b_c, ref: const, const, const
# idx_a_b_c 索引生效,a、b、c 字段能用到索引
explain select * from test_table where a = 1 and b = 2 and c = 3

# type: ref, possible_keys: idx_a_b_c, key: idx_a_b_c, ref: const, const, const
# idx_a_b_c 索引生效,a、b、c 字段能用到索引,优化器会调整 a、b、c 的顺序,从而用上索引
explain select * from test_table where b = 2 and c = 3 and a = 1

# type: range, possible_keys: idx_a_b_c, key: idx_a_b_c, ref: null, key_len: 75
# a 字段类型为 varchar(18),字符集为 utf8mb4,1 个字符占 4 个字节,占用 4*18=72 字节
# varchar 为变长数据类型,额外占用 2 个字节
# 字段默认为 null,额外占用 1 个字节
# 因此 key_len = 72 + 2 + 1 = 75,可判断 idx_a_b_c 索引生效,但只有 a 字段用到索引
explain select * from test_table where a > 1 and b = 2 and c = 3

我们知道索引是用 B+Tree 实现的,如果只对 a 字段建立普通索引,那么 B+Tree 根据 a 字段排序。如果对 a、b、c 建立联合索引,那么首先根据 a 字段排序,如果 a 字段值相同,再根据 b 字段排序,如果 b 字段值也相同,再根据 c 字段排序。因此,使用联合索引必须按照从左到右,也就是字段排序的顺序,只有先用了 a,才能接着使用 b,使用了 b 才能接着使用 c

标签:customer,name,idx,索引,key,MySQL,失效,id
From: https://www.cnblogs.com/Yee-Q/p/18103308

相关文章

  • @Transactional详解(作用、失效场景与解决方法)| 事务注解实际原理(AOP)解析
    开发中代码实现事务的方式,理论上说有两种:编程式事务、注解式事务。但是实际上使用最多的还是注解实现的事务控制; 1、编程式事务(开发用的很少了)基于底层的API,如PlatformTransactionManager、TransactionDefinition 和 TransactionTemplate 等核心接口,开发者完全可以通过编......
  • java计算机毕业设计(附源码)阳光心理健康网站(ssm+mysql+maven+LW文档)
    本系统(程序+源码)带文档lw万字以上  文末可领取本课题的JAVA源码参考系统程序文件列表系统的选题背景和意义标题:探索阳光心理,点亮健康人生在快节奏的现代生活中,人们常常面临巨大的心理压力和挑战。阳光心理健康网站应运而生,旨在为广大群众提供一个专业、便捷的心理健康服......
  • java计算机毕业设计(附源码)阳光学院蓝桥杯名师工作室(ssm+mysql+maven+LW文档)
    本系统(程序+源码)带文档lw万字以上  文末可领取本课题的JAVA源码参考系统程序文件列表系统的选题背景和意义选题背景:阳光学院蓝桥杯名师工作室的成立,源自于当前信息技术迅猛发展的时代背景和对高素质计算机人才的迫切需求。在全球化和数字化浪潮推动下,编程能力、算法设计......
  • java计算机毕业设计(附源码)燕理快递中转站系统(ssm+mysql+maven+LW文档)
    本系统(程序+源码)带文档lw万字以上  文末可领取本课题的JAVA源码参考系统程序文件列表系统的选题背景和意义选题背景:在现代物流行业的快速发展中,快递服务作为重要的一环,对于提升社会整体的运营效率和满足消费者需求具有不可忽视的作用。燕理快递中转站系统作为该领域内的......
  • java计算机毕业设计(附源码)演唱会网上订票购票系统(ssm+mysql+maven+LW文档)
    本系统(程序+源码)带文档lw万字以上  文末可领取本课题的JAVA源码参考系统程序文件列表系统的选题背景和意义选题背景:随着数字时代的到来,互联网技术已深入人们的日常生活,尤其在娱乐消费领域,其影响尤为显著。演唱会作为现代流行文化的重要组成部分,吸引了大量音乐爱好者的关......
  • MySQL索引
    随笔参考:一文搞懂MySQL索引(清晰明了)-CSDN博客MySQL体系构架、存储引擎和索引结构_mysqlsegment为什么分为叶节点和非叶节点-CSDN博客一.什么是索引索引对于表中的记录而言起到一个目录的作用,通过查询索引,我们可以快速的找到想要的记录,而不需要对表进行全面的扫描。就和......
  • java毕业设计社团物品租赁小程序(Springboot+mysql+jdk1.8+maven3.39)
    本系统(程序+源码)带文档lw万字以上 文末可领取本课题的JAVA源码参考系统程序文件列表系统的选题背景和意义一、选题背景:随着校园文化的繁荣和学生社团活动的增多,各类社团对于特定物品的需求日益增长。这些物品包括活动器材、会议设备、表演服装等,购买成本高且使用频率不......
  • java毕业设计汽车服务系统(Springboot+mysql+jdk1.8+maven3.39)
    本系统(程序+源码)带文档lw万字以上 文末可领取本课题的JAVA源码参考系统程序文件列表系统的选题背景和意义选题背景:随着科技的进步和汽车产业的迅猛发展,汽车行业的竞争已经从单纯的价格竞争逐渐转向服务竞争。消费者对汽车服务的需求日益增长,不仅关注汽车的性能、外观和......
  • java毕业设计社团管理系统(Springboot+mysql+jdk1.8+maven3.39)
    本系统(程序+源码)带文档lw万字以上 文末可领取本课题的JAVA源码参考系统程序文件列表系统的选题背景和意义选题背景:随着高校教育的不断发展,学生社团作为校园文化的重要组成部分,承担着丰富学生课余生活、培养学生兴趣爱好、提升学生实践能力的重要职能。然而,传统的社团管......
  • java毕业设计实验室资源管理(Springboot+mysql+jdk1.8+maven3.39)
    本系统(程序+源码)带文档lw万字以上 文末可领取本课题的JAVA源码参考系统程序文件列表系统的选题背景和意义一、选题背景:在高等教育和科研机构中,实验室是进行教学和科学研究的重要场所。一个现代化的实验室通常拥有大量昂贵的设备、仪器和材料。如何有效地管理这些资源,确......