Oracle in and exist
複習一下in&exist語法:
一般來講in是對外表和內表作Hash Join,而exist是對外表和內表做了一個nested loop,也就是說,對於exist,針對外表(需要遍歷其所有內容)需要遍歷的每一行,都會對內表進行一次查詢,因此如果外表和內表大小相當,in和exist在效能的差別上就不是很大:)
如果兩個表中一個是較小的表,一個是較大的表,如果內表大則用exists效能會更好(因為外表小了,遍歷的行次數就少了很多),內表表小的用in效能會更好。
舉個例子:
例如:表A(小表),表B(大表),CC列上有索引
在外表小,內表大的情況下用in(Hash Join A和B兩個表).效率就會很低,比如:
select * from A where cc in (select cc from B)
這個時候查詢用到了A表上cc列的索引,但是因為A是小表,而且大表B上的索引也沒有充分利用,因此效率不高
而如果用了exist,我們發現走的是對A,B兩個表的nested Loop,對於遍歷A的每一行,都會對大表B進行一次查詢(可能對B表的查詢結果不是太多.)
select * from A where exists(select cc from B where cc=A.cc)
這樣效率會高些,因為用到了B表(大表)上cc列的索引。
not in 和not exists
如果查詢語句使用了not in 那麼內外表都進行全表掃描,沒有用到索引;
而not extsts 的子查詢依然能用到表上的索引。
所以無論那個表大,用not exists都比not in要快。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12361284/viewspace-451/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- oracle exists and not existOracle
- Oracle集合的first, last , next ,count,existOracleAST
- in與exist , not in與not exist 的區別
- oracle for linux安裝報錯 file /home/oracle/.Xauthority does not existOracleLinux
- Oracle not exist子查詢全掃的優化Oracle優化
- 【Oracle】-【許可權-ORA-04043】- object does not existOracleObject
- 關於 in與exist , not in與not exist 的區別
- aix Oracle 使用者報錯:Too many processes already existAIOracle
- SQL not exist out joinSQL
- jquery judge element existjQuery
- asmcmd does not exist in directoryASM
- 靜默安裝Oracle建庫時報Template General Purpose does not existOracle
- Oracle ORA - 01720 grant option does not exist for..報錯解決Oracle
- javax.media does not existJava
- 'mysql.column_stats' doesn't exist and Table 'mysql.index_stats' doesn't existMySqlIndex
- oracle11g:Prvf-0042: /usr/local/bin/ssh request by the client does not existOracleclient
- sql case when, Exist ,group by ,聚合SQL
- Property [title] does not exist on this collection instance
- Waring: /dev/centos/swap does not existdevCentOS
- PatchObject constructor:Input file does not existObjectStruct
- SQL語句中not in 和not exist的區別SQL
- PSQLexception: ERROR : type "signed" does not existSQLExceptionError
- Laravel Class env does not exist 問題排查Laravel
- SNMP TABLE ERROR : Requested table is empty or does not existError
- ORA-00942: table or view does not existView
- which situation IN is better than exist, and vice versa.
- FAQ:Field DATABASE does not exist; see long textDatabase
- Oracle RAC的ORA-12545 Connect failed because target host or object does not exist問題OracleAIObject
- relation with OID 637165 does not exist
- SQLite中中實現 if not exist 類似功能SQLite
- MYSQL ERROR 1146 Table doesnt exist 解析MySqlError
- about Res folder doesn't exist in android projectAndroidProject
- ORA-04043: object DBA_DATA_FILES does not existObject
- Setup had an error Error: At least one of these paths should existErrorAST
- ORA-04042 procedure, function, package, or package body does not existFunctionPackage
- ORA-24756: transaction does not exist問題解決
- PL/SQL: ORA-00942: table or view does not existSQLView
- ORA-29857: domain indexes and/or secondary objects exist in the tablespaceAIIndexObject