<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>惜分飞 &#187; not in</title>
	<atom:link href="https://www.xifenfei.com/tag/not-in/feed" rel="self" type="application/rss+xml" />
	<link>https://www.xifenfei.com</link>
	<description>提供7*24专业数据库(Oracle,SQL Server,MySQL,PostgreSQL等)恢复和技术支持@Tel:17813235971</description>
	<lastBuildDate>Sun, 25 Oct 2026 13:02:42 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	
	<item>
		<title>_optimizer_null_aware_antijoin和not in效率</title>
		<link>https://www.xifenfei.com/2017/06/_optimizer_null_aware_antijoin-not-in.html</link>
		<comments>https://www.xifenfei.com/2017/06/_optimizer_null_aware_antijoin-not-in.html#comments</comments>
		<pubDate>Sun, 04 Jun 2017 05:26:22 +0000</pubDate>
		<dc:creator><![CDATA[惜分飞]]></dc:creator>
				<category><![CDATA[Oracle性能优化]]></category>
		<category><![CDATA[not in]]></category>
		<category><![CDATA[_optimizer_null_aware_antijoin]]></category>

		<guid isPermaLink="false">https://www.xifenfei.com/?p=7128</guid>
		<description><![CDATA[联系：手机/微信(+86 17813235971) QQ(107644445)  &#8230; <a href="https://www.xifenfei.com/2017/06/_optimizer_null_aware_antijoin-not-in.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<div id="xifenfeicopyright">
<p>联系：<a href="https://www.xifenfei.com/about_xifenfei" target="_blank">手机/微信(+86 17813235971)   QQ(107644445)</a><a target="_blank" href="http://wpa.qq.com/msgrd?v=3&#038;uin=107644445&#038;site=qq&#038;menu=yes"><img src="https://www.xifenfei.com/wp-content/themes/img/site_qq.jpg" alt="QQ咨询惜分飞" title="QQ咨询惜分飞"/></a></p>
<p>标题：<a href="https://www.xifenfei.com/2017/06/_optimizer_null_aware_antijoin-not-in.html" target="_blank">_optimizer_null_aware_antijoin和not in效率</a></p>
<p>作者：<a href="https://www.xifenfei.com" target="_blank">惜分飞</a>©版权所有[未经本人同意,不得以任何形式转载,否则有进一步追究法律责任的权利.]</p>
</div>
<p><strong>准备两个测试表</strong></p>
<pre class="brush: sql; title: ; notranslate">
SQL&gt; conn chf/oracle
Connected.
SQL&gt; select * from v$version;

BANNER
----------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
PL/SQL Release 11.2.0.4.0 - Production
CORE    11.2.0.4.0      Production
TNS for Linux: Version 11.2.0.4.0 - Production
NLSRTL Version 11.2.0.4.0 - Production

SQL&gt; create table t_xifenfei
  2  as select * from dba_objects;

Table created.

SQL&gt; select count(*) from t_xifenfei;

  COUNT(*)
----------
     86259

SQL&gt; create table t_xifenfei1
  2  as select * from dba_objects;

Table created.

SQL&gt; select count(*) from t_xifenfei1;

  COUNT(*)
----------
     86260

--删除部分记录，用来做not in的内部表
SQL&gt; delete from t_xifenfei where object_id&gt;86200;

918 rows deleted.

SQL&gt; commit;

Commit complete.
</pre>
<p><strong>查询_optimizer_null_aware_antijoin隐含参数默认值</strong></p>
<pre class="brush: sql; title: ; notranslate">
SQL&gt; conn / as sysdba
Connected.
SQL&gt; col name for a52
SQL&gt; col value for a24
SQL&gt; col description for a50
set linesize 150
select a.ksppinm name,b.ksppstvl value,a.ksppdesc description
  from x$ksppi a,x$ksppcv b
 where a.inst_id = USERENV ('Instance')
   and b.inst_id = USERENV ('Instance')
   and a.indx = b.indx
   and upper(a.ksppinm) LIKE upper('%&amp;param%')
order by name
SQL&gt; SQL&gt;   2    3    4    5    6    7    8  /
Enter value for param: _optimizer_null_aware_antijoin
old   6:    and upper(a.ksppinm) LIKE upper('%&amp;param%')
new   6:    and upper(a.ksppinm) LIKE upper('%_optimizer_null_aware_antijoin%')

NAME                                                 VALUE                    DESCRIPTION
---------------------------------------------------- ------------------------ -----------------------------
_optimizer_null_aware_antijoin                       TRUE                     null-aware antijoin parameter
</pre>
<p>_optimizer_null_aware_antijoin从11.1.0.6开始引进,默认为true<br />
<br />
<strong>_optimizer_null_aware_antijoin为true,执行not in</strong></p>
<pre class="brush: sql; title: ; notranslate">
SQL&gt; conn chf/oracle 
Connected.
SQL&gt; set autot trace
SQL&gt; set timing on
SQL&gt; set lines 150
SQL&gt; set pages 1000
SQL&gt;  select count(*) from t_xifenfei1 where object_id not in(select object_id from t_xifenfei);

Elapsed: 00:00:00.09

Execution Plan
----------------------------------------------------------
Plan hash value: 4048525918

----------------------------------------------------------------------------------------
| Id  | Operation                | Name        | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT         |             |     1 |    10 |   688   (1)| 00:00:09 |
|   1 |  SORT AGGREGATE          |             |     1 |    10 |            |          |
|*  2 |   HASH JOIN RIGHT ANTI NA|             |  1137 | 11370 |   688   (1)| 00:00:09 |
|   3 |    TABLE ACCESS FULL     | T_XIFENFEI  | 85341 |   416K|   344   (1)| 00:00:05 |
|   4 |    TABLE ACCESS FULL     | T_XIFENFEI1 | 86260 |   421K|   344   (1)| 00:00:05 |
----------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access(&quot;OBJECT_ID&quot;=&quot;OBJECT_ID&quot;)


Statistics
----------------------------------------------------------
          1  recursive calls
          0  db block gets
       2472  consistent gets
          0  physical reads
          0  redo size
        527  bytes sent via SQL*Net to client
        520  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed
</pre>
<p><strong>_optimizer_null_aware_antijoin为false,执行not in</strong></p>
<pre class="brush: sql; title: ; notranslate">
SQL&gt; alter session set &quot;_optimizer_null_aware_antijoin&quot;=false;

Session altered.

Elapsed: 00:00:00.00
SQL&gt; select count(*) from t_xifenfei1 where object_id not in(select object_id from t_xifenfei);

Elapsed: 00:02:29.64

Execution Plan
----------------------------------------------------------
Plan hash value: 2503880249

-----------------------------------------------------------------------------------
| Id  | Operation           | Name        | Rows  | Bytes | Cost (%CPU)| Time     |
-----------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |             |     1 |     5 |    25M  (1)| 86:20:57 |
|   1 |  SORT AGGREGATE     |             |     1 |     5 |            |          |
|*  2 |   FILTER            |             |       |       |            |          |
|   3 |    TABLE ACCESS FULL| T_XIFENFEI1 | 86260 |   421K|   344   (1)| 00:00:05 |
|*  4 |    TABLE ACCESS FULL| T_XIFENFEI  |     1 |     5 |   344   (1)| 00:00:05 |
-----------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - filter( NOT EXISTS (SELECT 0 FROM &quot;T_XIFENFEI&quot; &quot;T_XIFENFEI&quot; WHERE
              LNNVL(&quot;OBJECT_ID&quot;&lt;&gt;:B1)))
   4 - filter(LNNVL(&quot;OBJECT_ID&quot;&lt;&gt;:B1))


Statistics
----------------------------------------------------------
          0  recursive calls
          0  db block gets
   52982891  consistent gets
          0  physical reads
          0  redo size
        527  bytes sent via SQL*Net to client
        520  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed
</pre>
<p>这里很明显,当 _optimizer_null_aware_antijoin为false的时候not in效率非常低(当in里面记录多，使用FILTER效率肯定低下).<br />
<br />
<strong>_optimizer_null_aware_antijoin为false,执行not exists</strong></p>
<pre class="brush: sql; title: ; notranslate">
SQL&gt; alter session set &quot;_optimizer_null_aware_antijoin&quot;=false;

Session altered.

SQL&gt;select count(*) from t_xifenfei1 b where not exists 
 2  (select 1 from t_xifenfei a where a.object_id=b.object_id);

Elapsed: 00:00:00.07

Execution Plan
----------------------------------------------------------
Plan hash value: 2976307246

-------------------------------------------------------------------------------------
| Id  | Operation             | Name        | Rows  | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT      |             |     1 |    10 |   688   (1)| 00:00:09 |
|   1 |  SORT AGGREGATE       |             |     1 |    10 |            |          |
|*  2 |   HASH JOIN RIGHT ANTI|             |  1137 | 11370 |   688   (1)| 00:00:09 |
|   3 |    TABLE ACCESS FULL  | T_XIFENFEI  | 85341 |   416K|   344   (1)| 00:00:05 |
|   4 |    TABLE ACCESS FULL  | T_XIFENFEI1 | 86260 |   421K|   344   (1)| 00:00:05 |
-------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access(&quot;A&quot;.&quot;OBJECT_ID&quot;=&quot;B&quot;.&quot;OBJECT_ID&quot;)


Statistics
----------------------------------------------------------
          0  recursive calls
          0  db block gets
       2472  consistent gets
          0  physical reads
          0  redo size
        527  bytes sent via SQL*Net to client
        520  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed
</pre>
<p>对于11g的版本可以通过_optimizer_null_aware_antijoin参数开启NULL-aware Anti join特性来提高not in的效率,对于11g以下版本可以通过not exists来提高效率</p>
<div id="crp_related"><ul><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2012/04/%e6%89%a7%e8%a1%8c%e8%ae%a1%e5%88%92%e4%b8%ad%e5%b8%b8%e8%a7%81index%e8%ae%bf%e9%97%ae%e6%96%b9%e5%bc%8f.html" rel="bookmark" class="crp_title">执行计划中常见index访问方式</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2012/12/%e5%88%9b%e5%bb%ba%e6%94%af%e6%8c%81null-index.html" rel="bookmark" class="crp_title">创建包含null值index</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2013/04/12c-top-n-sql%e5%ae%9e%e7%8e%b0%e5%88%86%e9%a1%b5%e5%8a%9f%e8%83%bd.html" rel="bookmark" class="crp_title">ORACLE 12C TOP N SQL实现分页功能</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2015/05/oracle-12c-table-access-by-index-rowid-batched.html" rel="bookmark" class="crp_title">Oracle 12C TABLE ACCESS BY INDEX ROWID BATCHED</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2011/07/select-maxamina-from-table%e4%bc%98%e5%8c%96.html" rel="bookmark" class="crp_title">select max(id),min(id) from table优化</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2012/05/bind-variable-peeking-%e6%b5%8b%e8%af%95.html" rel="bookmark" class="crp_title">Bind Variable Peeking 测试</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2014/03/%e5%90%abis-null-sql%e8%af%ad%e5%8f%a5%e4%bc%98%e5%8c%96.html" rel="bookmark" class="crp_title">含is null sql语句优化</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2012/06/autotrace%e6%98%be%e7%a4%bastatistics%e5%be%88%e5%a4%9a%e4%bf%a1%e6%81%af%e4%b8%ba0.html" rel="bookmark" class="crp_title">autotrace显示Statistics很多信息为0</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2012/05/sql-profile-%e4%bd%bf%e7%94%a8.html" rel="bookmark" class="crp_title">sql profile 使用</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2012/04/hint%e6%8c%87%e5%ae%9aindex%e7%9a%84%e6%b7%b1%e5%85%a5%e7%90%86%e8%a7%a3.html" rel="bookmark" class="crp_title">hint指定index的深入理解</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2014/10/11g%e4%b8%ad-connect-by-%e8%af%ad%e5%8f%a5%e6%89%a7%e8%a1%8c%e8%ae%a1%e5%88%92%e6%94%b9%e5%8f%98.html" rel="bookmark" class="crp_title">11g中 connect by 语句执行计划改变</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2011/09/oracle%e7%9b%b4%e6%96%b9%e5%9b%be%e7%90%86%e8%a7%a3%e4%b8%8e%e5%ae%9e%e9%aa%8c.html" rel="bookmark" class="crp_title">Oracle直方图理解与实验</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>https://www.xifenfei.com/2017/06/_optimizer_null_aware_antijoin-not-in.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
