mysql 索引匹配原则_MySQL索引最左匹配原则

创建一个测试表

CREATE TABLE `test` (

`id` int(10) unsigned NOT NULL AUTO_INCREMENT,

`a` varchar(32) NOT NULL,

`b` varchar(32) NOT NULL,

`c` varchar(64) NOT NULL,

`d` varchar(128) NOT NULL,

`e` varchar(256) NOT NULL,

`create_time` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

`update_time` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

插入数据

INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES('a', 'b', 'c', 'd', 'e');

INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES('a', 'b', 'c', 'd', 'e');

INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES('a', 'b', 'c', 'd', 'e');

INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES('a', 'b', 'c', 'd', 'e');

INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES('a', 'b', 'c', 'd', 'e');

INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES('a', 'b', 'c', 'd', 'e');

INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES('a', 'b', 'c', 'd', 'e');

INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES('a', 'b', 'c', 'd', 'e');

INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES('a', 'b', 'c', 'd', 'e');

INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES('a', 'b', 'c', 'd', 'e');

查看索引

show index from test;

6c95d204e693e2e91b0807bc2cb94a89.png

查看执行计划

explain select * from test where id = "1";

815d86c0d94ccf495939435ccf50ac7b.png

explain select * from test where a = "1";

06b304709e622bb07d2ca49df4894106.png

explain select * from test where b = "1";

814cb57a6b2e65681c97f2aa7d0b836f.png

explain select * from test where a = "1" and b = "1";

a778da5188b28a05249c4ea9c6105897.png

创建索引(a和b的联合索引)

create index idx_a_b on test(a, b);

查看索引

show index from test;

135f9e0dc7ab9392018458ed86d53ed5.png

查看执行计划

explain select * from test where a = "1";

b39bf707893fae0d7bd8e8802383cde1.png

explain select * from test where b = "1";

8ad8292fec6081d66afae7e9b786f798.png

explain select * from test where a = "1" and b = "1";

0ae78f7a5eca4a9eaeb7708080275e42.png

创建索引(b的索引)

create index idx_b on test(b);

查看索引

show index from test;

9b992e493a808cde3e6742bfe84fe306.png

查看执行计划

explain select * from test where a = "1";

c427ad9d96d14c8265f18032d7027a1d.png

explain select * from test where b = "1";

8b920b7cdf3a35218738d80e773c00f8.png

explain select * from test where a = "1" and b = "1";

372837d5754d05705437137fada793bf.png

删除索引

drop index idx_a_b on test;

drop index idx_b on test;

删除索引后,创建a, b, c 3个字段的联合索引

create index idx_a_b_c on test(a, b, c);

查看如下语句的执行计划

-- ref

explain select * from test where a = "1";

-- all 扫全表

explain select * from test where b = "1";

-- all 扫全表

explain select * from test where c = "1";

-- ref

explain select * from test where a = "1" and b = "1" and c = "1";

-- ref

explain select * from test where a = "1" and b = "1";

-- ref

explain select * from test where a = "1" and c = "1"

-- all 扫全表

explain select * from test where b = "1" and c = "1"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值