<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Prepared Quizzes, How Would You Answer that Question? 3</title>
	<atom:link href="http://hoopercharles.wordpress.com/2011/01/13/prepared-quizzes-how-would-you-answer-that-question-3/feed/" rel="self" type="application/rss+xml" />
	<link>http://hoopercharles.wordpress.com/2011/01/13/prepared-quizzes-how-would-you-answer-that-question-3/</link>
	<description>Miscellaneous Random Oracle Topics: Stop, Think, ... Understand</description>
	<lastBuildDate>Mon, 13 May 2013 14:10:06 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Charles Hooper</title>
		<link>http://hoopercharles.wordpress.com/2011/01/13/prepared-quizzes-how-would-you-answer-that-question-3/#comment-2672</link>
		<dc:creator><![CDATA[Charles Hooper]]></dc:creator>
		<pubDate>Thu, 13 Jan 2011 18:11:19 +0000</pubDate>
		<guid isPermaLink="false">http://hoopercharles.wordpress.com/?p=4259#comment-2672</guid>
		<description><![CDATA[Hi Maxim,

I am thinking that optimization must be specific to 11g R2, although I am not sure that I recall testing it on 11g R2.  A brief test case table:
[code]
CREATE TABLE T1(
  C1 NUMBER,
  C2 VARCHAR2(200),
  PRIMARY KEY(C1));
 
INSERT INTO
  T1
SELECT
  ROWNUM C1,
  LPAD(&#039;A&#039;,200,&#039;A&#039;) C2
FROM
  DUAL
CONNECT BY
  LEVEL&lt;=1000000;
 
COMMIT;
 
EXEC DBMS_STATS.GATHER_TABLE_STATS(OWNNAME=&gt;USER,TABNAME=&gt;&#039;T1&#039;,CASCADE=&gt;TRUE)
[/code]
 
Test case script:
[code]
SELECT
  C1,
  SUBSTR(C2,1,10) C2
FROM
  T1
WHERE
  C1=10000;
 
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(NULL,NULL,&#039;TYPICAL&#039;));
 
SELECT
  C1,
  SUBSTR(C2,1,10) C2
FROM
  T1
WHERE
  NVL(C1,0)=10000;
 
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(NULL,NULL,&#039;TYPICAL&#039;));
 
SELECT /*+ INDEX(T1) */
  C1,
  SUBSTR(C2,1,10) C2
FROM
  T1
WHERE
  NVL(C1,0)=10000;
 
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(NULL,NULL,&#039;TYPICAL&#039;));
[/code]

This is the output that I receive on Oracle Database 10.2.0.2 (very similar results were seen on 11.1.0.7):
[code]
SQL_ID  gywbrs9kx94v7, child number 0
-------------------------------------
SELECT   C1,   SUBSTR(C2,1,10) C2 FROM   T1 WHERE   C1=10000
 
Plan hash value: 1462557378
 
--------------------------------------------------------------------------------------------
&#124; Id  &#124; Operation                   &#124; Name         &#124; Rows  &#124; Bytes &#124; Cost (%CPU)&#124; Time     &#124;
--------------------------------------------------------------------------------------------
&#124;   0 &#124; SELECT STATEMENT            &#124;              &#124;       &#124;       &#124;     3 (100)&#124;          &#124;
&#124;   1 &#124;  TABLE ACCESS BY INDEX ROWID&#124; T1           &#124;     1 &#124;   205 &#124;     3   (0)&#124; 00:00:01 &#124;
&#124;*  2 &#124;   INDEX UNIQUE SCAN         &#124; SYS_C0020654 &#124;     1 &#124;       &#124;     2   (0)&#124; 00:00:01 &#124;
--------------------------------------------------------------------------------------------
 
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - access(&quot;C1&quot;=10000)
 
----
 
SQL_ID  b6g33508zt1nz, child number 0
-------------------------------------
SELECT   C1,   SUBSTR(C2,1,10) C2 FROM   T1 WHERE   NVL(C1,0)=10000
 
Plan hash value: 3617692013
 
--------------------------------------------------------------------------
&#124; Id  &#124; Operation         &#124; Name &#124; Rows  &#124; Bytes &#124; Cost (%CPU)&#124; Time     &#124;
--------------------------------------------------------------------------
&#124;   0 &#124; SELECT STATEMENT  &#124;      &#124;       &#124;       &#124;  1507 (100)&#124;          &#124;
&#124;*  1 &#124;  TABLE ACCESS FULL&#124; T1   &#124;     1 &#124;   205 &#124;  1507   (5)&#124; 00:00:08 &#124;
--------------------------------------------------------------------------
 
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter(NVL(&quot;C1&quot;,0)=10000)
 
----
 
SQL_ID  43fdysjbtscam, child number 0
-------------------------------------
SELECT /*+ INDEX(T1) */   C1,   SUBSTR(C2,1,10) C2 FROM   T1 WHERE   NVL(C1,0)=10000
 
Plan hash value: 4057798311
 
-------------------------------------------------------------------------------------------
&#124; Id  &#124; Operation                   &#124; Name         &#124; Rows  &#124; Bytes &#124; Cost (%CPU)&#124; Time
-------------------------------------------------------------------------------------------
&#124;   0 &#124; SELECT STATEMENT            &#124;              &#124;       &#124;       &#124;  1919 (100)&#124;
&#124;   1 &#124;  TABLE ACCESS BY INDEX ROWID&#124; T1           &#124;     1 &#124;   205 &#124;  1919   (3)&#124; 00:00:10
&#124;*  2 &#124;   INDEX FULL SCAN           &#124; SYS_C0020654 &#124;     1 &#124;       &#124;  1918   (3)&#124; 00:00:10
-------------------------------------------------------------------------------------------
 
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - filter(NVL(&quot;C1&quot;,0)=10000)
[/code]

The unhinted execution plan from 11.1.0.7:
[code]
SQL_ID  b6g33508zt1nz, child number 0
-------------------------------------
SELECT   C1,   SUBSTR(C2,1,10) C2 FROM   T1 WHERE   NVL(C1,0)=10000

Plan hash value: 3617692013
 
--------------------------------------------------------------------------
&#124; Id  &#124; Operation         &#124; Name &#124; Rows  &#124; Bytes &#124; Cost (%CPU)&#124; Time     &#124;
--------------------------------------------------------------------------
&#124;   0 &#124; SELECT STATEMENT  &#124;      &#124;       &#124;       &#124;  3543 (100)&#124;          &#124;
&#124;*  1 &#124;  TABLE ACCESS FULL&#124; T1   &#124;     1 &#124;   205 &#124;  3543   (2)&#124; 00:00:15 &#124;
--------------------------------------------------------------------------
 
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter(NVL(&quot;C1&quot;,0)=10000)
[/code]

The index hinted execution plan (note that the calculated cost is lower):
[code]
SQL_ID  43fdysjbtscam, child number 0
-------------------------------------
SELECT /*+ INDEX(T1) */   C1,   SUBSTR(C2,1,10) C2 FROM   T1 WHERE
NVL(C1,0)=10000

Plan hash value: 3968247418

--------------------------------------------------------------------------------------------
&#124; Id  &#124; Operation                   &#124; Name         &#124; Rows  &#124; Bytes &#124; Cost (%CPU)&#124; Time     &#124;
--------------------------------------------------------------------------------------------
&#124;   0 &#124; SELECT STATEMENT            &#124;              &#124;       &#124;       &#124;  1909 (100)&#124;          &#124;
&#124;   1 &#124;  TABLE ACCESS BY INDEX ROWID&#124; T1           &#124;     1 &#124;   205 &#124;  1909   (2)&#124; 00:00:08 &#124;
&#124;*  2 &#124;   INDEX FULL SCAN           &#124; SYS_C0017730 &#124;     1 &#124;       &#124;  1908   (2)&#124; 00:00:08 &#124;
--------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------
   2 - filter(NVL(&quot;C1&quot;,0)=10000)
[/code]

I will have to test 11.2.0.1/11.2.0.2 later to see if I am able to reproduce the behavior - thanks for the tip.]]></description>
		<content:encoded><![CDATA[<p>Hi Maxim,</p>
<p>I am thinking that optimization must be specific to 11g R2, although I am not sure that I recall testing it on 11g R2.  A brief test case table:</p>
<pre class="brush: plain; title: ; notranslate">
CREATE TABLE T1(
  C1 NUMBER,
  C2 VARCHAR2(200),
  PRIMARY KEY(C1));
 
INSERT INTO
  T1
SELECT
  ROWNUM C1,
  LPAD('A',200,'A') C2
FROM
  DUAL
CONNECT BY
  LEVEL&lt;=1000000;
 
COMMIT;
 
EXEC DBMS_STATS.GATHER_TABLE_STATS(OWNNAME=&gt;USER,TABNAME=&gt;'T1',CASCADE=&gt;TRUE)
</pre>
<p>Test case script:</p>
<pre class="brush: plain; title: ; notranslate">
SELECT
  C1,
  SUBSTR(C2,1,10) C2
FROM
  T1
WHERE
  C1=10000;
 
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(NULL,NULL,'TYPICAL'));
 
SELECT
  C1,
  SUBSTR(C2,1,10) C2
FROM
  T1
WHERE
  NVL(C1,0)=10000;
 
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(NULL,NULL,'TYPICAL'));
 
SELECT /*+ INDEX(T1) */
  C1,
  SUBSTR(C2,1,10) C2
FROM
  T1
WHERE
  NVL(C1,0)=10000;
 
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(NULL,NULL,'TYPICAL'));
</pre>
<p>This is the output that I receive on Oracle Database 10.2.0.2 (very similar results were seen on 11.1.0.7):</p>
<pre class="brush: plain; title: ; notranslate">
SQL_ID  gywbrs9kx94v7, child number 0
-------------------------------------
SELECT   C1,   SUBSTR(C2,1,10) C2 FROM   T1 WHERE   C1=10000
 
Plan hash value: 1462557378
 
--------------------------------------------------------------------------------------------
| Id  | Operation                   | Name         | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |              |       |       |     3 (100)|          |
|   1 |  TABLE ACCESS BY INDEX ROWID| T1           |     1 |   205 |     3   (0)| 00:00:01 |
|*  2 |   INDEX UNIQUE SCAN         | SYS_C0020654 |     1 |       |     2   (0)| 00:00:01 |
--------------------------------------------------------------------------------------------
 
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - access(&quot;C1&quot;=10000)
 
----
 
SQL_ID  b6g33508zt1nz, child number 0
-------------------------------------
SELECT   C1,   SUBSTR(C2,1,10) C2 FROM   T1 WHERE   NVL(C1,0)=10000
 
Plan hash value: 3617692013
 
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |       |       |  1507 (100)|          |
|*  1 |  TABLE ACCESS FULL| T1   |     1 |   205 |  1507   (5)| 00:00:08 |
--------------------------------------------------------------------------
 
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter(NVL(&quot;C1&quot;,0)=10000)
 
----
 
SQL_ID  43fdysjbtscam, child number 0
-------------------------------------
SELECT /*+ INDEX(T1) */   C1,   SUBSTR(C2,1,10) C2 FROM   T1 WHERE   NVL(C1,0)=10000
 
Plan hash value: 4057798311
 
-------------------------------------------------------------------------------------------
| Id  | Operation                   | Name         | Rows  | Bytes | Cost (%CPU)| Time
-------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |              |       |       |  1919 (100)|
|   1 |  TABLE ACCESS BY INDEX ROWID| T1           |     1 |   205 |  1919   (3)| 00:00:10
|*  2 |   INDEX FULL SCAN           | SYS_C0020654 |     1 |       |  1918   (3)| 00:00:10
-------------------------------------------------------------------------------------------
 
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - filter(NVL(&quot;C1&quot;,0)=10000)
</pre>
<p>The unhinted execution plan from 11.1.0.7:</p>
<pre class="brush: plain; title: ; notranslate">
SQL_ID  b6g33508zt1nz, child number 0
-------------------------------------
SELECT   C1,   SUBSTR(C2,1,10) C2 FROM   T1 WHERE   NVL(C1,0)=10000

Plan hash value: 3617692013
 
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |       |       |  3543 (100)|          |
|*  1 |  TABLE ACCESS FULL| T1   |     1 |   205 |  3543   (2)| 00:00:15 |
--------------------------------------------------------------------------
 
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter(NVL(&quot;C1&quot;,0)=10000)
</pre>
<p>The index hinted execution plan (note that the calculated cost is lower):</p>
<pre class="brush: plain; title: ; notranslate">
SQL_ID  43fdysjbtscam, child number 0
-------------------------------------
SELECT /*+ INDEX(T1) */   C1,   SUBSTR(C2,1,10) C2 FROM   T1 WHERE
NVL(C1,0)=10000

Plan hash value: 3968247418

--------------------------------------------------------------------------------------------
| Id  | Operation                   | Name         | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |              |       |       |  1909 (100)|          |
|   1 |  TABLE ACCESS BY INDEX ROWID| T1           |     1 |   205 |  1909   (2)| 00:00:08 |
|*  2 |   INDEX FULL SCAN           | SYS_C0017730 |     1 |       |  1908   (2)| 00:00:08 |
--------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------
   2 - filter(NVL(&quot;C1&quot;,0)=10000)
</pre>
<p>I will have to test 11.2.0.1/11.2.0.2 later to see if I am able to reproduce the behavior &#8211; thanks for the tip.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Maxim</title>
		<link>http://hoopercharles.wordpress.com/2011/01/13/prepared-quizzes-how-would-you-answer-that-question-3/#comment-2671</link>
		<dc:creator><![CDATA[Maxim]]></dc:creator>
		<pubDate>Thu, 13 Jan 2011 13:05:06 +0000</pubDate>
		<guid isPermaLink="false">http://hoopercharles.wordpress.com/?p=4259#comment-2671</guid>
		<description><![CDATA[Thank you for that - i was not aware of this kind of optimization in 11gR2 ( maybe 11.1 - can&#039;t test it at the moment)
- if an indexed column is not nullable, the index range/unique scan can be used for nvl(column,value), as optimizer probably understand
now, the null can&#039;t occur anyway.

Best regards

Maxim]]></description>
		<content:encoded><![CDATA[<p>Thank you for that &#8211; i was not aware of this kind of optimization in 11gR2 ( maybe 11.1 &#8211; can&#8217;t test it at the moment)<br />
- if an indexed column is not nullable, the index range/unique scan can be used for nvl(column,value), as optimizer probably understand<br />
now, the null can&#8217;t occur anyway.</p>
<p>Best regards</p>
<p>Maxim</p>
]]></content:encoded>
	</item>
</channel>
</rss>
