MySQL創(chuàng)建index提高多表查詢效率
將模型簡單化,假設(shè)有三個(gè)表:tblA, tblB, tblC. 每個(gè)表包含三列:col1, col2, col3. 表的其它屬性不考慮。
在不創(chuàng)建index的情況下,我們使用以下語句關(guān)聯(lián)三個(gè)表:
- SELECT
- *
- FROM
- tblA, 5 tblB, 6 tblC 7 WHERE
- tblA.col1 = tblB.col1 9 AND tblA.col2 = tblC.col1;
對(duì)該語句使用EXPLAIN命令查看其處理情況:
+-------+------+---------------+------+---------+------+------+-------------+ | table | type | possible_keys | key | key_len | ref | rows | Extra | +-------+------+---------------+------+---------+------+------+-------------+ | tblA | ALL | NULL | NULL | NULL | NULL | 1000 | | | tblB | ALL | NULL | NULL | NULL | NULL | 1000 | Using where | | tblC | ALL | NULL | NULL | NULL | NULL | 1000 | Using where | +-------+------+---------------+------+---------+------+------+-------------+
[http://www.cnitblog.com/aliyiyi08/archive/2008/09/09/48878.html]
查詢機(jī)制
對(duì)于命令的查詢機(jī)制,可以參照下MySQL manual(7.2.1)中的一段說明:
The tables are listed in the output in the order that MySQL would read them while processing the query. MySQL resolves all joins using a single-sweep multi-join method. This means that MySQL reads a row from the first table, then finds a matching row in the second table, then in the third table, and so on. When all tables are processed, MySQL outputs the selected columns and backtracks through the table list until a table is found for which there are more matching rows. The next row is read from this table and the process continues with the next table.
正如上面所說,MySQL按照tblA, tblB, tblC的順序依次讀取數(shù)據(jù),從EXPLAIN輸出的信息結(jié)構(gòu)看,之前的表查詢的數(shù)據(jù)用來查找當(dāng)前表對(duì)應(yīng)的內(nèi)容。即用tblA的值來查找tblB中滿足條件的值,tblB的值用來查找tblC中滿足條件的值。而當(dāng)一次查找完成時(shí)(即三個(gè)表的值都查找過一次),MySQL并不會(huì)重新返回到tblA中的下一個(gè)數(shù)據(jù)重新開始,而是繼續(xù)返回到tblB中的數(shù)據(jù),來看tblB中是否還有其它行的值和tblA相匹配,如果有的話,繼續(xù)到tblC,重復(fù)剛才的過程。這整個(gè)過程的關(guān)鍵原則就是:使用前一個(gè)表查詢的數(shù)據(jù)來查找當(dāng)前表對(duì)應(yīng)的內(nèi)容。
了解到MySQL在執(zhí)行多表查詢時(shí)使用前一個(gè)表查詢的數(shù)據(jù)來查找當(dāng)前表對(duì)應(yīng)的內(nèi)容這一原理后,那么創(chuàng)建Index的目的就是告訴MySQL怎么去直接找到下一個(gè)表的對(duì)應(yīng)數(shù)據(jù),如何按照MySQL需要的數(shù)據(jù)順序來關(guān)聯(lián)(JOIN)一個(gè)表。
再拿剛才的例子,tblA和tblB兩個(gè)表通過條件 ”tblA.col1 = tblB.col1” 關(guān)聯(lián)起來。我們首先獲得tblA.col1,接下來MySQL需要的是來自tblB.col1的值,所以我們?yōu)樗鼊?chuàng)建INDEX tblB.col1. 創(chuàng)建index后再次EXPLAIN之前的查詢命令如下:
+-------+------+---------------+----------+---------+-----------+------+-------------+ | table | type | possible_keys | key | key_len | ref | rows | Extra | +-------+------+---------------+----------+---------+-----------+------+-------------+ | tblA | ALL | NULL | NULL | NULL | NULL | 1000 | | | tblB | ref | ndx_col1 | ndx_col1 | 5 | tblA.col1 | 1 | Using where | | tblC | ALL | NULL | NULL | NULL | NULL | 1000 | Using where | +-------+------+---------------+----------+---------+-----------+------+-------------+
從結(jié)果中可以看出,MySQL現(xiàn)在使用key ‘ndx_col1’來關(guān)聯(lián)tblB到tblA。也就是說,當(dāng)MySQL查找tblB中的各行數(shù)據(jù)時(shí),它直接使用key ‘ndx_col1’ 對(duì)應(yīng)的tblA.col1來找到對(duì)應(yīng)的行,而不是像之前那樣進(jìn)行全表掃描查找。
例子
舉一個(gè)實(shí)例說明用法
其中USING選擇的參數(shù),要求是每個(gè)表所共有且在每個(gè)表中值不重復(fù),以保證index***。
join (PRIMARY)中PRIMARY參數(shù)為Index名,
表的屬性中,作為index需要將參數(shù)勾選PK屬性,即Primary Key。
勾選telnum作為主鍵,需要將Default值中默認(rèn)的NULL刪除,PRIMARY_KEY不允許包含NULL值。
為每一個(gè)表創(chuàng)建了Index值后,EXPLAIN輸出為:
對(duì)于MySQL,不管多復(fù)雜的查詢,每次只需要按照EXPLAIN顯示的順序關(guān)聯(lián)兩張表中的內(nèi)容。創(chuàng)建Index是為了讓MySQL能夠利用已經(jīng)查找到的內(nèi)容來快速找到下一張表的對(duì)應(yīng)行內(nèi)容。
原文鏈接:http://www.cnblogs.com/dwayne/archive/2012/07/06/MySQL_index_join_wayne.html
【編輯推薦】