前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MySQL组合索引不被命中使用的情况

MySQL组合索引不被命中使用的情况

作者头像
Li_XiaoJin
发布2022-06-10 21:23:44
1.2K0
发布2022-06-10 21:23:44
举报
文章被收录于专栏:Lixj's BlogLixj's Blog

今天遇到这个问题,之前没有仔细想过这个问题,记录一下。

1. 准备工作

先创建一张表

代码语言:javascript
复制
create table mytable (
    col1 int primary key,
    col2 int not null,
    col3 int not null,
    col4 int not null,
    col5 int not null,
    col6 int not null
);

添加组合索引

代码语言:javascript
复制
ALTER TABLE mytable ADD INDEX index_name(col1,col2,col3);

插入数据

代码语言:javascript
复制
INSERT INTO cloud.mytable (col1,col2,col3,col4,col5,col6) VALUES
     (1,1,1,1,1,1),
     (2,1,1,1,1,1),
     (3,1,1,1,1,1),
     (4,1,1,1,1,1),
     (5,1,1,1,1,1),
     (6,1,1,1,1,1),
     (7,1,1,1,1,1),
     (8,1,1,1,1,1),
     (9,1,1,1,1,1),
     (10,1,1,1,1,1);
INSERT INTO cloud.mytable (col1,col2,col3,col4,col5,col6) VALUES
     (11,1,1,1,1,1);

2. 测试

1. 查询条件中包含索引的前缀部分, 也就是 col1, 可以触发索引的使用
代码语言:javascript
复制
explain select * from mytable where col1=1;// 命中索引
explain select * from mytable where col2=1;// 未命中索引
explain select * from mytable where col3=1;// 未命中索引

explain select * from mytable where col1=1 and col2 = 1;// 命中索引
explain select * from mytable where col2=1 and col1 = 1;// 命中索引
explain select * from mytable where col1=1 and col3 = 1;// 命中索引
explain select * from mytable where col3=1 and col1 = 1;// 命中索引
explain select * from mytable where col2=1 and col3 = 1;// 未命中索引
explain select * from mytable where col3=1 and col2 = 1;// 未命中索引
2. 使用联合索引的全部索引键, 可触发索引的使用
代码语言:javascript
复制
explain select * from mytable where col1=1 and col2 = 1;// 命中索引
explain select * from mytable where col1=1 and col2 = 1 and col3 =1;// 命中索引
explain select * from mytable where col1=2 and col3 = 1 and col1 =1;// 未命中索引
explain select * from mytable where col1=3 and col2 = 1 and col1 =1;// 未命中索引
3. 根据最左前缀原则查询条件中包含索引的前缀部分, 也就是 col1, 可以触发索引的使用
代码语言:javascript
复制
explain select * from mytable where col1=1;// 命中索引
explain select * from mytable where col1=1 and col4=1;// 命中索引
4. 使用部分索引键, 但不包含索引的前缀部分,不可触发索引的使用
代码语言:javascript
复制
explain select * from mytable where col2=1;// 未命中索引
explain select * from mytable where col3=1;// 未命中索引
5. 使用联合索引的全部索引键, 但不是AND操作, 不可触发索引的使用
代码语言:javascript
复制
explain select * from mytable where col1=1 or col2=1;// 未命中索引
explain select * from mytable where col2=1 or col1=1;// 未命中索引
explain select * from mytable where col1=1 or col2=1 or col3=1;// 未命中索引
6. 使用联合索引的,但是在索引列使用比较、计算的(包含不等于和not)不可触发索引的使用;

但是请注意在对主键和int类型的索引使用比较类型的则可以出发索引;

使用联合索引的,但是在索引列使用比较、计算的不可触发索引的使用

代码语言:javascript
复制
explain select * from mytable where col1 > 0;// 命中索引
explain select * from mytable where col1 + 1 > 1;// 未命中索引
explain select * from mytable where col1  > 1 + 1;// 命中索引
7. 使用联合索引的,但是在索引列使用前导模糊查询、正则匹配的不可触发索引的使用
代码语言:javascript
复制
explain select * from mytable where col1 like  "%1"; //不可命中索引
explain select * from mytable where col1 regexp "^%1"; //不可命中索引
explain select * from mytable where col1 like  "1%"; //可以命中索引

参考链接:https://www.jianshu.com/p/af6075c5e9fb

Copyright: 采用 知识共享署名4.0 国际许可协议进行许可 Links: https://lixj.fun/archives/mysql组合索引不被命中使用的情况

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-04-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 准备工作
  • 2. 测试
    • 1. 查询条件中包含索引的前缀部分, 也就是 col1, 可以触发索引的使用
      • 2. 使用联合索引的全部索引键, 可触发索引的使用
        • 3. 根据最左前缀原则查询条件中包含索引的前缀部分, 也就是 col1, 可以触发索引的使用
          • 4. 使用部分索引键, 但不包含索引的前缀部分,不可触发索引的使用
            • 5. 使用联合索引的全部索引键, 但不是AND操作, 不可触发索引的使用
              • 6. 使用联合索引的,但是在索引列使用比较、计算的(包含不等于和not)不可触发索引的使用;
                • 7. 使用联合索引的,但是在索引列使用前导模糊查询、正则匹配的不可触发索引的使用
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档