關於 in與exist , not in與not exist 的區別
- 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連線
相關文章
- SQL語句中not in 和not exist的區別SQL
- SQL not exist out joinSQL
- nginx關於root與alias的區別Nginx
- 關於C與C++的區別C++
- Android關於buildToolVersion與CompileSdkVersion的區別AndroidUICompile
- Waring: /dev/centos/swap does not existdevCentOS
- sql case when, Exist ,group by ,聚合SQL
- Property [title] does not exist on this collection instance
- PSQLexception: ERROR : type "signed" does not existSQLExceptionError
- Excutors 與 ThreadPoolExcutor 的關係與區別thread
- Docker與containerd的關係與區別DockerAI
- Python 關於TCP簡介以及與UDP的區別PythonTCPUDP
- Java中類與物件的關係與區別Java物件
- [20180917]關於分析函式的range與rows的區別.txt函式
- Property 'context' does not exist on type 'NodeRequire'.ts(2339)ContextUI
- Laravel Class env does not exist 問題排查Laravel
- 關於C++中字串輸入get與getline的區別C++字串
- 關於call, apply, bind方法的區別與內部實現APP
- WebSocket於HTTP 、WebSocket與Socket的區別WebHTTP
- template might not exist or might not be accessible by any of the configured Template Resolvers
- 115 svn: URL 'svn://132.232.108.38/think' doesn't exist
- Setup had an error Error: At least one of these paths should existErrorAST
- ??與?:的區別
- 教你如何用SQLite 實現if not exist 類似功能的操作SQLite
- ORA-04043: object DBA_DATA_FILES does not existObject
- [20211231]ORA-01418 specified index does not exist.txtIndex
- CV關於Mysql中ON與Where區別問題詳解buaMySql
- mac scrcpy 報錯 does not exist or is not a regular file,ERROR: Server connection failed(scrcpy mac與極空間衝突)MacErrorServerAI
- SQL與NoSQL(關係型與非關係型)資料庫的區別SQL資料庫
- final與static關鍵字的區別?(skycto JEEditor)
- 理解cookie、session、localStorage、sessionStorage的關係與區別CookieSession
- dcat-admin 表單 Field type [autocomplete] does not exist.
- cookie與session的區別與聯絡CookieSession
- Session與Cookie的區別與聯絡SessionCookie
- JRE與JDK的區別與聯絡JDK
- const與static的區別
- HTTP 與 HTTPS 的區別HTTP
- getAttribute() 與 attr() 的區別
- @import與<link> 的區別Import