什么是组合索引?在哪些场景中,组合索引会失效?

什么是组合索引?

  由多个字段组成的索引叫组合索引。

问题:在哪些场景中,组合索引会失效?

场景

数据表:job_status_trace_log【说明:id是主键】
数据量:35w
创建索引:ALTER table job_status_trace_log add INDEX creation_time_index(creation_time,job_name,source)

注意:创建表的时候,字段要超过组合索引字段的个数,因为它会命中覆盖索引,导致跟我下面测试的数据不一致

例子:组合索引(a,b,c)都有哪些排列组合

  • a,b,c
  • a,c,b
  • c,a,b
  • c,b,a
  • b,c,a
  • a,b
  • b,a
  • a,c
  • c,a
  • b,c
  • a
  • b
  • c
  • …就列举那么多已经够了

先看看总结,再看验证过程,可能会更棒

总结:

1、组合索引字段无论顺序如何改变都会用到索引,前提是所有字段都在where条件上
2、如果想要使用一个或者两个字段在where条件上,必须有组合索引里的第一个字段,但是与顺序无关,例如a,c或c,a,这种场景是可以命中索引的。但是,b,c或c,b这种是不会命中索引的。
3、如果组合索引存在范围查询,则组合索引可能会命中索引,这个跟B+Tree的叶子节点中存储的数据是否在当前的叶子节点中,即InnoDB存储引擎的最小存储单元——页,InnoDB页的大小默认是16k,可以通过参数查看页的默认大小:show global status like ‘innodb_page_size’;如果想要修改InnoDB页的大小,需要通过修改mysql源码才可以修改,找到源码文件(storage/innobase/include/univ.i),找到参数:UNIV_PAGE_SIZE,该参数必须是2的n次方,例如4k、8k、16k、32k、64k等等。
4、order by 只能使用a,才能用到索引

排列组合一:a,b,c,恭喜成功命中索引
mysql> explain select * from job_status_trace_log where creation_time = '2020-01-01 00:00:00' and job_name ='member.channelRouteTask' and source='LITE_EXECUTOR'\G
*************************** 1.***************************
id           : 1
select_type  : SIMPLE
table        : job_status_trace_log
partitions   : NULL
type         : ref
possible_keys: creation_time_index
key          : creation_time_index
key_len      : 609
ref          : const,const,const
rows         : 1
filtered     : 100.00
Extra        : NULL
1 行于数据集 (0.02)
排列组合二:a,c,b,恭喜成功命中索引
mysql> explain select * from job_status_trace_log where creation_time = '2020-01-01 00:00:00' and source='LITE_EXECUTOR' and job_name ='member.channelRouteTask'\G
*************************** 1.***************************
id           : 1
select_type  : SIMPLE
table        : job_status_trace_log
partitions   : NULL
type         : ref
possible_keys: creation_time_index
key          : creation_time_index
key_len      : 609
ref          : const,const,const
rows         : 1
filtered     : 100.00
Extra        : NULL
1 行于数据集 (0.03)
排列组合三:c,a,b,恭喜成功命中索引
mysql> explain select * from job_status_trace_log where source='LITE_EXECUTOR' and creation_time = '2020-01-01 00:00:00' and job_name ='member.channelRouteTask'\G
*************************** 1.***************************
id           : 1
select_type  : SIMPLE
table        : job_status_trace_log
partitions   : NULL
type         : ref
possible_keys: creation_time_index
key          : creation_time_index
key_len      : 609
ref          : const,const,const
rows         : 1
filtered     : 100.00
Extra        : NULL
1 行于数据集 (0.03)
排列组合四:c,b,a,恭喜成功命中索引
mysql> explain select * from job_status_trace_log where source='LITE_EXECUTOR' and job_name ='member.channelRouteTask' and creation_time = '2020-01-01 00:00:00'\G
*************************** 1.***************************
id           : 1
select_type  : SIMPLE
table        : job_status_trace_log
partitions   : NULL
type         : ref
possible_keys: creation_time_index
key          : creation_time_index
key_len      : 609
ref          : const,const,const
rows         : 1
filtered     : 100.00
Extra        : NULL
1 行于数据集 (0.07)
排列组合五:b,c,a,恭喜成功命中索引
mysql> explain select * from job_status_trace_log where job_name ='member.channelRouteTask' and source='LITE_EXECUTOR' and creation_time = '2020-01-01 00:00:00'\G
*************************** 1.***************************
id           : 1
select_type  : SIMPLE
table        : job_status_trace_log
partitions   : NULL
type         : ref
possible_keys: creation_time_index
key          : creation_time_index
key_len      : 609
ref          : const,const,const
rows         : 1
filtered     : 100.00
Extra        : NULL
1 行于数据集 (0.02)
排列组合六:a,b,恭喜成功命中索引
mysql> explain select * from job_status_trace_log where creation_time = '2020-01-01 00:00:00' and job_name ='member.channelRouteTask'\G
*************************** 1.***************************
id           : 1
select_type  : SIMPLE
table        : job_status_trace_log
partitions   : NULL
type         : ref
possible_keys: creation_time_index
key          : creation_time_index
key_len      : 407
ref          : const,const
rows         : 2
filtered     : 100.00
Extra        : NULL
1 行于数据集 (0.04)
排列组合七:b,a,恭喜成功命中索引
mysql> explain select * from job_status_trace_log where job_name ='member.channelRouteTask' and creation_time = '2020-01-01 00:00:00'\G
*************************** 1.***************************
id           : 1
select_type  : SIMPLE
table        : job_status_trace_log
partitions   : NULL
type         : ref
possible_keys: creation_time_index
key          : creation_time_index
key_len      : 407
ref          : const,const
rows         : 2
filtered     : 100.00
Extra        : NULL
1 行于数据集 (0.03)
排列组合八:a,c,恭喜成功命中索引
mysql> explain select * from job_status_trace_log where creation_time = '2020-01-01 00:00:00' and source='11'\G 
*************************** 1.***************************
id           : 1
select_type  : SIMPLE
table        : job_status_trace_log
partitions   : NULL
type         : ref
possible_keys: creation_time_index
key          : creation_time_index
key_len      : 5
ref          : const
rows         : 17
filtered     : 10.00
Extra        : Using index condition
1 行于数据集 (0.04)
排列组合九:c,a,恭喜成功命中索引
mysql> explain select * from job_status_trace_log where source='11' and creation_time = '2020-01-01 00:00:00'\G
*************************** 1.***************************
id           : 1
select_type  : SIMPLE
table        : job_status_trace_log
partitions   : NULL
type         : ref
possible_keys: creation_time_index
key          : creation_time_index
key_len      : 5
ref          : const
rows         : 17
filtered     : 10.00
Extra        : Using index condition
1 行于数据集 (0.04)
排列组合十:b,c,很遗憾全表扫描了
mysql> explain select * from job_status_trace_log where job_name ='member.channelRouteTask' and source='LITE_EXECUTOR'\G
*************************** 1.***************************
id           : 1
select_type  : SIMPLE
table        : job_status_trace_log
partitions   : NULL
type         : ALL
possible_keys: NULL
key          : NULL
key_len      : NULL
ref          : NULL
rows         : 4316676
filtered     : 1.00
Extra        : Using where
1 行于数据集 (0.03)
排列组合十一:a,恭喜成功命中索引
mysql> explain select * from job_status_trace_log where creation_time = '2020-01-01 00:00:00'\G
*************************** 1.***************************
id           : 1
select_type  : SIMPLE
table        : job_status_trace_log
partitions   : NULL
type         : ref
possible_keys: creation_time_index
key          : creation_time_index
key_len      : 5
ref          : const
rows         : 17
filtered     : 100.00
Extra        : NULL
1 行于数据集 (0.02)
排列组合十二:b,很遗憾全表扫描了
mysql> explain select * from job_status_trace_log where job_name ='member.channelRouteTask'\G
*************************** 1.***************************
id           : 1
select_type  : SIMPLE
table        : job_status_trace_log
partitions   : NULL
type         : ALL
possible_keys: NULL
key          : NULL
key_len      : NULL
ref          : NULL
rows         : 4316693
filtered     : 10.00
Extra        : Using where
1 行于数据集 (0.04)
排列组合十三:c,很遗憾全表扫描了
mysql> explain select * from job_status_trace_log where source='LITE_EXECUTOR'\G 
*************************** 1.***************************
id           : 1
select_type  : SIMPLE
table        : job_status_trace_log
partitions   : NULL
type         : ALL
possible_keys: NULL
key          : NULL
key_len      : NULL
ref          : NULL
rows         : 4316702
filtered     : 10.00
Extra        : Using where
1 行于数据集 (0.06)
排列组合十四:a>0,b=1,c=1,很遗憾全表扫描了
mysql> explain select * from job_status_trace_log where creation_time > '2020-01-01 00:00:00' and job_name ='member.channelRouteTask' and source='LITE_EXECUTOR'\G
*************************** 1.***************************
id           : 1
select_type  : SIMPLE
table        : job_status_trace_log
partitions   : NULL
type         : ALL
possible_keys: creation_time_index
key          : NULL
key_len      : NULL
ref          : NULL
rows         : 4316703
filtered     : 0.38
Extra        : Using where
1 行于数据集 (0.04)

排列组合十五:a=1,b>0,c=1,很遗憾全表扫描了
mysql> explain select * from job_status_trace_log where  job_name ='member.channelRouteTask' and creation_time > '2020-01-01 00:00:00' and source='LITE_EXECUTOR'\G
*************************** 1.***************************
id           : 1
select_type  : SIMPLE
table        : job_status_trace_log
partitions   : NULL
type         : ALL
possible_keys: creation_time_index
key          : NULL
key_len      : NULL
ref          : NULL
rows         : 4316727
filtered     : 0.38
Extra        : Using where
1 行于数据集 (0.02)
排列组合十六:a=1,b=1,c>0,很遗憾全表扫描了
mysql> explain select * from job_status_trace_log where  job_name ='member.channelRouteTask'  and source='LITE_EXECUTOR' and creation_time > '2020-01-01 00:00:00'\G
*************************** 1.***************************
id           : 1
select_type  : SIMPLE
table        : job_status_trace_log
partitions   : NULL
type         : ALL
possible_keys: creation_time_index
key          : NULL
key_len      : NULL
ref          : NULL
rows         : 4316730
filtered     : 0.38
Extra        : Using where
1 行于数据集 (0.06)
排列组合十七:a>0,很遗憾全表扫描了
mysql> explain select * from job_status_trace_log where creation_time > '2020-01-01 00:00:00'\G
*************************** 1.***************************
id           : 1
select_type  : SIMPLE
table        : job_status_trace_log
partitions   : NULL
type         : ALL
possible_keys: creation_time_index
key          : NULL
key_len      : NULL
ref          : NULL
rows         : 4316730
filtered     : 38.09
Extra        : Using where
1 行于数据集 (0.03)
总结:
  • 1、组合索引字段无论顺序如何改变都会用到索引,前提是所有字段都在where条件上。
  • 2、如果想要使用一个或者两个字段在where条件上,必须有组合索引里的第一个字段,但是与顺序无关,例如a,c或c,a,这种场景是可以命中索引的。但是,b,c或c,b这种是不会命中索引的。
  • 3、如果组合索引存在范围查询,则组合索引可能会命中索引,这个跟B+Tree的叶子节点中存储的数据是否在当前的叶子节点中,即InnoDB存储引擎的最小存储单元——页,InnoDB页的大小默认是16k,可以通过参数查看页的默认大小:show global status like ‘innodb_page_size’;如果想要修改InnoDB页的大小,需要通过修改mysql源码才可以修改,找到源码文件(storage/innobase/include/univ.i),找到参数:UNIV_PAGE_SIZE,该参数必须是2的n次方,例如4k、8k、16k、32k、64k等等。
  • 4、order by 只能使用a,才能用到索引。

欢迎关注我的微信公众号,里面有很多干货,各种面试题
在这里插入图片描述

  • 25
    点赞
  • 173
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 10
    评论
索引在以下场景下可能失效: 1. 使用函数或表达式进行查询:如果在查询条件使用了函数或表达式,例如`WHERE UPPER(column_name) = 'VALUE'`,索引可能无法起作用,因为函数或表达式的结果无法直接匹配索引的值。 2. 对索引列进行类型转换:如果在查询条件对索引列进行了类型转换,例如`WHERE CAST(column_name AS VARCHAR) = 'value'`,索引可能无法起作用,因为类型转换后的值无法直接匹配索引的数据类型。 3. 使用模糊查询:当使用模糊查询操作符(如`LIKE`)进行搜索时,如果搜索模式以通配符开头(例如`LIKE '%value'`),则索引可能无法起作用,因为通配符开头的模式无法利用索引的有序性。 4. 列的基数太低:如果索引列的基数(不同值的数量)非常低,即使使用了索引,数据库优化器可能认为全表扫描更快,从而选择不使用索引。 5. 数据量过小:当表的数据量非常小(例如只有几行)时,使用索引进行查询可能比全表扫描更慢,因为额外的索引查找开销可能抵消使用索引的好处。 6. 范围查询:对于一些范围查询(例如`BETWEEN`、`>、<`等),索引可能失效,因为范围查询需要扫描多个索引节点,而不是单个等值匹配。 7. 隐式类型转换:如果在查询条件进行了隐式类型转换,例如将字符串与数字进行比较,索引可能无法起作用,因为隐式转换可能导致索引列的值与查询条件不匹配。 8. 复合索引未使用第一个列:对于复合索引,如果查询条件没有使用到索引的第一个列,那么索引可能失效。 要确保索引的有效使用,需要根据具体的查询场景和数据特点来设计和优化索引。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

雨润泽林

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

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

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

打赏作者

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

抵扣说明:

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

余额充值