MySQL資料庫回表與索引
阿新 • • 發佈:2020-05-19
[TOC]
## 回表的概念
先得出結論,根據下面的實驗。如果我要獲得['liu','25']這條記錄。需要什麼步驟。
* 1.先通過['liu']記錄對應到普通索引`index(name)`,獲取到主鍵id:4.
* 2.再通過`clustered index`,定位到行記錄。也就是上面說的這條['liu','25']記錄資料。
因此,上述就是說的**回表查詢**,先定位主鍵值,再定位行記錄。多掃了一遍索引樹。
當然,也就多耗費了CPU,IO,記憶體等。
### 1.stu_info表案例
```sql
create table stu_info (
id int primary key,
name varchar(20),
age int,
index(name)
)
```
### 2.檢視剛剛建立的表結構
```sql
mysql> show create table stu_info\G;
*************************** 1. row ***************************
Table: stu_info
Create Table: CREATE TABLE `stu_info` (
`id` int(11) NOT NULL,
`name` varchar(20) COLLATE utf8_bin DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
1 row in set (0.00 sec)
```
### 3.插入測試資料
```sql
insert into stu_info values(1,'zhang',20);
insert into stu_info values(4,'liu',25);
insert into stu_info values(7,'huang',19);
insert into stu_info values(10,'allen',27);
insert into stu_info values(30,'benjiemin',27);
insert into stu_info values(16,'roger',27);
insert into stu_info values(28,'peter',16);
commit
```
### 4.分析過程
我們來分析這幾條資料的索引。由於我們name這個列建立了索引。所以name索引儲存會按照【a~z】順序排列。通過select語句,可以得到一些感性認識。如下:
```sql
mysql> select name from stu_info;
+-----------+
| name |
+-----------+
| allen |
| benjiemin |
| huang |
| liu |
| peter |
| roger |
| zhang |
+-----------+
```
上述的普通索引`secondary index`在B+樹儲存格式可能如下:
根據舊金山大學提供的視覺化B+tree的效果。
其視覺化地址為:https://www.cs.usfca.edu/~galles/visualization/BPlusTree.html
如下圖:
![](https://img2020.cnblogs.com/blog/1398629/202005/1398629-20200519161215912-1377119692.png)
我在根據上面的圖,畫一個自己的。如下圖所示:
![](https://img2020.cnblogs.com/blog/1398629/202005/1398629-20200519161229408-223145422.png)
也能看到name這幾個資料建立的B+樹是怎麼樣的。也能看到我需要找到[liu]這個元素的話,需要兩次查詢。
但是,如果我的需求是,除了獲取name之外還需要獲取age的話。這裡就需要**回表**了。為什麼?因為我找不到**age**資料。
* 普通索引的葉子節點,只存主鍵。
那麼`clustered index`聚集索引是如何儲存的呢?繼續使用上述視覺化工具,再分析一波。
![](https://img2020.cnblogs.com/blog/1398629/202005/1398629-20200519161344204-1546600670.png)
上圖是聚集索引的示意圖。轉化為我的圖如下:
![](https://img2020.cnblogs.com/blog/1398629/202005/1398629-20200519161400273-1179030519.png)
所以,name='liu'查詢liu的年齡,是需要回表的。首先查詢普通索引的B+樹,再查詢聚集索引的B+樹。最後得到liu的那條行記錄。
### 5.執行計劃
我們也可以通過**執行計劃**來分析以下。如下:
```sql
mysql> explain select id,name,age from stu_info where name='liu'\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: stu_info
type: ref
possible_keys: name
key: name
key_len: 63
ref: const
rows: 1
Extra: Using index condition
1 row in set (0.00 sec)
```
看到`Using index condition`,我們這裡用到了回表。
如果不取age,只取`id和name`的話,那麼。就不需要回表。如下實驗,繼續看執行計劃:
```sql
mysql> explain select id,name from stu_info where name='liu'\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: stu_info
type: ref
possible_keys: name
key: name
key_len: 63
ref: const
rows: 1
Extra: Using where; Using index
1 row in set (0.00 sec)
```
那麼,如果我們不想回表,不想多做IO的話。我們可以通過建立組合索引來解決這個問題。通過
```sql
ALTER TABLE stu_info DROP INDEX name;
alter table stu_info add key(name,age);
```
我們再繼續看執行計劃,如下:
```sql
mysql> explain select name,age from stu_info where name='liu'\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: stu_info
type: ref
possible_keys: name
key: name
key_len: 63
ref: const
rows: 1
Extra: Using where; Using index
1 row in set (0.00 sec)
```
可以看到額外資訊是`Using where; Using index`而不是`Using index condition`也就沒有用到回