Mysql InnoDB儲存引擎中 join原理

2021-08-20 08:36:06 字數 3105 閱讀 7724

本文翻譯自mysql效能調優及架構設計 優化部分。主要是做個記錄和增加學習的廣度。

實現演算法:

nested loop join。本質上和我們平時使用時的直觀感受一致,選定一張驅動表作為迴圈基礎資料,然後迴圈該結果集中的資料作為過濾條件到下一張表內查詢資料,然後合併結果。如果有多個join,則將前面的結果集作為迴圈條件,到後乙個表中查詢資料。

書中的例子:

select m.subject msg_subject, c.content msg_content  

from user_group g,group_message m,group_message_content c

where g.user_id = 1

and m.group_id = g.group_id

and c.group_msg_id = m.id

userid,group_id,group_msg_id都存在索引。

*************************** 1. row ***************************  

id: 1

select_type: ******

table: g

type: ref

possible_keys: user_group_gid_ind,user_group_uid_ind,user_group_gid_uid_ind

key: user_group_uid_ind

key_len: 4

ref: const

rows: 2

extra:

*************************** 2. row ***************************

id: 1

select_type: ******

table: m

type: ref

possible_keys: primary,idx_group_message_gid_uid

key: idx_group_message_gid_uid

key_len: 4

ref: g.group_id

rows: 3

extra:

*************************** 3. row ***************************

id: 1

select_type: ******

table: c

type: ref

possible_keys: idx_group_message_content_msg_id

key: idx_group_message_content_msg_id

key_len: 4

ref: m.id

rows: 2

extra:

偽**如下:

for each record g_rec in table user_group that g_rec.user_id=1

}

如果去掉group_msg_id欄位的索引,執行計畫就不一樣了。

*************************** 1. row ***************************  

id: 1

select_type: ******

table: g

type: ref

possible_keys: user_group_uid_ind

key: user_group_uid_ind

key_len: 4

ref: const

rows: 2

extra:

*************************** 2. row ***************************

id: 1

select_type: ******

table: m

type: ref

possible_keys: primary,idx_group_message_gid_uid

key: idx_group_message_gid_uid

key_len: 4

ref: g.group_id

rows: 3

extra:

*************************** 3. row ***************************

id: 1

select_type: ******

table: c

type: all

possible_keys: null

key: null

key_len: null

ref: null

rows: 96

extra:using where;using join buffer

最後一張表的索引刪除了,就只能全表掃瞄,因此使用到了join buffer,其實也就是快取。注意到,前兩張表的資料放到了快取內,然後通過這個快取再和第三張表進行查詢。這是因為第三張表需要全表掃瞄了,這時候沒有必要每一次都關聯。。。

for each record g_rec in table user_group

}flush_buffer()

empty the buffer;

}

join語句的優化:

1.小結果集驅動大結果集

2.優先優化內層迴圈。

3.對join欄位建立索引。

4.當無法建立索引時,需要注意join buffer size(暫時不清楚,沒遇到具體場景)

mysql InnoDB儲存引擎

innodb的組成部分 1.後台執行緒 2.儲存引擎記憶體池 innodb儲存引擎記憶體緩衝池 1.記憶體緩衝池 innodb是基於磁碟儲存的,並將其中的記錄按照頁的方式進行管理。因此,可將其視為基礎磁碟的資料庫系統。在資料庫系統中,由於cpu速度與磁碟速度之間的紅狗,基於磁碟的資料庫系統通常使用緩...

Mysql Innodb儲存引擎

鎖 參考資料 參考資料 行鎖 innodb的鎖是對索引加鎖,如果查詢到並沒有用到索引就會對錶進行加鎖 record lock 對單條記錄加上鎖 gap lock 間隙鎖,鎖定乙個範圍,但是不包含記錄本身 next key lock record lock gap lock,鎖定乙個方位並鎖定記錄本身...

MYSQL INNODB 儲存引擎

innodb 是事務安全的mysql儲存引擎,設計上採用了類似於oracle的架構。一般而言,在oltp的應用中,innodb應該作為核心應用表的首選儲存引擎。同時,也是因為innodb的存在,才使得mysql變得更有魅力。第一 innodb儲存引擎概述 innodb由innobase oy 公司開...