1、Using join buffer (Block Nested Loop)
- 例如A表 Join B表,如TYPE類型是ALL或Index時候,則可以使用連接緩存(Join Buffer)
- 官方示例代碼:
for each row in t1 matching range { for each row in t2 matching reference key { store used columns from t1, t2 in join buffer if buffer is full { for each row in t3 { for each t1, t2 combination in join buffer { if row satisfies join conditions, send to client } } empty join buffer } } } if buffer is not empty { for each row in t3 { for each t1, t2 combination in join buffer { if row satisfies join conditions, send to client } } }
2、索引join
- 如果為連接列增加索引,則會通過索引匹配,而不需要到表里掃描
- 比如A表JoinB表,A表為驅動表的情況下,如果B表的Join列有普通索引,則在外層循環(A表記錄),里層循環(B表記錄),替換里層循環的對象為索引,由於是普通索引,所以需要回表查詢(相當於拿到主鍵ID之后還需要去遍歷主鍵索引的B+樹)
- 如果上述的B表(非驅動表)的Join列是主鍵的話,則速度會更快,因為直接通過主鍵索引獲取到數據,不需要再回表查詢
3、普通join(A表外層循環,B表內層循環,挨個匹配)
如果啥都沒有,那只能挨個去匹配了,但一般會用第一種。