'乾貨:MySQL性能優化,in和exists'

MySQL SQL 腳本語言 JAVA架構師之路 2019-09-06
"
"
乾貨:MySQL性能優化,in和exists

in和exists哪個性能更優

sql腳本:

"
乾貨:MySQL性能優化,in和exists

in和exists哪個性能更優

sql腳本:

乾貨:MySQL性能優化,in和exists

上面的sql中 訂單表中(orders) 存在user_id,而又有用戶表(users),所以我們用orders表中user_id和user表中的id 來in 和 exists。

結果

1.where後面是小表

(1)select count(1) from orders o where o.user_id in(select u.id from users u);

"
乾貨:MySQL性能優化,in和exists

in和exists哪個性能更優

sql腳本:

乾貨:MySQL性能優化,in和exists

上面的sql中 訂單表中(orders) 存在user_id,而又有用戶表(users),所以我們用orders表中user_id和user表中的id 來in 和 exists。

結果

1.where後面是小表

(1)select count(1) from orders o where o.user_id in(select u.id from users u);

乾貨:MySQL性能優化,in和exists

(2)select count(1) from orders o where exists (select 1 from users u where u.id = o.user_id);

"
乾貨:MySQL性能優化,in和exists

in和exists哪個性能更優

sql腳本:

乾貨:MySQL性能優化,in和exists

上面的sql中 訂單表中(orders) 存在user_id,而又有用戶表(users),所以我們用orders表中user_id和user表中的id 來in 和 exists。

結果

1.where後面是小表

(1)select count(1) from orders o where o.user_id in(select u.id from users u);

乾貨:MySQL性能優化,in和exists

(2)select count(1) from orders o where exists (select 1 from users u where u.id = o.user_id);

乾貨:MySQL性能優化,in和exists

2.where後面是大表

(1)select count(1) from users u where u.id in (select o.user_id from orders o);

"
乾貨:MySQL性能優化,in和exists

in和exists哪個性能更優

sql腳本:

乾貨:MySQL性能優化,in和exists

上面的sql中 訂單表中(orders) 存在user_id,而又有用戶表(users),所以我們用orders表中user_id和user表中的id 來in 和 exists。

結果

1.where後面是小表

(1)select count(1) from orders o where o.user_id in(select u.id from users u);

乾貨:MySQL性能優化,in和exists

(2)select count(1) from orders o where exists (select 1 from users u where u.id = o.user_id);

乾貨:MySQL性能優化,in和exists

2.where後面是大表

(1)select count(1) from users u where u.id in (select o.user_id from orders o);

乾貨:MySQL性能優化,in和exists

(2)select count(1) from users u where exists (select 1 from orders o where o.user_id = u.id);

"
乾貨:MySQL性能優化,in和exists

in和exists哪個性能更優

sql腳本:

乾貨:MySQL性能優化,in和exists

上面的sql中 訂單表中(orders) 存在user_id,而又有用戶表(users),所以我們用orders表中user_id和user表中的id 來in 和 exists。

結果

1.where後面是小表

(1)select count(1) from orders o where o.user_id in(select u.id from users u);

乾貨:MySQL性能優化,in和exists

(2)select count(1) from orders o where exists (select 1 from users u where u.id = o.user_id);

乾貨:MySQL性能優化,in和exists

2.where後面是大表

(1)select count(1) from users u where u.id in (select o.user_id from orders o);

乾貨:MySQL性能優化,in和exists

(2)select count(1) from users u where exists (select 1 from orders o where o.user_id = u.id);

乾貨:MySQL性能優化,in和exists

分析

我們用下面的這兩條語句分析:

select count(1) from orders o where o.user_id in(select u.id from users u); select count(1) from orders o where exists (select 1 from users u where u.id = o.user_id);

1.in:先查詢in後面的users表,然後再去orders中過濾,也就是先執行子查詢,結果出來後,再遍歷主查詢,遍歷主查詢是根據user_id和id相等查詢的。

即查詢users表相當於外層循環,主查詢就是外層循環

小結:in先執行子查詢,也就是in()所包含的語句。子查詢查詢出數據以後,將前面的查詢分為n次普通查詢(n表示在子查詢中返回的數據行數)

2.exists:主查詢是內層循環,先查詢出orders,查詢orders就是外層循環,然後會判斷是不是存在order_id和 users表中的id相等,相等才保留數據,查詢users表就是內層循環

這裡所說的外層循環和內層循環就是我們所說的嵌套循環,而嵌套循環應該遵循“外小內大”的原則,這就好比你複製很多個小文件和複製幾個大文件的區別

小結:如果子查詢查到數據,就返回布爾值true;如果沒有,就返回布爾值false。返回布爾值true則將該條數據保存下來,否則就捨棄掉。也就是說exists查詢,是查詢出一條數據就執行一次子查詢

結論

小表驅動大表。

in適合於外表大而內表小的情況,exists適合於外表小而內表大的情況。

粉絲福利,需獲取HashMap、分佈式、微服務、spring全家桶等最新相關架構資料

關注我+轉發此文+私信回覆關鍵詞:架構

"

相關推薦

推薦中...