<?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: Subquery Factoring (4)</title>
	<atom:link href="http://jonathanlewis.wordpress.com/2010/09/13/subquery-factoring-4/feed/" rel="self" type="application/rss+xml" />
	<link>http://jonathanlewis.wordpress.com/2010/09/13/subquery-factoring-4/</link>
	<description>Just another Oracle weblog</description>
	<lastBuildDate>Tue, 18 Jun 2013 04:32:58 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: ANSI &#8211; argh &#171; Oracle Scratchpad</title>
		<link>http://jonathanlewis.wordpress.com/2010/09/13/subquery-factoring-4/#comment-38055</link>
		<dc:creator><![CDATA[ANSI &#8211; argh &#171; Oracle Scratchpad]]></dc:creator>
		<pubDate>Fri, 03 Dec 2010 19:32:00 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanlewis.wordpress.com/?p=4506#comment-38055</guid>
		<description><![CDATA[[...] included a couple of &#8220;factored subqueries&#8221; &#8211; and there are a few outstanding optimizer defects with handling subquery factoring &#8211; so when he claimed that this was an example of Oracle [...]]]></description>
		<content:encoded><![CDATA[<p>[...] included a couple of &#8220;factored subqueries&#8221; &#8211; and there are a few outstanding optimizer defects with handling subquery factoring &#8211; so when he claimed that this was an example of Oracle [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Quiz Night &#171; Oracle Scratchpad</title>
		<link>http://jonathanlewis.wordpress.com/2010/09/13/subquery-factoring-4/#comment-37752</link>
		<dc:creator><![CDATA[Quiz Night &#171; Oracle Scratchpad]]></dc:creator>
		<pubDate>Sun, 21 Nov 2010 12:28:02 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanlewis.wordpress.com/?p=4506#comment-37752</guid>
		<description><![CDATA[[...] visits the product_pricing table twice might be a very good idea &#8211; because there is a special &#8220;index (min/max) range scan&#8221; optimisation that works very well if (a) have the right indexes in place (b) use a nested loop and (c) only ask [...]]]></description>
		<content:encoded><![CDATA[<p>[...] visits the product_pricing table twice might be a very good idea &#8211; because there is a special &#8220;index (min/max) range scan&#8221; optimisation that works very well if (a) have the right indexes in place (b) use a nested loop and (c) only ask [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jonathan Lewis</title>
		<link>http://jonathanlewis.wordpress.com/2010/09/13/subquery-factoring-4/#comment-37421</link>
		<dc:creator><![CDATA[Jonathan Lewis]]></dc:creator>
		<pubDate>Wed, 29 Sep 2010 14:50:26 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanlewis.wordpress.com/?p=4506#comment-37421</guid>
		<description><![CDATA[Sharas,

You haven&#039;t mentioned a version of Oracle, and you haven&#039;t supplied any execution plans - although we might guess from what you&#039;ve said that the subquery is being materialized. 

I&#039;d have to guess, from what you have said, that the optimizer was using a simple filter pushdown (FPD) when the views were inline, but wasn&#039;t able to do the pushdown when the subquery materialized. This sounds very like an (unpublished) bug that Karen Morten mentioned to me some time ago.

If you want a workaround - try creating the subquery as a real &#039;in-database&#039; view.]]></description>
		<content:encoded><![CDATA[<p>Sharas,</p>
<p>You haven&#8217;t mentioned a version of Oracle, and you haven&#8217;t supplied any execution plans &#8211; although we might guess from what you&#8217;ve said that the subquery is being materialized. </p>
<p>I&#8217;d have to guess, from what you have said, that the optimizer was using a simple filter pushdown (FPD) when the views were inline, but wasn&#8217;t able to do the pushdown when the subquery materialized. This sounds very like an (unpublished) bug that Karen Morten mentioned to me some time ago.</p>
<p>If you want a workaround &#8211; try creating the subquery as a real &#8216;in-database&#8217; view.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sharas Vital</title>
		<link>http://jonathanlewis.wordpress.com/2010/09/13/subquery-factoring-4/#comment-37415</link>
		<dc:creator><![CDATA[Sharas Vital]]></dc:creator>
		<pubDate>Wed, 29 Sep 2010 11:16:33 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanlewis.wordpress.com/?p=4506#comment-37415</guid>
		<description><![CDATA[Hello Jonathan –
I have re-written a view using subquery factoring. Here&#039;s a brief,

Original view (V1)
--------------
select * 
  from t1, (select a complex query here) X
 where 
union all
select * 
  from t2, (select same complex query ) X
where 

subquery factoring applied to the V1 (let’s say V2)
-----------
with X as
(select a complex query here)
select * 
  from t1, X
 where 
union all
select * 
  from t2, X
where 

The report using the view V2 with a filter takes longer than the unconstrained one.
I guess, it is because it is loading all the data from the complex query into the temp table, then goes on with the rest of the query.
Is there a way to tell ORACLE to evaluate the filter first and then proceed to the temp table trans formation ? 

Thanks,
Sharas]]></description>
		<content:encoded><![CDATA[<p>Hello Jonathan –<br />
I have re-written a view using subquery factoring. Here&#8217;s a brief,</p>
<p>Original view (V1)<br />
&#8212;&#8212;&#8212;&#8212;&#8211;<br />
select *<br />
  from t1, (select a complex query here) X<br />
 where<br />
union all<br />
select *<br />
  from t2, (select same complex query ) X<br />
where </p>
<p>subquery factoring applied to the V1 (let’s say V2)<br />
&#8212;&#8212;&#8212;&#8211;<br />
with X as<br />
(select a complex query here)<br />
select *<br />
  from t1, X<br />
 where<br />
union all<br />
select *<br />
  from t2, X<br />
where </p>
<p>The report using the view V2 with a filter takes longer than the unconstrained one.<br />
I guess, it is because it is loading all the data from the complex query into the temp table, then goes on with the rest of the query.<br />
Is there a way to tell ORACLE to evaluate the filter first and then proceed to the temp table trans formation ? </p>
<p>Thanks,<br />
Sharas</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jonathan Lewis</title>
		<link>http://jonathanlewis.wordpress.com/2010/09/13/subquery-factoring-4/#comment-37355</link>
		<dc:creator><![CDATA[Jonathan Lewis]]></dc:creator>
		<pubDate>Tue, 21 Sep 2010 12:19:33 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanlewis.wordpress.com/?p=4506#comment-37355</guid>
		<description><![CDATA[Martin,

Thanks, yes, I&#039;ve now corrected it.]]></description>
		<content:encoded><![CDATA[<p>Martin,</p>
<p>Thanks, yes, I&#8217;ve now corrected it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Martin Berns</title>
		<link>http://jonathanlewis.wordpress.com/2010/09/13/subquery-factoring-4/#comment-37353</link>
		<dc:creator><![CDATA[Martin Berns]]></dc:creator>
		<pubDate>Tue, 21 Sep 2010 10:10:01 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanlewis.wordpress.com/?p=4506#comment-37353</guid>
		<description><![CDATA[Hi Jonathan,

is the &#039;,&#039; behind mcc in the first query just a typo? 

-----------------8&lt;-----------------
	...
left join
	max_cust_comm	mcc, -- &lt;&lt;&lt;&lt;
on	mcc.order_id = ord.order_id
-----------------8&lt;-----------------

Shouldn&#039;t you get an ORA-00905 - Missing keyword?]]></description>
		<content:encoded><![CDATA[<p>Hi Jonathan,</p>
<p>is the &#8216;,&#8217; behind mcc in the first query just a typo? </p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;8&lt;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
	&#8230;<br />
left join<br />
	max_cust_comm	mcc, &#8212; &lt;&lt;&lt;&lt;<br />
on	mcc.order_id = ord.order_id<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;8&lt;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<p>Shouldn&#039;t you get an ORA-00905 &#8211; Missing keyword?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jonathan Lewis</title>
		<link>http://jonathanlewis.wordpress.com/2010/09/13/subquery-factoring-4/#comment-37324</link>
		<dc:creator><![CDATA[Jonathan Lewis]]></dc:creator>
		<pubDate>Thu, 16 Sep 2010 19:25:11 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanlewis.wordpress.com/?p=4506#comment-37324</guid>
		<description><![CDATA[Timur,

So much to test, so little time !  (Sir Cecil Rhodes - misquoted).

Thanks for picking this up and letting us know the results. That&#039;s action worthy of &lt;a href=&quot;http://timurakhmadeev.wordpress.com/2010/08/31/im-an-oakie-now/&quot; rel=&quot;nofollow&quot;&gt;&lt;em&gt;&lt;strong&gt;an Oak Table member&lt;/strong&gt;&lt;/em&gt;&lt;/a&gt;.]]></description>
		<content:encoded><![CDATA[<p>Timur,</p>
<p>So much to test, so little time !  (Sir Cecil Rhodes &#8211; misquoted).</p>
<p>Thanks for picking this up and letting us know the results. That&#8217;s action worthy of <a href="http://timurakhmadeev.wordpress.com/2010/08/31/im-an-oakie-now/" rel="nofollow"><em><strong>an Oak Table member</strong></em></a>.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Timur Akhmadeev</title>
		<link>http://jonathanlewis.wordpress.com/2010/09/13/subquery-factoring-4/#comment-37323</link>
		<dc:creator><![CDATA[Timur Akhmadeev]]></dc:creator>
		<pubDate>Thu, 16 Sep 2010 17:17:16 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanlewis.wordpress.com/?p=4506#comment-37323</guid>
		<description><![CDATA[11.2.0.2 doesn&#039;t unnest subquery and the result is correct
[sourcecode lang=&quot;sql&quot;]
----------------------------------------------------------------------------
&#124; Id  &#124; Operation           &#124; Name &#124; Rows  &#124; Bytes &#124; Cost (%CPU)&#124; Time     &#124;
----------------------------------------------------------------------------
&#124;   0 &#124; SELECT STATEMENT    &#124;      &#124;     1 &#124;    13 &#124;    15   (0)&#124; 00:00:01 &#124;
&#124;*  1 &#124;  FILTER             &#124;      &#124;       &#124;       &#124;            &#124;          &#124;
&#124;   2 &#124;   TABLE ACCESS FULL &#124; T2   &#124;     1 &#124;    13 &#124;     3   (0)&#124; 00:00:01 &#124;
&#124;   3 &#124;   UNION-ALL         &#124;      &#124;       &#124;       &#124;            &#124;          &#124;
&#124;*  4 &#124;    TABLE ACCESS FULL&#124; T1   &#124;     1 &#124;    13 &#124;     3   (0)&#124; 00:00:01 &#124;
&#124;*  5 &#124;    TABLE ACCESS FULL&#124; T1   &#124;     1 &#124;    13 &#124;     3   (0)&#124; 00:00:01 &#124;
&#124;*  6 &#124;    TABLE ACCESS FULL&#124; T1   &#124;     1 &#124;    13 &#124;     3   (0)&#124; 00:00:01 &#124;
&#124;*  7 &#124;    TABLE ACCESS FULL&#124; T1   &#124;     1 &#124;    13 &#124;     3   (0)&#124; 00:00:01 &#124;
----------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter( EXISTS ( (SELECT &quot;NUM&quot; FROM &quot;T1&quot; &quot;T1&quot; WHERE &quot;NUM&quot;&lt;=1)
              UNION ALL  (SELECT &quot;NUM&quot; FROM &quot;T1&quot; &quot;T1&quot; WHERE &quot;NUM&quot;&lt;=1) UNION ALL
              (SELECT &quot;NUM&quot; FROM &quot;T1&quot; &quot;T1&quot; WHERE &quot;NUM&quot;&lt;=1) UNION ALL  (SELECT &quot;NUM&quot;
              FROM &quot;T1&quot; &quot;T1&quot; WHERE &quot;NUM&quot;&lt;=1)))
   4 - filter(&quot;NUM&quot;&lt;=1)
   5 - filter(&quot;NUM&quot;&lt;=1)
   6 - filter(&quot;NUM&quot;&lt;=1)
   7 - filter(&quot;NUM&quot;&lt;=1)
[/sourcecode]
with explicit message in 10053 trace:
[sourcecode]
Subquery Unnesting on query block SEL$1 (#1)SU: Performing unnesting that does not require costing.
SU: Considering subquery unnest on query block SEL$1 (#1).
SU:   Checking validity of unnesting subquery SET$1 (#2)
SU:     SU bypassed: Unnest would lead to cartesian product.
SU:   Validity checks failed.
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>11.2.0.2 doesn&#8217;t unnest subquery and the result is correct</p>
<pre class="brush: sql; title: ; notranslate">
----------------------------------------------------------------------------
| Id  | Operation           | Name | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |      |     1 |    13 |    15   (0)| 00:00:01 |
|*  1 |  FILTER             |      |       |       |            |          |
|   2 |   TABLE ACCESS FULL | T2   |     1 |    13 |     3   (0)| 00:00:01 |
|   3 |   UNION-ALL         |      |       |       |            |          |
|*  4 |    TABLE ACCESS FULL| T1   |     1 |    13 |     3   (0)| 00:00:01 |
|*  5 |    TABLE ACCESS FULL| T1   |     1 |    13 |     3   (0)| 00:00:01 |
|*  6 |    TABLE ACCESS FULL| T1   |     1 |    13 |     3   (0)| 00:00:01 |
|*  7 |    TABLE ACCESS FULL| T1   |     1 |    13 |     3   (0)| 00:00:01 |
----------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter( EXISTS ( (SELECT &quot;NUM&quot; FROM &quot;T1&quot; &quot;T1&quot; WHERE &quot;NUM&quot;&lt;=1)
              UNION ALL  (SELECT &quot;NUM&quot; FROM &quot;T1&quot; &quot;T1&quot; WHERE &quot;NUM&quot;&lt;=1) UNION ALL
              (SELECT &quot;NUM&quot; FROM &quot;T1&quot; &quot;T1&quot; WHERE &quot;NUM&quot;&lt;=1) UNION ALL  (SELECT &quot;NUM&quot;
              FROM &quot;T1&quot; &quot;T1&quot; WHERE &quot;NUM&quot;&lt;=1)))
   4 - filter(&quot;NUM&quot;&lt;=1)
   5 - filter(&quot;NUM&quot;&lt;=1)
   6 - filter(&quot;NUM&quot;&lt;=1)
   7 - filter(&quot;NUM&quot;&lt;=1)
</pre>
<p>with explicit message in 10053 trace:</p>
<pre class="brush: plain; title: ; notranslate">
Subquery Unnesting on query block SEL$1 (#1)SU: Performing unnesting that does not require costing.
SU: Considering subquery unnest on query block SEL$1 (#1).
SU:   Checking validity of unnesting subquery SET$1 (#2)
SU:     SU bypassed: Unnest would lead to cartesian product.
SU:   Validity checks failed.
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Timur Akhmadeev</title>
		<link>http://jonathanlewis.wordpress.com/2010/09/13/subquery-factoring-4/#comment-37318</link>
		<dc:creator><![CDATA[Timur Akhmadeev]]></dc:creator>
		<pubDate>Thu, 16 Sep 2010 12:08:46 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanlewis.wordpress.com/?p=4506#comment-37318</guid>
		<description><![CDATA[It&#039;s not related to subquery factoring, because Oracle fails to produce correct results without it (10.2.0.5 &amp; 11.2.0.1). Looks like Oracle lost join predicate somewhere on the step of subquery unnesting:
[sourcecode lang=&quot;sql&quot;]
SQL&gt; select num from t2
  2  where 1 &gt;= any (select num as col from t1
  3             union all
  4             select num from t1
  5             union all
  6             select num from t1
  7             union all
  8             select num from t1);

                 NUM
--------------------
                   1
                   1
                   1
                   1

4 rows selected.

SQL&gt; @x

PLAN_TABLE_OUTPUT
----------------------------------------------------------------------
----------------------------------------------------------------------
SQL_ID  11s2f4nmsbwum, child number 1
-------------------------------------
select num from t2 where 1 &gt;= any (select num as col from t1
union all            select num from t1            union all
select num from t1            union all            select num from t1)

Plan hash value: 2572381663

---------------------------------------------------
&#124; Id  &#124; Operation             &#124; Name     &#124; E-Rows &#124;
---------------------------------------------------
&#124;   0 &#124; SELECT STATEMENT      &#124;          &#124;        &#124;
&#124;   1 &#124;  MERGE JOIN CARTESIAN &#124;          &#124;      1 &#124;
&#124;   2 &#124;   TABLE ACCESS FULL   &#124; T2       &#124;      1 &#124;
&#124;   3 &#124;   BUFFER SORT         &#124;          &#124;      4 &#124;
&#124;   4 &#124;    VIEW               &#124; VW_NSO_1 &#124;      4 &#124;
&#124;   5 &#124;     UNION-ALL         &#124;          &#124;        &#124;
&#124;*  6 &#124;      TABLE ACCESS FULL&#124; T1       &#124;      1 &#124;
&#124;*  7 &#124;      TABLE ACCESS FULL&#124; T1       &#124;      1 &#124;
&#124;*  8 &#124;      TABLE ACCESS FULL&#124; T1       &#124;      1 &#124;
&#124;*  9 &#124;      TABLE ACCESS FULL&#124; T1       &#124;      1 &#124;
---------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   6 - filter(&quot;NUM&quot;&lt;=1)
   7 - filter(&quot;NUM&quot;&lt;=1)
   8 - filter(&quot;NUM&quot;&lt;=1)
   9 - filter(&quot;NUM&quot;&lt;=1)
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>It&#8217;s not related to subquery factoring, because Oracle fails to produce correct results without it (10.2.0.5 &amp; 11.2.0.1). Looks like Oracle lost join predicate somewhere on the step of subquery unnesting:</p>
<pre class="brush: sql; title: ; notranslate">
SQL&gt; select num from t2
  2  where 1 &gt;= any (select num as col from t1
  3             union all
  4             select num from t1
  5             union all
  6             select num from t1
  7             union all
  8             select num from t1);

                 NUM
--------------------
                   1
                   1
                   1
                   1

4 rows selected.

SQL&gt; @x

PLAN_TABLE_OUTPUT
----------------------------------------------------------------------
----------------------------------------------------------------------
SQL_ID  11s2f4nmsbwum, child number 1
-------------------------------------
select num from t2 where 1 &gt;= any (select num as col from t1
union all            select num from t1            union all
select num from t1            union all            select num from t1)

Plan hash value: 2572381663

---------------------------------------------------
| Id  | Operation             | Name     | E-Rows |
---------------------------------------------------
|   0 | SELECT STATEMENT      |          |        |
|   1 |  MERGE JOIN CARTESIAN |          |      1 |
|   2 |   TABLE ACCESS FULL   | T2       |      1 |
|   3 |   BUFFER SORT         |          |      4 |
|   4 |    VIEW               | VW_NSO_1 |      4 |
|   5 |     UNION-ALL         |          |        |
|*  6 |      TABLE ACCESS FULL| T1       |      1 |
|*  7 |      TABLE ACCESS FULL| T1       |      1 |
|*  8 |      TABLE ACCESS FULL| T1       |      1 |
|*  9 |      TABLE ACCESS FULL| T1       |      1 |
---------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   6 - filter(&quot;NUM&quot;&lt;=1)
   7 - filter(&quot;NUM&quot;&lt;=1)
   8 - filter(&quot;NUM&quot;&lt;=1)
   9 - filter(&quot;NUM&quot;&lt;=1)
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jonathan Lewis</title>
		<link>http://jonathanlewis.wordpress.com/2010/09/13/subquery-factoring-4/#comment-37317</link>
		<dc:creator><![CDATA[Jonathan Lewis]]></dc:creator>
		<pubDate>Thu, 16 Sep 2010 09:39:47 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanlewis.wordpress.com/?p=4506#comment-37317</guid>
		<description><![CDATA[I&#039;ve just been directed to another problem with subquery factoring that makes the irritation factor of my example pale into insignificance. 

How&#039;s this for wrong results:  http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:4423923392083#2674339300346087357

You run a query with a restriction that should reduce the row count - and get back more rows than you put in !]]></description>
		<content:encoded><![CDATA[<p>I&#8217;ve just been directed to another problem with subquery factoring that makes the irritation factor of my example pale into insignificance. </p>
<p>How&#8217;s this for wrong results:  <a href="http://asktom.oracle.com/pls/asktom/f?p=100:11:0" rel="nofollow">http://asktom.oracle.com/pls/asktom/f?p=100:11:0</a>::::P11_QUESTION_ID:4423923392083#2674339300346087357</p>
<p>You run a query with a restriction that should reduce the row count &#8211; and get back more rows than you put in !</p>
]]></content:encoded>
	</item>
</channel>
</rss>
