---
title: "MySQL索引最左匹配原则"
author: "fairy_xzc"
date: "2018-11-14T12:44:18Z"
tags: []
categories: ["数据库"]
slug: "mysql"
draft: false
lastmod: "2018-11-14T13:04:10Z"
id: 2433805
url: "https://agilestyle.iteye.com/blog/2433805"
---

**原创转载请注明出处：[http://agilestyle.iteye.com/blog/2433805](/blog/2433805)**

 

创建一个测试表

```
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;
```

![](http://dl2.iteye.com/upload/attachment/0131/0431/e5dc6e7b-0b64-3119-aa6e-d30948c95fcb.png)  
 

查看执行计划

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

![](http://dl2.iteye.com/upload/attachment/0131/0433/8b0564cd-a58e-3f52-b028-04fe9fdf00e4.png)  
 

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

![](http://dl2.iteye.com/upload/attachment/0131/0435/ae54df2f-774e-3c58-a532-8489bd976a5a.png)  
 

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

![](http://dl2.iteye.com/upload/attachment/0131/0437/fbd7e920-7cc8-343b-8680-f6316b04bf04.png)  
 

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

![](http://dl2.iteye.com/upload/attachment/0131/0439/1d54c5b6-1eed-3ecf-bd69-2af44592c9f8.png)  
 

创建索引（a和b的联合索引）

```
create index idx_a_b on test(a, b);
```

 

查看索引

```
show index from test;
```

![](http://dl2.iteye.com/upload/attachment/0131/0443/e70f5ce6-dd15-351a-9167-129c213062c3.png)  
 

查看执行计划

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

![](http://dl2.iteye.com/upload/attachment/0131/0445/aced8f7f-01fc-368b-90c6-20285de852b3.png)  
 

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

![](http://dl2.iteye.com/upload/attachment/0131/0447/76a36928-7d5b-3917-8702-a3d5d46b7671.png)  
 

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

![](http://dl2.iteye.com/upload/attachment/0131/0449/a2aeda78-869c-337b-812c-a1b87abc6e28.png)  
 

创建索引（b的索引）

```
create index idx_b on test(b);
```

查看索引

```
show index from test;
```

![](http://dl2.iteye.com/upload/attachment/0131/0451/aa0fd3b1-55f3-3c54-8358-ed21f164ea00.png)  
 

查看执行计划

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

![](http://dl2.iteye.com/upload/attachment/0131/0453/2d2b2e66-769c-3bea-ad7c-1f1d0fb4402e.png)  
 

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

![](http://dl2.iteye.com/upload/attachment/0131/0455/e93c443b-6f84-3ccb-956a-e18241e53a18.png)  
 

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

![](http://dl2.iteye.com/upload/attachment/0131/0457/63813400-55b7-3dde-b423-6d26707100c5.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"
```