回表的概念
先得出結(jié)論,根據(jù)下面的實(shí)驗(yàn)。如果我要獲得['liu','25']這條記錄。需要什么步驟。
- 1.先通過['liu']記錄對(duì)應(yīng)到普通索引index(name),獲取到主鍵id:4.
- 2.再通過clustered index,定位到行記錄。也就是上面說(shuō)的這條['liu','25']記錄數(shù)據(jù)。
因此,上述就是說(shuō)的回表查詢,先定位主鍵值,再定位行記錄。多掃了一遍索引樹。
當(dāng)然,也就多耗費(fèi)了CPU,IO,內(nèi)存等。
1.stu_info表案例
- create table stu_info (
- id int primary key,
- name varchar(20),
- age int,
- index(name)
- )
2.查看剛剛建立的表結(jié)構(gòu)
- 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.插入測(cè)試數(shù)據(jù)
- 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.分析過程
我們來(lái)分析這幾條數(shù)據(jù)的索引。由于我們name這個(gè)列建立了索引。所以name索引存儲(chǔ)會(huì)按照【a~z】順序排列。通過select語(yǔ)句,可以得到一些感性認(rèn)識(shí)。如下:
- mysql> select name from stu_info;
- +-----------+
- | name |
- +-----------+
- | allen |
- | benjiemin |
- | huang |
- | liu |
- | peter |
- | roger |
- | zhang |
- +-----------+
上述的普通索引secondary index在B+樹存儲(chǔ)格式可能如下:
根據(jù)舊金山大學(xué)提供的可視化B+tree的效果。
其可視化地址為:https://www.cs.usfca.edu/~galles/visualization/BPlusTree.html
如下圖:

我在根據(jù)上面的圖,畫一個(gè)自己的。如下圖所示:

也能看到name這幾個(gè)數(shù)據(jù)建立的B+樹是怎么樣的。也能看到我需要找到[liu]這個(gè)元素的話,需要兩次查找。
但是,如果我的需求是,除了獲取name之外還需要獲取age的話。這里就需要回表了。為什么?因?yàn)槲艺也坏?strong>age數(shù)據(jù)。
- 普通索引的葉子節(jié)點(diǎn),只存主鍵。
那么clustered index聚集索引是如何保存的呢?繼續(xù)使用上述可視化工具,再分析一波。

上圖是聚集索引的示意圖。轉(zhuǎn)化為我的圖如下:

所以,name='liu'查詢liu的年齡,是需要回表的。首先查詢普通索引的B+樹,再查詢聚集索引的B+樹。最后得到liu的那條行記錄。
5.執(zhí)行計(jì)劃
我們也可以通過執(zhí)行計(jì)劃來(lái)分析一下,如下:
- 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的話,那么。就不需要回表。如下實(shí)驗(yàn),繼續(xù)看執(zhí)行計(jì)劃:
- 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的話。我們可以通過建立組合索引來(lái)解決這個(gè)問題。通過
- ALTER TABLE stu_info DROP INDEX name;
- alter table stu_info add key(name,age);
我們?cè)倮^續(xù)看執(zhí)行計(jì)劃,如下:
- 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也就沒有用到回表了。