<?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: Conditional SQL (2)</title>
	<atom:link href="http://jonathanlewis.wordpress.com/2007/02/14/conditional-sql-2/feed/" rel="self" type="application/rss+xml" />
	<link>http://jonathanlewis.wordpress.com/2007/02/14/conditional-sql-2/</link>
	<description>Just another Oracle weblog</description>
	<lastBuildDate>Thu, 23 May 2013 12:47:17 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: VSharma</title>
		<link>http://jonathanlewis.wordpress.com/2007/02/14/conditional-sql-2/#comment-30658</link>
		<dc:creator><![CDATA[VSharma]]></dc:creator>
		<pubDate>Tue, 25 Mar 2008 12:27:46 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanlewis.wordpress.com/2007/02/14/conditional-sql-2/#comment-30658</guid>
		<description><![CDATA[Hi Jonathan,

Based on the example provided by you, I do not see the difference, in terms of logical i/o&#039;s, between the query with NVL and without NVL i.e.
n1=nvl(:b1,n1) the I/O is 14 Gets and with
n1=:b1, the I/O again is 14 Gets. Infact, when I changed the query to incorporate the changes suggested Paul James, i.e. (n1=:b1 or :b1 is null), the optimizer opted only for a Full Table Scan of T1 and the I/O&#039;s were way high (364 I/O&#039;s). 

Hence, to me it seems the original plan of Split and Concatenation is a good choice. 

What is your say on this ?

Regards]]></description>
		<content:encoded><![CDATA[<p>Hi Jonathan,</p>
<p>Based on the example provided by you, I do not see the difference, in terms of logical i/o&#8217;s, between the query with NVL and without NVL i.e.<br />
n1=nvl(:b1,n1) the I/O is 14 Gets and with<br />
n1=:b1, the I/O again is 14 Gets. Infact, when I changed the query to incorporate the changes suggested Paul James, i.e. (n1=:b1 or :b1 is null), the optimizer opted only for a Full Table Scan of T1 and the I/O&#8217;s were way high (364 I/O&#8217;s). </p>
<p>Hence, to me it seems the original plan of Split and Concatenation is a good choice. </p>
<p>What is your say on this ?</p>
<p>Regards</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jonathan Lewis</title>
		<link>http://jonathanlewis.wordpress.com/2007/02/14/conditional-sql-2/#comment-3357</link>
		<dc:creator><![CDATA[Jonathan Lewis]]></dc:creator>
		<pubDate>Thu, 15 Feb 2007 15:39:41 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanlewis.wordpress.com/2007/02/14/conditional-sql-2/#comment-3357</guid>
		<description><![CDATA[Paul, a quick test on a single table (9.2.0.8 and 10.2.0.1) suggests that this optimisation is a very special case aimed very precisely at the nvl(:b1,colX) and equivalant decode() statemetns.  The equivalent statement using your suggested approach results in a simple tablescan access path, and no concatenation - even when I added the &lt;b&gt;USE_CONCAT&lt;/b&gt; hint.]]></description>
		<content:encoded><![CDATA[<p>Paul, a quick test on a single table (9.2.0.8 and 10.2.0.1) suggests that this optimisation is a very special case aimed very precisely at the nvl(:b1,colX) and equivalant decode() statemetns.  The equivalent statement using your suggested approach results in a simple tablescan access path, and no concatenation &#8211; even when I added the <b>USE_CONCAT</b> hint.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Paul James</title>
		<link>http://jonathanlewis.wordpress.com/2007/02/14/conditional-sql-2/#comment-3356</link>
		<dc:creator><![CDATA[Paul James]]></dc:creator>
		<pubDate>Thu, 15 Feb 2007 10:17:40 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanlewis.wordpress.com/2007/02/14/conditional-sql-2/#comment-3356</guid>
		<description><![CDATA[Johnathan,

Thanks for the answer. I&#039;ve also seen...

AND (   n1 = :b1
     OR :b1 IS NULL)

Would this be any better than the original code?]]></description>
		<content:encoded><![CDATA[<p>Johnathan,</p>
<p>Thanks for the answer. I&#8217;ve also seen&#8230;</p>
<p>AND (   n1 = :b1<br />
     OR :b1 IS NULL)</p>
<p>Would this be any better than the original code?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jonathan Lewis</title>
		<link>http://jonathanlewis.wordpress.com/2007/02/14/conditional-sql-2/#comment-3342</link>
		<dc:creator><![CDATA[Jonathan Lewis]]></dc:creator>
		<pubDate>Wed, 14 Feb 2007 16:46:03 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanlewis.wordpress.com/2007/02/14/conditional-sql-2/#comment-3342</guid>
		<description><![CDATA[Anonymous,  certainly a point worth raising. There are always compromises that have to be made, especially in high-concurrency systems: the skill lies in getting the cost/benefit balanced properly.

The &lt;i&gt;&quot;bind variables are good for OLTP&quot;&lt;/i&gt; argument often gets a little over-used, or abused, in cases where the argument for &quot;a few&quot; specialised cursors has some merit.]]></description>
		<content:encoded><![CDATA[<p>Anonymous,  certainly a point worth raising. There are always compromises that have to be made, especially in high-concurrency systems: the skill lies in getting the cost/benefit balanced properly.</p>
<p>The <i>&#8220;bind variables are good for OLTP&#8221;</i> argument often gets a little over-used, or abused, in cases where the argument for &#8220;a few&#8221; specialised cursors has some merit.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://jonathanlewis.wordpress.com/2007/02/14/conditional-sql-2/#comment-3341</link>
		<dc:creator><![CDATA[Anonymous]]></dc:creator>
		<pubDate>Wed, 14 Feb 2007 16:14:30 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanlewis.wordpress.com/2007/02/14/conditional-sql-2/#comment-3341</guid>
		<description><![CDATA[One reason people I work with give for doing this, is because then &quot;you only use one cursor instead of two, and that way you benefit from [cursor] caching&quot;.

I doubt that any caching benefit would outweigh the inefficient execution plans this could give you, but I wanted to throw it out there.]]></description>
		<content:encoded><![CDATA[<p>One reason people I work with give for doing this, is because then &#8220;you only use one cursor instead of two, and that way you benefit from [cursor] caching&#8221;.</p>
<p>I doubt that any caching benefit would outweigh the inefficient execution plans this could give you, but I wanted to throw it out there.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jonathan Lewis</title>
		<link>http://jonathanlewis.wordpress.com/2007/02/14/conditional-sql-2/#comment-3340</link>
		<dc:creator><![CDATA[Jonathan Lewis]]></dc:creator>
		<pubDate>Wed, 14 Feb 2007 14:42:28 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanlewis.wordpress.com/2007/02/14/conditional-sql-2/#comment-3340</guid>
		<description><![CDATA[Tobias,  better yet is this example from http://tkyte.blogspot.com/2006/10/slow-by-slow.html which reads:
&lt;i&gt;
My mantra, that I&#039;ll be sticking with thank you very much, is:

&#160;&#160;&#160;&#160;You should do it in a single SQL statement if at all possible.
&#160;&#160;&#160;&#160;If you cannot do it in a single SQL Statement, then do it in PL/SQL.
&#160;&#160;&#160;&#160;If you cannot do it in PL/SQL, try a Java Stored Procedure.
&#160;&#160;&#160;&#160;If you cannot do it in Java, do it in a C external procedure.

If you cannot do it in a C external routine, you might want to seriously think about why it is you need to do it…
&lt;/i&gt;

But I don&#039;t think Tom is suggesting you could write the entire application with a single SQL statement. Your reference, and my quote, relate to items comparing &lt;em&gt;&lt;strong&gt;set-wise&lt;/strong&gt;&lt;/em&gt; processing and &lt;em&gt;row-by-row &lt;/em&gt;processing. I doubt if Tom would have any worries about having a little procedural control over which &lt;em&gt;&lt;strong&gt;set-wise &lt;/strong&gt;&lt;/em&gt;operation to use.

Of course, there are some people who go a bit over the top with the &lt;em&gt;&quot;one SQL statement&quot;&lt;/em&gt; approach - but I don&#039;t think that is the common cause for this particular error: it&#039;s more likely to be code-generators, and programmers whose only concern is meeting the specification of content with no regard for performance.

If you really want to stick to the single statement, then you could simply concatenate (&lt;i&gt;union all&lt;/i&gt;)the various bits of SQL, remembering to include the relevant &lt;em&gt;&lt;b&gt;is null&lt;/b&gt;&lt;/em&gt; and &lt;em&gt;&lt;b&gt;is not null&lt;/b&gt;&lt;/em&gt; predicates to get complete non-overlapping coverage. But that&#039;s not so easy as &lt;em&gt;&lt;strong&gt;nulls&lt;/strong&gt;&lt;/em&gt; are counter-intuitive.

The name, by the way, is Jonathan, not John.]]></description>
		<content:encoded><![CDATA[<p>Tobias,  better yet is this example from <a href="http://tkyte.blogspot.com/2006/10/slow-by-slow.html" rel="nofollow">http://tkyte.blogspot.com/2006/10/slow-by-slow.html</a> which reads:<br />
<i><br />
My mantra, that I&#8217;ll be sticking with thank you very much, is:</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;You should do it in a single SQL statement if at all possible.<br />
&nbsp;&nbsp;&nbsp;&nbsp;If you cannot do it in a single SQL Statement, then do it in PL/SQL.<br />
&nbsp;&nbsp;&nbsp;&nbsp;If you cannot do it in PL/SQL, try a Java Stored Procedure.<br />
&nbsp;&nbsp;&nbsp;&nbsp;If you cannot do it in Java, do it in a C external procedure.</p>
<p>If you cannot do it in a C external routine, you might want to seriously think about why it is you need to do it…<br />
</i></p>
<p>But I don&#8217;t think Tom is suggesting you could write the entire application with a single SQL statement. Your reference, and my quote, relate to items comparing <em><strong>set-wise</strong></em> processing and <em>row-by-row </em>processing. I doubt if Tom would have any worries about having a little procedural control over which <em><strong>set-wise </strong></em>operation to use.</p>
<p>Of course, there are some people who go a bit over the top with the <em>&#8220;one SQL statement&#8221;</em> approach &#8211; but I don&#8217;t think that is the common cause for this particular error: it&#8217;s more likely to be code-generators, and programmers whose only concern is meeting the specification of content with no regard for performance.</p>
<p>If you really want to stick to the single statement, then you could simply concatenate (<i>union all</i>)the various bits of SQL, remembering to include the relevant <em><b>is null</b></em> and <em><b>is not null</b></em> predicates to get complete non-overlapping coverage. But that&#8217;s not so easy as <em><strong>nulls</strong></em> are counter-intuitive.</p>
<p>The name, by the way, is Jonathan, not John.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tobias</title>
		<link>http://jonathanlewis.wordpress.com/2007/02/14/conditional-sql-2/#comment-3338</link>
		<dc:creator><![CDATA[Tobias]]></dc:creator>
		<pubDate>Wed, 14 Feb 2007 14:03:19 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanlewis.wordpress.com/2007/02/14/conditional-sql-2/#comment-3338</guid>
		<description><![CDATA[Hi John,

Tom Kyte keeps saying:

&quot;Never do procedurally that which can be done in a single sql statement&quot;

For example on this posting:

http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:6407993912330

or in many of his presentations. 

I believe that is the reason why many people try to write 1 sql statement to return the data they need. 

Thanks]]></description>
		<content:encoded><![CDATA[<p>Hi John,</p>
<p>Tom Kyte keeps saying:</p>
<p>&#8220;Never do procedurally that which can be done in a single sql statement&#8221;</p>
<p>For example on this posting:</p>
<p><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:6407993912330</p>
<p>or in many of his presentations. </p>
<p>I believe that is the reason why many people try to write 1 sql statement to return the data they need. </p>
<p>Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jonathan Lewis</title>
		<link>http://jonathanlewis.wordpress.com/2007/02/14/conditional-sql-2/#comment-3337</link>
		<dc:creator><![CDATA[Jonathan Lewis]]></dc:creator>
		<pubDate>Wed, 14 Feb 2007 11:58:21 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanlewis.wordpress.com/2007/02/14/conditional-sql-2/#comment-3337</guid>
		<description><![CDATA[Paul, as a general principle, code the calling program with something like:
&lt;code&gt;
if&#160;(variable&#160;is&#160;null)&#160;then
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;execute&#160;SQL-statement-1
else
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;execute&#160;SQL-statement-2
end&#160;if;
&lt;/code&gt;
If this is going to give you too many options (2^N for N variables) then restrict the set of statements to those that make the efficient access paths possible and put up with the the fact that some of the filter predicates will still have to use the &lt;em&gt;&lt;b&gt;nvl()&lt;/b&gt;&lt;/em&gt; construct.]]></description>
		<content:encoded><![CDATA[<p>Paul, as a general principle, code the calling program with something like:<br />
<code><br />
if&nbsp;(variable&nbsp;is&nbsp;null)&nbsp;then<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;execute&nbsp;SQL-statement-1<br />
else<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;execute&nbsp;SQL-statement-2<br />
end&nbsp;if;<br />
</code><br />
If this is going to give you too many options (2^N for N variables) then restrict the set of statements to those that make the efficient access paths possible and put up with the the fact that some of the filter predicates will still have to use the <em><b>nvl()</b></em> construct.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Paul James</title>
		<link>http://jonathanlewis.wordpress.com/2007/02/14/conditional-sql-2/#comment-3336</link>
		<dc:creator><![CDATA[Paul James]]></dc:creator>
		<pubDate>Wed, 14 Feb 2007 10:00:30 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanlewis.wordpress.com/2007/02/14/conditional-sql-2/#comment-3336</guid>
		<description><![CDATA[So, how would you &quot;fix the code&quot; then.

I&#039;m asking because this approach is still taken where I work to allow parameters to be passed in as NULL.]]></description>
		<content:encoded><![CDATA[<p>So, how would you &#8220;fix the code&#8221; then.</p>
<p>I&#8217;m asking because this approach is still taken where I work to allow parameters to be passed in as NULL.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
