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的效能
考慮如下SQL語句
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 < count(A); $i++) {
$a = get_record(A, $i); #從A表逐條擷取記錄
if (B.id = $a[id]) #如果子條件成立
$result[] = $a;
}
return $result;
大概就是這麼個意思,其實可以看到,查詢1主要是用到了B表的索引,A表如何對查詢的效率影響應該不大
假設B表的所有id為1,2,3,查詢2可以轉換為
select * from A where A.id = 1 or A.id = 2 or A.id = 3; (這種等效在大部分資料庫中成立,但是在MySQL中不是這樣)
這個好理解了,這裡主要是用到了A的索引,B表如何對查詢影響不大
MySQL中的IN(N個資料)會先被資料庫排序,然後使用二分尋找在N個資料的資料中尋找與M個表中資料相匹配的值,拋開索引不說,他的效率是要logN層級的,要比直接使用
id=1 or id=2 or id=3 的效率要高.
下面再看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是個範圍查詢,這種!=的範圍查詢無法使用任何索引,等於說A表的每條記錄,都要在B表裡遍曆一次,查看B表裡是否存在這條記錄
故not exists比not in效率高