在咱們的工做中可能會遇到這樣的情形:html
咱們須要查詢a表裏面的數據,可是要以b表做爲約束。數據庫
舉個例子,好比咱們須要查詢訂單表中的數據,可是要以用戶表爲約束,也就是查詢出來的訂單的user_id要在用戶表裏面存在才返回。緩存
表結構和表數據以下:oop
table1 usertb;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(30) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
+----+-----------+
| id | name |
+----+-----------+
| 1 | panchao |
| 2 | tangping |
| 3 | yinkaiyue |
+----+-----------+post
table2 ordertb;
+------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | YES | | NULL | |
| order_name | varchar(50) | YES | | NULL | |
+------------+-------------+------+-----+---------+----------------+
+----+---------+-------------------+
| id | user_id | order_name |
+----+---------+-------------------+
| 1 | 1 | tangping's order |
| 2 | 2 | yinkaiyue's order |
| 3 | 0 | zhangtian's order |
+----+---------+-------------------+性能
看過表事後,你們在腦海中可能已經想出了不少方法了,對吧。url
主要三種方法:left join、in、exists。spa
咱們分別來看看。他們的查詢結果和explain的結果。htm
一、left join:blog
MariaDB [test]> select * from ordertb a left join usertb b on a.user_id = b.id;
+----+---------+-------------------+------+----------+
| id | user_id | order_name | id | name |
+----+---------+-------------------+------+----------+
| 1 | 1 | tangping's order | 1 | panchao |
| 2 | 2 | yinkaiyue's order | 2 | tangping |
| 3 | 0 | zhangtian's order | NULL | NULL |
+----+---------+-------------------+------+----------+
MariaDB [test]> explain select * from ordertb a left join usertb b on a.user_id= b.id;
+------+-------------+-------+--------+---------------+---------+---------+----------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------+--------+---------------+---------+---------+----------------+------+-------------+
| 1 | SIMPLE | a | ALL | NULL | NULL | NULL | NULL | 3 | |
| 1 | SIMPLE | b | eq_ref | PRIMARY | PRIMARY | 4 | test.a.user_id | 1 | Using where |
+------+-------------+-------+--------+---------------+---------+---------+----------------+------+-------------+
二、in:
MariaDB [test]> select * from ordertb where ordertb.user_id in (select id from usertb);
+----+---------+-------------------+
| id | user_id | order_name |
+----+---------+-------------------+
| 1 | 1 | tangping's order |
| 2 | 2 | yinkaiyue's order |
+----+---------+-------------------+
MariaDB [test]> explain select * from ordertb where ordertb.user_id in (select id from usertb);
+------+-------------+---------+--------+---------------+---------+---------+----------------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+---------+--------+---------------+---------+---------+----------------------+------+-------------+
| 1 | PRIMARY | ordertb | ALL | NULL | NULL | NULL | NULL | 3 | Using where |
| 1 | PRIMARY | usertb | eq_ref | PRIMARY | PRIMARY | 4 | test.ordertb.user_id | 1 | Using index |
+------+-------------+---------+--------+---------------+---------+---------+----------------------+------+-------------+
三、exists:
MariaDB [test]> select * from ordertb where exists(select 1 from usertb where usertb.id = ordertb.user_id);
+----+---------+-------------------+
| id | user_id | order_name |
+----+---------+-------------------+
| 1 | 1 | tangping's order |
| 2 | 2 | yinkaiyue's order |
+----+---------+-------------------+
MariaDB [test]> explain select * from ordertb where exists(select 1 from usertbwhere usertb.id = ordertb.user_id);
+------+-------------+---------+--------+---------------+---------+---------+----------------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+---------+--------+---------------+---------+---------+----------------------+------+-------------+
| 1 | PRIMARY | ordertb | ALL | NULL | NULL | NULL | NULL | 3 | Using where |
| 1 | PRIMARY | usertb | eq_ref | PRIMARY | PRIMARY | 4 | test.ordertb.user_id | 1 | Using index |
+------+-------------+---------+--------+---------------+---------+---------+----------------------+------+-------------+
咱們能夠看到,這三種查詢的explain結果大體相同,惟一不一樣的是left join中的Extra沒有用到Useing Where。說明left join相比於其餘兩個查詢效率要低一些,而且left join中有冗餘數據。
咱們再來看 in 和 exists ,從表面上來看好像xiaolv同樣。其實否則。咱們來深刻分析一下這兩個語句。
一、in。
其中usertb咱們用B來代替,ordertb咱們用A來代替。
in()只執行一次,它查出B表中的全部id字段並緩存起來.以後,檢查A表的user_id是否與B表中的id相等,若是相等則將A表的記錄加入結果集中,直到遍歷完A表的全部記錄. 它的查詢過程相似於如下過程
List resultSet=[]; Array A=(select * from A); Array B=(select id from B);
for(int i=0;i<A.length;i++) { for(int j=0;j<B.length;j++) { if(A[i].id==B[j].id) { resultSet.add(A[i]); break; } } } return resultSet;
能夠看出,當B表數據較大時不適合使用in(),由於它會B表數據所有遍歷一次. 如:A表有10000條記錄,B表有1000000條記錄,那麼最多有可能遍歷10000*1000000次,效率不好. 再如:A表有10000條記錄,B表有100條記錄,那麼最多有可能遍歷10000*100次,遍歷次數大大減小,效率大大提高.
二、exists。
exists()會執行A.length次,它並不緩存exists()結果集,由於exists()結果集的內容並不重要,重要的是結果集中是否有記錄,若是有則返回true,沒有則返回false. 它的查詢過程相似於如下過程
List resultSet=[]; Array A=(select * from A)
for(int i=0;i<A.length;i++) { if(exists(A[i].id) { //執行select 1 from B b where b.id=a.id是否有記錄返回 resultSet.add(A[i]); } } return resultSet;
當B表比A表數據大時適合使用exists(),由於它沒有那麼遍歷操做,只須要再執行一次查詢就行. 如:A表有10000條記錄,B表有1000000條記錄,那麼exists()會執行10000次去判斷A表中的id是否與B表中的id相等. 如:A表有10000條記錄,B表有100000000條記錄,那麼exists()仍是執行10000次,由於它只執行A.length次,可見B表數據越多,越適合exists()發揮效果. 再如:A表有10000條記錄,B表有100條記錄,那麼exists()仍是執行10000次,還不如使用in()遍歷10000*100次,由於in()是在內存裏遍歷比較,而exists()須要查詢數據庫,咱們都知道查詢數據庫所消耗的性能更高,而內存比較很快.
結論:exists()適合B表比A表數據大的狀況
當A表數據與B表數據同樣大時,in與exists效率差很少,可任選一個使用.
in 和 exists的區別:
若是子查詢得出的結果集記錄較少,主查詢中的表較大且又有索引時應該用in, 反之若是外層的主查詢記錄較少,子查詢中的表大,又有索引時使用exists。其實咱們區分in和exists主要是形成了驅動順序的改變(這是性能變化的關鍵),若是是exists,那麼之外層表爲驅動表,先被訪問,若是是IN,那麼先執行子查詢,因此咱們會以驅動表的快速返回爲目標,那麼就會考慮到索引及結果集的關係了 ,另外IN時不對NULL進行處理。
in 是把外表和內表做hash 鏈接,而exists是對外表做loop循環,每次loop循環再對內表進行查詢。一直以來認爲exists比in效率高的說法是不許確的。
更多細節,能夠參考如下博客(SQL語句中exists和in的區別),由於我也是看了這個博客寫的文章。