MySQL數(shù)據(jù)庫回表與索引怎么理解

本篇內(nèi)容介紹了“MySQL數(shù)據(jù)庫回表與索引怎么理解”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

成都創(chuàng)新互聯(lián)公司成立與2013年,先為臨武等服務建站,臨武等地企業(yè),進行企業(yè)商務咨詢服務。為臨武企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務解決您的所有建站問題。

回表的概念

先得出結論,根據(jù)下面的實驗。如果我要獲得['liu','25']這條記錄。需要什么步驟。

  • 1.先通過['liu']記錄對應到普通索引index(name),獲取到主鍵id:4.

  • 2.再通過clustered index,定位到行記錄。也就是上面說的這條['liu','25']記錄數(shù)據(jù)。

因此,上述就是說的回表查詢,先定位主鍵值,再定位行記錄。多掃了一遍索引樹。

當然,也就多耗費了CPU,IO,內(nèi)存等。

1.stu_info表案例

create table stu_info (  id int primary key,  name varchar(20),  age int,  index(name) )

2.查看剛剛建立的表結構

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.插入測試數(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.分析過程

我們來分析這幾條數(shù)據(jù)的索引。由于我們name這個列建立了索引。所以name索引存儲會按照【a~z】順序排列。通過select語句,可以得到一些感性認識。如下:

mysql> select name from stu_info; +-----------+ | name      | +-----------+ | allen     | | benjiemin | | huang     | | liu       | | peter     | | roger     | | zhang     | +-----------+

上述的普通索引secondary index在B+樹存儲格式可能如下:

根據(jù)舊金山大學提供的可視化B+tree的效果。

如下圖:

MySQL數(shù)據(jù)庫回表與索引怎么理解

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

MySQL數(shù)據(jù)庫回表與索引怎么理解

也能看到name這幾個數(shù)據(jù)建立的B+樹是怎么樣的。也能看到我需要找到[liu]這個元素的話,需要兩次查找。

但是,如果我的需求是,除了獲取name之外還需要獲取age的話。這里就需要回表了。為什么?因為我找不到age數(shù)據(jù)。

  • 普通索引的葉子節(jié)點,只存主鍵。

那么clustered index聚集索引是如何保存的呢?繼續(xù)使用上述可視化工具,再分析一波。

MySQL數(shù)據(jù)庫回表與索引怎么理解

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

MySQL數(shù)據(jù)庫回表與索引怎么理解

所以,name='liu'查詢liu的年齡,是需要回表的。首先查詢普通索引的B+樹,再查詢聚集索引的B+樹。最后得到liu的那條行記錄。

5.執(zhí)行計劃

我們也可以通過執(zhí)行計劃來分析一下,如下:

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的話,那么。就不需要回表。如下實驗,繼續(xù)看執(zhí)行計劃:

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的話。我們可以通過建立組合索引來解決這個問題。通過

ALTER TABLE stu_info DROP INDEX  name;   alter table stu_info add key(name,age);

我們再繼續(xù)看執(zhí)行計劃,如下:

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也就沒有用到回表了。

“MySQL數(shù)據(jù)庫回表與索引怎么理解”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識可以關注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

新聞標題:MySQL數(shù)據(jù)庫回表與索引怎么理解
文章鏈接:http://muchs.cn/article34/ghecpe.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、網(wǎng)站設計公司網(wǎng)站導航、網(wǎng)站營銷ChatGPT、企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應式網(wǎng)站建設