關於 in與exist , not in與not exist 的區別
吃奶的牛發表於2016-09-27
- in和exists
- in 是把外表和內表作hash 連線,而exists是對外表作loop迴圈,每次loop迴圈再對內表進行查詢。一直以來認為exists比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 extsts 的子查詢依然能用到表上的索引。所以無論那個表大,用not exists都比not in要快。
-
-
-
- not in 邏輯上不完全等同於not exists,如果你誤用了not in,小心你的程式存在致命的BUG:
-
- 請看下面的例子:
- create table t1 (c1 number,c2 number);
- create table t2 (c1 number,c2 number);
-
- insert into t1 values (1,2);
- insert into t1 values (1,3);
- insert into t2 values (1,2);
- insert into t2 values (1,null);
-
- select * from t1 where c2 not in (select c2 from t2);
- no rows found
- select * from t1 where not exists (select 1 from t2 where t1.c2=t2.c2);
- c1 c2
- 1 3
-
- 正如所看到的,not in 出現了不期望的結果集,存在邏輯錯誤。如果看一下上述兩個select語句的執行計劃,也會不同。後者使用了hash_aj。
- 因此,請儘量不要使用not in(它會呼叫子查詢),而儘量使用not exists(它會呼叫關聯子查詢)。如果子查詢中返回的任意一條記錄含有空值,則查詢將不返回任何記錄,正如上面例子所示。
- 除非子查詢欄位有非空限制,這時可以使用not in ,並且也可以通過提示讓它使用hasg_aj或merge_aj連線
相關文章
- in與exist , not in與not exist 的區別
2013-12-05
- SQL語句中not in 和not exist的區別
2018-09-15
SQL - Oracle in and exist
2007-12-12
Oracle - 關於HashSet與TreeSet的區別與聯絡
2017-07-12
- 關於重定向符>>與>的區別與作用
2012-11-29
- nginx關於root與alias的區別
2019-01-19
Nginx - 關於C與C++的區別
2018-07-17
C++ - 關於JSF與Struts的區別
2007-01-01
JS - oracle exists and not exist
2011-12-01
Oracle - SQL not exist out join
2019-03-15
SQL - jquery judge element exist
2016-12-01
jQuery - asmcmd does not exist in directory
2012-11-29
ASM - 關於String與StringBuffer的區別
2016-01-05
- 關於rman裡面的from 與until的區別
2009-12-09
- 關於MySQL與SQLLite的GroupBy排序原理的區別
2014-08-07
MySql排序 - javax.media does not exist
2003-08-06
Java - 'mysql.column_stats' doesn't exist and Table 'mysql.index_stats' doesn't exist
2016-02-18
MySqlIndex - 啟動OEM時關於ora-00942:table or view does not exist的處理
2007-05-20
View - Android關於buildToolVersion與CompileSdkVersion的區別
2018-09-18
AndroidUICompile - Excutors 與 ThreadPoolExcutor 的關係與區別
2024-03-22
thread - sql case when, Exist ,group by ,聚合
2024-03-15
SQL - Python 關於TCP簡介以及與UDP的區別
2022-08-06
PythonTCPUDP - Java中類與物件的關係與區別
2018-04-12
Java物件 - exist-in和關聯子查詢-非關聯子查詢
2011-11-09
- Property [title] does not exist on this collection instance
2020-07-09
- Waring: /dev/centos/swap does not exist
2019-04-15
devCentOS - PatchObject constructor:Input file does not exist
2013-08-27
ObjectStruct - Oracle集合的first, last , next ,count,exist
2013-10-31
OracleAST - WebSocket於HTTP 、WebSocket與Socket的區別
2018-08-30
WebHTTP - 關聯關係與依賴關係的區別
2015-10-11
- &與&&, |與||區別
2016-05-01
- ??與?:的區別
2021-04-27
- 關於call, apply, bind方法的區別與內部實現
2019-01-03
APP - 關於 log_archive_dest與log_archive_dest_n a的區別
2010-07-24
Hive - PSQLexception: ERROR : type "signed" does not exist
2022-08-04
SQLExceptionError - Laravel Class env does not exist 問題排查
2021-10-21
Laravel - SNMP TABLE ERROR : Requested table is empty or does not exist
2014-10-20
Error - ORA-00942: table or view does not exist
2012-10-19
View