<?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: Arithmetic</title>
	<atom:link href="http://jonathanlewis.wordpress.com/2012/08/06/arithmetic/feed/" rel="self" type="application/rss+xml" />
	<link>http://jonathanlewis.wordpress.com/2012/08/06/arithmetic/</link>
	<description>Just another Oracle weblog</description>
	<lastBuildDate>Mon, 20 May 2013 17:10:14 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Jonathan Lewis</title>
		<link>http://jonathanlewis.wordpress.com/2012/08/06/arithmetic/#comment-48786</link>
		<dc:creator><![CDATA[Jonathan Lewis]]></dc:creator>
		<pubDate>Wed, 08 Aug 2012 10:31:35 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanlewis.wordpress.com/?p=9291#comment-48786</guid>
		<description><![CDATA[Flado,

Thanks for that - the impact of the HAVING clause is a detail I haven&#039;t looked at for a very long time.
I suspect the effects are version dependent (when, for example) can the optimizer push the having clause arithmetic down into the WHERE clause arithmetic).

I&#039;ve just done a couple of quick tests with a larger data set, and come up with two cases - one where the arithmetic is applied twice, and one where it&#039;s applied once. (The predicate still appears twice in the execution plan output, though).  I&#039;ll have to write up the details some time.]]></description>
		<content:encoded><![CDATA[<p>Flado,</p>
<p>Thanks for that &#8211; the impact of the HAVING clause is a detail I haven&#8217;t looked at for a very long time.<br />
I suspect the effects are version dependent (when, for example) can the optimizer push the having clause arithmetic down into the WHERE clause arithmetic).</p>
<p>I&#8217;ve just done a couple of quick tests with a larger data set, and come up with two cases &#8211; one where the arithmetic is applied twice, and one where it&#8217;s applied once. (The predicate still appears twice in the execution plan output, though).  I&#8217;ll have to write up the details some time.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Flado</title>
		<link>http://jonathanlewis.wordpress.com/2012/08/06/arithmetic/#comment-48784</link>
		<dc:creator><![CDATA[Flado]]></dc:creator>
		<pubDate>Wed, 08 Aug 2012 09:54:44 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanlewis.wordpress.com/?p=9291#comment-48784</guid>
		<description><![CDATA[Sure - I only added (&quot;having&quot;) predicates to your example and worked on a materialised copy of dba_data_files to keep the plans short. I noticed some interesting things which - in hindsight - were only to be expected:
1. duplicated predicates do affect the cardinality estimates when statistics are available.
2. dynamic sampling saves the day: it samples with all the predicates and is therefore not affected by the redundancy or the function in the predicate.
The version is 11.2.0.2.0 - 64bit on SLES 11.
Here&#039;s the script (two identical predicates and dynamic sampling):
[sourcecode]
set echo off timi off
drop table my_data_files purge;
create table my_data_files as 
select * from dba_data_files union all
select * from dba_data_files union all
select * from dba_data_files
;
-- exec dbms_stats.gather_table_stats(null,&#039;my_data_files&#039;)
explain plan for
select 
	file_name, tablespace_name, sum(bytes)/(1024*1024*1024) 
from 
	my_data_files 
group by 
	file_name, tablespace_name
having 
      sum(bytes)/(1024*1024*1024) &gt; 0 
  and sum(bytes)/(1024*1024*1024) &gt; 0 
--  and sum(bytes)/power(1024,3) &gt; 0
;

select * from table(dbms_xplan.display);
[/sourcecode]

And the output:
[sourcecode]
11:51:59 dbread@pd12m&gt;@predicates

Table dropped.


Table created.


Explained.


PLAN_TABLE_OUTPUT
----------------------------------
Plan hash value: 3621239192

-------------------------------------------------------------------------------------
&#124; Id  &#124; Operation           &#124; Name          &#124; Rows  &#124; Bytes &#124; Cost (%CPU)&#124; Time     &#124;
-------------------------------------------------------------------------------------
&#124;   0 &#124; SELECT STATEMENT    &#124;               &#124;    66 &#124; 19008 &#124;     4  (25)&#124; 00:00:01 &#124;
&#124;*  1 &#124;  FILTER             &#124;               &#124;       &#124;       &#124;            &#124;          &#124;
&#124;   2 &#124;   HASH GROUP BY     &#124;               &#124;    66 &#124; 19008 &#124;     4  (25)&#124; 00:00:01 &#124;
&#124;   3 &#124;    TABLE ACCESS FULL&#124; MY_DATA_FILES &#124;    66 &#124; 19008 &#124;     3   (0)&#124; 00:00:01 &#124;
-------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter(SUM(&quot;BYTES&quot;)/1073741824&gt;0 AND SUM(&quot;BYTES&quot;)/1073741824&gt;0)

Note
-----
   - dynamic sampling used for this statement (level=2)

19 rows selected.


[/sourcecode]

Hope this helps.]]></description>
		<content:encoded><![CDATA[<p>Sure &#8211; I only added (&#8220;having&#8221;) predicates to your example and worked on a materialised copy of dba_data_files to keep the plans short. I noticed some interesting things which &#8211; in hindsight &#8211; were only to be expected:<br />
1. duplicated predicates do affect the cardinality estimates when statistics are available.<br />
2. dynamic sampling saves the day: it samples with all the predicates and is therefore not affected by the redundancy or the function in the predicate.<br />
The version is 11.2.0.2.0 &#8211; 64bit on SLES 11.<br />
Here&#8217;s the script (two identical predicates and dynamic sampling):</p>
<pre class="brush: plain; title: ; notranslate">
set echo off timi off
drop table my_data_files purge;
create table my_data_files as 
select * from dba_data_files union all
select * from dba_data_files union all
select * from dba_data_files
;
-- exec dbms_stats.gather_table_stats(null,'my_data_files')
explain plan for
select 
	file_name, tablespace_name, sum(bytes)/(1024*1024*1024) 
from 
	my_data_files 
group by 
	file_name, tablespace_name
having 
      sum(bytes)/(1024*1024*1024) &gt; 0 
  and sum(bytes)/(1024*1024*1024) &gt; 0 
--  and sum(bytes)/power(1024,3) &gt; 0
;

select * from table(dbms_xplan.display);
</pre>
<p>And the output:</p>
<pre class="brush: plain; title: ; notranslate">
11:51:59 dbread@pd12m&gt;@predicates

Table dropped.


Table created.


Explained.


PLAN_TABLE_OUTPUT
----------------------------------
Plan hash value: 3621239192

-------------------------------------------------------------------------------------
| Id  | Operation           | Name          | Rows  | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |               |    66 | 19008 |     4  (25)| 00:00:01 |
|*  1 |  FILTER             |               |       |       |            |          |
|   2 |   HASH GROUP BY     |               |    66 | 19008 |     4  (25)| 00:00:01 |
|   3 |    TABLE ACCESS FULL| MY_DATA_FILES |    66 | 19008 |     3   (0)| 00:00:01 |
-------------------------------------------------------------------------------------

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

   1 - filter(SUM(&quot;BYTES&quot;)/1073741824&gt;0 AND SUM(&quot;BYTES&quot;)/1073741824&gt;0)

Note
-----
   - dynamic sampling used for this statement (level=2)

19 rows selected.


</pre>
<p>Hope this helps.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jonathan Lewis</title>
		<link>http://jonathanlewis.wordpress.com/2012/08/06/arithmetic/#comment-48746</link>
		<dc:creator><![CDATA[Jonathan Lewis]]></dc:creator>
		<pubDate>Tue, 07 Aug 2012 17:30:49 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanlewis.wordpress.com/?p=9291#comment-48746</guid>
		<description><![CDATA[Flado,

Do you have a couple of simple examples of duplicated preidcates surviving ? There have been various changes in the optimizer over the last few versions (10g onwards) to deal with different scenarios, but I hadn&#039;t noticed any cases of duplicates surviving recently. Every version has to be checked in detail, of course, but as a general guideline (and ignoring predicates generated by RLS/FGAC in the very latest versions, I think) if a predicate appears twice it&#039;s selectivity is applied twice.

Contrary to your observation, I actually have an example in 11.2.0.3 where I would like Oracle to keep a duplicated predicate because that would allow it to use a very efficient execution path that is otherwise not available.]]></description>
		<content:encoded><![CDATA[<p>Flado,</p>
<p>Do you have a couple of simple examples of duplicated preidcates surviving ? There have been various changes in the optimizer over the last few versions (10g onwards) to deal with different scenarios, but I hadn&#8217;t noticed any cases of duplicates surviving recently. Every version has to be checked in detail, of course, but as a general guideline (and ignoring predicates generated by RLS/FGAC in the very latest versions, I think) if a predicate appears twice it&#8217;s selectivity is applied twice.</p>
<p>Contrary to your observation, I actually have an example in 11.2.0.3 where I would like Oracle to keep a duplicated predicate because that would allow it to use a very efficient execution path that is otherwise not available.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Flado</title>
		<link>http://jonathanlewis.wordpress.com/2012/08/06/arithmetic/#comment-48726</link>
		<dc:creator><![CDATA[Flado]]></dc:creator>
		<pubDate>Tue, 07 Aug 2012 07:22:13 +0000</pubDate>
		<guid isPermaLink="false">http://jonathanlewis.wordpress.com/?p=9291#comment-48726</guid>
		<description><![CDATA[And it turns out that my preferred expression &quot;sum(bytes)/power(1024,3)&quot; also gets pre-calculated and not eliminated (in 11.2.0.2):

   1 - filter(SUM(&quot;BYTES&quot;)/1073741824&gt;0 AND SUM(&quot;BYTES&quot;)/1073741824&gt;0)

As a matter of fact, even exactly duplicated (before any transformation or pre-calculation) predicates survive. Must check whether they affect any cardinality estimates though.]]></description>
		<content:encoded><![CDATA[<p>And it turns out that my preferred expression &#8220;sum(bytes)/power(1024,3)&#8221; also gets pre-calculated and not eliminated (in 11.2.0.2):</p>
<p>   1 &#8211; filter(SUM(&#8220;BYTES&#8221;)/1073741824&gt;0 AND SUM(&#8220;BYTES&#8221;)/1073741824&gt;0)</p>
<p>As a matter of fact, even exactly duplicated (before any transformation or pre-calculation) predicates survive. Must check whether they affect any cardinality estimates though.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
