mysql中exists與in的使用

2021-08-09 13:03:20 字數 2931 閱讀 8465

exists對外表用loop逐條查詢,每次查詢都會檢視exists的條件語句,當exists裡的條件語句能夠返回記錄行時(無論記錄行是多少,只要能返回),條件就為真,返回當前loop到的這條記錄,反之如果exists裡的條件語句不能返回記錄行,則當前loop到的這條記錄被丟棄,exists的條件就像乙個bool條件,當能返回結果集則為true,不能返回結果集則為false

如下:select * from user where exists(select 1);

對user表的記錄逐條取出,由於子條件中的select 1永遠能返回記錄行,那麼user表的所有記錄都將被加入結果集,所以與select * from user;是一樣的

又如下select * from user where exists(select * from user where userid = 0);

可以知道對user進行loop時,檢查條件語句(select * from user where userid = 0),由於userid永遠不為0,所以條件永遠為false,那麼user表的所有記錄都將被丟棄

not exists與exists相反,也就是當exists條件有結果返回時,loop到的記錄將被丟棄,否則將loop到的記錄加入結果集

總的來說,如果a表有n條記錄,那麼exists查詢就是將這n條資料逐條取出,然後判斷n遍exists條件

in查詢相當於多個or條件的疊加

如下:select * from user where userid in (1,2,3);

等效於select * from user where userid =1 or userid = 2 or userid = 3;

not in與in相反,如下

select * from user where userid not in(1,2,3);

等效於select * from user where userid !=1 and userid !=2 and userid !=3;

總的來說,in查詢就是先將子查詢條件的記錄全部查出來,假設結果集為b,共有m條記錄,然後在將子查詢條件結果分解成m個,再進行m次查詢

值得一提的是,in查詢的子條件返回結果必須只有乙個字段,例如

select * from user where userid in(select id from b);

而不能是

select * from user where userid in(select id,age from b);

而exists就沒有這個限制

下面來考慮exists和in的效能

如下:1,select * from a where exists (select * from b where b.id = a.id)

2,select * from a where a.id in(select id from b);

查詢1轉化為偽**:

for($i=0;$i可以看到,查詢1主要用到的是b表的索引,a表如何對查詢的效率影響應該不大

假設b表的所有id為1,2,3,產尋2可以轉化為

select * from a where a.id = 1 or a.id = 2 or a.id = 3;

這裡主要用到了a的索引,b表如何對查詢影響不大

再看not exists和not in

1,select * from a where not exists (select * from b where b.id = a.id);

2,select * from a where a.id not in(select id from b);

查詢1,還是用了b的索引

查詢2,可以轉換為

select * from a where a.id !=1 and a.id !=2 and a.id != 3;

可以知道not in是個範圍查詢,這種!=的範圍查詢無法使用任何索引,等於說b表的每條記錄,都要在a表裡遍歷一次,檢視a表裡是存在這條記錄

故not exists比not in效率高

如果查詢的兩個表大小相當,那麼用in和exists差別不大

如果兩個表中乙個較小,乙個是大表,則子查詢表大的用exists,子查詢錶小的用in:

例如:表a(小表),表b(大表)

1:select * from a where cc in(select cc from b)效率低,用到了a表上的cc列索引;

select * from a where exists(select cc from b where cc=a.cc)效率高,用到了b表上的cc列索引

2:select * from b where cc in(select cc from a)效率高,用到了b表上cc列索引

select * from b where exists(select cc from a where cc=b.cc)效率低,用到了a表上cc列索引

not in和not exists

如果查詢語句使用了not in那麼內外表都進行全表掃瞄,沒有用到索引;而not exists的子查詢依然能用到表上的索引

所以無論那個表大,not exists都比not in要快

in與=的區別

select name from student where name in('zhang','wang','li');

與select name from student where name='zhang' or name='li' or name='wang';

的結果是相同

MySQL中exists與in的使用

exists對外表用loop逐條查詢,每次查詢都會檢視exists的條件語句,當 exists裡的條件語句能夠返回記錄行時 無論記錄行是的多少,只要能返回 條件就為真,返回當前loop到的這條記錄,反之如果exists裡的條 件語句不能返回記錄行,則當前loop到的這條記錄被丟棄,exists的條件...

MySQL中exists與in的使用

總結 當涉及到外表和子查詢時,要考慮到exists和in 的選擇 exists的子句是乙個boolean條件,這個子句當能返回結果集則為true,不能返回結果集則為 false,子句為真所的記錄會被加入結果集,子句為假所的記錄被拋棄結果集為空 in查詢相當於多個or條件的疊加,in查詢就是先將子查詢...

mysql中in與exists效率比較

這條語句適用於a錶比b表大的情況 select from ecs goods a where cat id in select cat id from ecs category 這條語句適用於b錶比a表大的情況 select from ecs goods a where exists select ...