<?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; is null 优化</title>
	<atom:link href="https://www.xifenfei.com/tag/is-null-%e4%bc%98%e5%8c%96/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>Thu, 29 Oct 2026 15:31:46 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	
	<item>
		<title>含is null sql语句优化</title>
		<link>https://www.xifenfei.com/2014/03/%e5%90%abis-null-sql%e8%af%ad%e5%8f%a5%e4%bc%98%e5%8c%96.html</link>
		<comments>https://www.xifenfei.com/2014/03/%e5%90%abis-null-sql%e8%af%ad%e5%8f%a5%e4%bc%98%e5%8c%96.html#comments</comments>
		<pubDate>Fri, 14 Mar 2014 11:37:24 +0000</pubDate>
		<dc:creator><![CDATA[惜分飞]]></dc:creator>
				<category><![CDATA[Oracle性能优化]]></category>
		<category><![CDATA[is null 优化]]></category>

		<guid isPermaLink="false">https://www.xifenfei.com/?p=5199</guid>
		<description><![CDATA[联系：手机/微信(+86 17813235971) QQ(107644445)  &#8230; <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">继续阅读 <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/2014/03/%e5%90%abis-null-sql%e8%af%ad%e5%8f%a5%e4%bc%98%e5%8c%96.html" target="_blank">含is null sql语句优化</a></p>
<p>作者：<a href="https://www.xifenfei.com" target="_blank">惜分飞</a>©版权所有[未经本人同意,不得以任何形式转载,否则有进一步追究法律责任的权利.]</p>
</div>
<p><strong>原sql语句与执行计划</strong></p>
<pre class="brush: sql; title: ; notranslate">
SQL&gt; set autot trace
SQL&gt; WITH AL AS (SELECT * FROM XIFENFEI_LOG WHERE CLEAR_TIME IS NULL) 
   2 SELECT SWP.ID SWP_ID, AL.* FROM AL FULL OUTER JOIN XIFENFEI_LOG_SWAP SWP ON SWP.ID = AL.ID;

54 rows selected.


Execution Plan
----------------------------------------------------------
Plan hash value: 888046630

----------------------------------------------------------------------------------------------------------
| Id  | Operation                  | Name                        | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT           |                             |    24 | 11064 | 24658   (2)| 00:04:56 |
|   1 |  TEMP TABLE TRANSFORMATION |                             |       |       |            |     |
|   2 |   LOAD AS SELECT           |                             |       |       |            |     |
|*  3 |    TABLE ACCESS FULL       | XIFENFEI_LOG                |    23 |  2576 | 24652   (2)| 00:04:56 |
|   4 |   VIEW                     |                             |    24 | 11064 |     6  (17)| 00:00:01 |
|   5 |    UNION-ALL               |                             |       |       |            |     |
|   6 |     NESTED LOOPS OUTER     |                             |    23 | 10465 |     2   (0)| 00:00:01 |
|   7 |      VIEW                  |                             |    23 | 10304 |     2   (0)| 00:00:01 |
|   8 |       TABLE ACCESS FULL    | SYS_TEMP_0FD9D6605_51B4E691 |    23 |  2576 |     2   (0)| 00:00:01 |
|*  9 |      INDEX UNIQUE SCAN     | XIFENFEI_LOG_SWP_PK         |     1 |     7 |     0   (0)| 00:00:01 |
|* 10 |     HASH JOIN ANTI         |                             |     1 |    20 |     4  (25)| 00:00:01 |
|  11 |      INDEX FULL SCAN       | XIFENFEI_LOG_SWP_PK         |    20 |   140 |     1   (0)| 00:00:01 |
|  12 |      VIEW                  |                             |    23 |   299 |     2   (0)| 00:00:01 |
|  13 |       TABLE ACCESS FULL    | SYS_TEMP_0FD9D6605_51B4E691 |    23 |  2576 |     2   (0)| 00:00:01 |
----------------------------------------------------------------------------------------------------------


Predicate Information (identified by operation id):
---------------------------------------------------

   3 - filter(&quot;CLEAR_TIME&quot; IS NULL)
   9 - access(&quot;SWP&quot;.&quot;ID&quot;(+)=&quot;AL&quot;.&quot;ID&quot;)
  10 - access(&quot;SWP&quot;.&quot;ID&quot;=&quot;AL&quot;.&quot;ID&quot;)


Statistics
----------------------------------------------------------
          2  recursive calls
          8  db block gets
     111504  consistent gets
          1  physical reads
        692  redo size
       8075  bytes sent via SQL*Net to client
        502  bytes received via SQL*Net from client
          5  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
         54  rows processed
</pre>
<p>这里很明显占用资源多，执行时间长的都在XIFENFEI_LOG表的全表扫描上,而该表的where 条件是CLEAR_TIME is null.<br />
<br /> <br />
<strong>分析CLEAR_TIME 列null值的分布</strong></p>
<pre class="brush: sql; title: ; notranslate">
SQL&gt; SELECT count(*) FROM XIFENFEI_LOG WHERE CLEAR_TIME IS NULL;

  COUNT(*)
----------
        48

SQL&gt; SELECT count(*) FROM XIFENFEI_LOG WHERE CLEAR_TIME IS not NULL;

  COUNT(*)
----------
   6899211
</pre>
<p>通过这里分析可以知道,CLEAR_TIME is null的值非常少,如果能够创建一个index,取到CLEAR_TIME 列null的值,那效率将非常搞.但是有oracle index知识的人都知道,B树index是不包含null列,因此一般性index无法满足该需求.这里思考创建含常数的复合index,而且把CLEAR_TIME放在前面,因为后面的常数一定存在,因此CLEAR_TIME中含有null的记录也就包含在该复合index中.<br />
<br />
<strong>创建含常数复合index</strong></p>
<pre class="brush: sql; title: ; notranslate">
SQL&gt; create index ind_XIFENFEI_LOG_null on XIFENFEI_LOG (CLEAR_TIME,0) online;

Index created.
</pre>
<p><strong>再次查看执行计划</strong></p>
<pre class="brush: sql; title: ; notranslate">
SQL&gt; WITH AL AS (SELECT * FROM XIFENFEI_LOG WHERE CLEAR_TIME IS NULL) 
  2  SELECT SWP.ID SWP_ID, AL.* FROM AL FULL OUTER JOIN XIFENFEI_LOG_SWAP SWP ON SWP.ID = AL.ID;

50 rows selected.


Execution Plan
----------------------------------------------------------
Plan hash value: 2359331571

-------------------------------------------------------------------------------------------------------------
| Id  | Operation                     | Name                        | Rows  | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT              |                             |    24 | 11064 |    25   (4)| 00:00:01 |
|   1 |  TEMP TABLE TRANSFORMATION    |                             |       |       |            |          |
|   2 |   LOAD AS SELECT              |                             |       |       |            |          |
|   3 |    TABLE ACCESS BY INDEX ROWID| XIFENFEI_LOG                |    23 |  2576 |    19   (0)| 00:00:01 |
|*  4 |     INDEX RANGE SCAN          | IND_XIFENFEI_LOG_NULL       |    23 |       |     3   (0)| 00:00:01 |
|   5 |   VIEW                        |                             |    24 | 11064 |     6  (17)| 00:00:01 |
|   6 |    UNION-ALL                  |                             |       |       |            |          |
|   7 |     NESTED LOOPS OUTER        |                             |    23 | 10465 |     2   (0)| 00:00:01 |
|   8 |      VIEW                     |                             |    23 | 10304 |     2   (0)| 00:00:01 |
|   9 |       TABLE ACCESS FULL       | SYS_TEMP_0FD9D660D_51B4E691 |    23 |  2576 |     2   (0)| 00:00:01 |
|* 10 |      INDEX UNIQUE SCAN        | XIFENFEI_LOG_SWP_PK         |     1 |     7 |     0   (0)| 00:00:01 |
|* 11 |     HASH JOIN ANTI            |                             |     1 |    20 |     4  (25)| 00:00:01 |
|  12 |      INDEX FULL SCAN          | XIFENFEI_LOG_SWP_PK         |    20 |   140 |     1   (0)| 00:00:01 |
|  13 |      VIEW                     |                             |    23 |   299 |     2   (0)| 00:00:01 |
|  14 |       TABLE ACCESS FULL       | SYS_TEMP_0FD9D660D_51B4E691 |    23 |  2576 |     2   (0)| 00:00:01 |
-------------------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   4 - access(&quot;CLEAR_TIME&quot; IS NULL)
  10 - access(&quot;SWP&quot;.&quot;ID&quot;(+)=&quot;AL&quot;.&quot;ID&quot;)
  11 - access(&quot;SWP&quot;.&quot;ID&quot;=&quot;AL&quot;.&quot;ID&quot;)


Statistics
----------------------------------------------------------
          2  recursive calls
          8  db block gets
         33  consistent gets
          1  physical reads
        648  redo size
       7688  bytes sent via SQL*Net to client
        502  bytes received via SQL*Net from client
          5  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
         50  rows processed
</pre>
<p>这里可以发现,该sql使用了创建的含常数的复合index,sql执行时间从4分56秒,提高到现在的1秒钟,逻辑读从当初的111504减小到现在的33,巧用含常数的复合索引使得sql执行效率极大提高.</p>
<div id="crp_related"><ul><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/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/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/2017/06/_optimizer_null_aware_antijoin-not-in.html" rel="bookmark" class="crp_title">_optimizer_null_aware_antijoin和not in效率</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/2011/09/10g%e6%96%b0%e5%a2%9e%e5%88%97%e6%96%b9%e5%bc%8f%e6%8c%87%e5%ae%9ahint.html" rel="bookmark" class="crp_title">10g新增列方式指定HINT</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2013/08/oracle-12c-partial-globallocal-indexes-for-partitioned-tables.html" rel="bookmark" class="crp_title">ORACLE 12C Partial Global/Local Indexes for Partitioned Tables</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/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/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/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/2015/01/%e5%8d%87%e7%ba%a7%e6%95%b0%e6%8d%ae%e5%ba%93%e5%88%b010-2-0-5%e9%81%ad%e9%81%87ora-00918-column-ambiguously-defined.html" rel="bookmark" class="crp_title">升级数据库到10.2.0.5遭遇ORA-00918: column ambiguously defined</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>https://www.xifenfei.com/2014/03/%e5%90%abis-null-sql%e8%af%ad%e5%8f%a5%e4%bc%98%e5%8c%96.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
