<?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>Striving for Optimal Performance &#187; Query Optimizer</title>
	<atom:link href="http://antognini.ch/category/oracledatabase/queryoptimizer/feed/" rel="self" type="application/rss+xml" />
	<link>http://antognini.ch</link>
	<description></description>
	<lastBuildDate>Sat, 17 Dec 2011 23:42:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Ad: The Oracle Query Optimizer 2-Day Seminar</title>
		<link>http://antognini.ch/2011/12/ad-the-oracle-query-optimizer-two-day-seminar/</link>
		<comments>http://antognini.ch/2011/12/ad-the-oracle-query-optimizer-two-day-seminar/#comments</comments>
		<pubDate>Sat, 17 Dec 2011 23:42:45 +0000</pubDate>
		<dc:creator>Christian Antognini</dc:creator>
				<category><![CDATA[Query Optimizer]]></category>
		<category><![CDATA[Speaking]]></category>
		<category><![CDATA[TOP]]></category>

		<guid isPermaLink="false">http://antognini.ch/?p=1689</guid>
		<description><![CDATA[The 31st of January and 1st of February 2012 I will present a 2-day seminar about the Oracle query optimizer in Ballerup (DK). The event is organized by Miracle A/S. The content, which is based on the chapters 2, 4, 5, 6, 9 and 10 of my book, is the following:

Chapter 1 describes the life [...]]]></description>
			<content:encoded><![CDATA[<p>The 31st of January and 1st of February 2012 I will present a 2-day seminar about the Oracle query optimizer in Ballerup (DK). The event is organized by <a href="http://www.miracleas.dk/">Miracle A/S</a>. The content, which is based on the chapters 2, 4, 5, 6, 9 and 10 of <a href="/top">my book</a>, is the following:</p>
<ul>
<li>Chapter 1 describes the life cycle of SQL statements and when the database engine can share cursors.</li>
<li>Chapter 2 describes the aim and architecture of the query optimizer.</li>
<li>Chapter 3 and 4 discuss the statistics used by the query optimizer to carry out its work.</li>
<li>Chapter 5 describes the initialization parameters influencing the behavior of the query optimizer and how to set them.</li>
<li>Chapter 6 outlines different methods of obtaining execution plans, as well as how to read them and recognize inefficient ones.</li>
<li>Chapter 7 describes how to take advantage of available access structures in order to access data stored in a single table efficiently.</li>
<li>Chapter 8 goes beyond accessing a single table, by describing how to join data from several tables together.</li>
</ul>
<p>The <a href="http://antognini.ch/downloads/Miracle_TheOracleQueryOptimizer_20110131.pdf">flyer</a> and <a href="http://www.miracleas.dk/index.php?option=com_content&#038;view=article&#038;id=455%3Aseminar-the-oracle-query-optimizer&#038;catid=13%3Amiracle&#038;Itemid=82&#038;lang=da">this page</a> provide detailed information about the seminar.</p>
]]></content:encoded>
			<wfw:commentRss>http://antognini.ch/2011/12/ad-the-oracle-query-optimizer-two-day-seminar/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Challenges and Chances of the 11g Query Optimizer</title>
		<link>http://antognini.ch/2011/12/challenges-and-chances-of-the-11g-query-optimizer/</link>
		<comments>http://antognini.ch/2011/12/challenges-and-chances-of-the-11g-query-optimizer/#comments</comments>
		<pubDate>Mon, 12 Dec 2011 09:59:52 +0000</pubDate>
		<dc:creator>Christian Antognini</dc:creator>
				<category><![CDATA[11gR1]]></category>
		<category><![CDATA[11gR2]]></category>
		<category><![CDATA[Bug]]></category>
		<category><![CDATA[Indexes]]></category>
		<category><![CDATA[Object Statistics]]></category>
		<category><![CDATA[Query Optimizer]]></category>
		<category><![CDATA[Speaking]]></category>
		<category><![CDATA[System Statistics]]></category>

		<guid isPermaLink="false">http://antognini.ch/?p=1665</guid>
		<description><![CDATA[Challenges and Chances of the 11g Query Optimizer is the name of a presentation I gave at several events (e.g. Trivadis Performance Days, Oracle OpenWorld, DOAG Konferenz, UKOUG Conference) throughout 2011. Its abstract is the following:
With every new release, the query optimizer is enhanced. Oracle Database 11g Release 1 and Release 2 are no exception [...]]]></description>
			<content:encoded><![CDATA[<p><em>Challenges and Chances of the 11g Query Optimizer</em> is the name of a presentation I gave at several events (e.g. Trivadis Performance Days, Oracle OpenWorld, DOAG Konferenz, UKOUG Conference) throughout 2011. Its abstract is the following:</p>
<blockquote><p>With every new release, the query optimizer is enhanced. Oracle Database 11g Release 1 and Release 2 are no exception to the rule. Specifically, they introduce key improvements in the following areas: indexing, optimization techniques, object statistics and plan stability. The aim of this presentation is to review the new features from a practical point of view as well as to point out challenges related to them. In other words, to let you know what you can expect from the query optimizer when you upgrade to Oracle Database 11g.</p></blockquote>
<p>The aim of this short post is to point out that I made available the current version of the slides and all the scripts that go with them <a href="/publications/">here</a>.</p>
<p>The structure of the presentation (incl. a reference to the available scripts) is the following:</p>
<ul>
<li>Observations
<ul>
<li>Number of Query Optimizer Parameters by Release</li>
<li>Number of Query Optimizer Bugs Fixed by Patchset</li>
</ul>
</li>
<li>Indexing
<ul>
<li>Invisible Indexes (ex_invisible_index.sql)</li>
<li>Index Support for Linguistic LIKE (ex_linguistic_like.sql)</li>
<li>INDEX REBUILD and Statistics History (ex_index_rebuild.sql)</li>
</ul>
</li>
<li>Optimization Techniques
<ul>
<li>Full Outer Join (ex_full_outer_join.sql)</li>
<li>Join-Filter Pruning (ex_join_filter_pruning.sql)</li>
<li>Table Expansion (ex_table_expansion.sql)</li>
<li>Join Factorization (ex_join_factorization.sql)</li>
<li>OR Expansion (ex_or_expansion.sql)</li>
<li>Join Elimination (ex_join_elimination.sql)</li>
<li>Subquery Unnesting (ex_subquery_unnesting.sql)</li>
</ul>
</li>
<li>System and Object Statistics (DBMS_STATS)
<ul>
<li>Workload System Statistics</li>
<li>Object Statistics – Default Preferences</li>
<li>Object Statistics – Auto Sample Size</li>
<li>Object Statistics – Pending Statistics (ex_pending_object_statistics.sql)</li>
<li>Object Statistics – Incremental Statistics (ex_incremental_stats.sql)</li>
<li>Object Statistics – Extended Statistics on Expressions (ex_extended_statistics1.sql)</li>
<li>Object Statistics – Extended Statistics on Column Groups (ex_extended_statistics2.sql)</li>
<li>Object Statistics – Seeding Column Groups</li>
<li>Object Statistics – Comparing Statistics (ex_comparing_statistics.sql)</li>
<li>Object Statistics – Locks not Exported</li>
<li>JOB_QUEUE_PROCESSES</li>
</ul>
</li>
<li>Plan Stability
<ul>
<li>CURSOR_SHARING</li>
<li>SQL Plan Baselines  (ex_execution_plan_stability.sql, ex_execution_plan_stability_10g.sql, ex_execution_plan_stability_11g.sql)</li>
<li>Stored Outlines</li>
<li>Adaptive Cursor Sharing (ex_bind_peeking.sql, ex_bind_peeking_bind_aware.sql)</li>
<li>Cardinality Feedback (ex_cardinality_feedback.sql)</li>
</ul>
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://antognini.ch/2011/12/challenges-and-chances-of-the-11g-query-optimizer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Impact of STATISTICS_LEVEL on Cardinality Feedback and Adaptive Cursor Sharing</title>
		<link>http://antognini.ch/2011/09/impact-of-statistics_level-on-cardinality-feedback-and-adaptive-cursor-sharing/</link>
		<comments>http://antognini.ch/2011/09/impact-of-statistics_level-on-cardinality-feedback-and-adaptive-cursor-sharing/#comments</comments>
		<pubDate>Mon, 19 Sep 2011 17:20:09 +0000</pubDate>
		<dc:creator>Christian Antognini</dc:creator>
				<category><![CDATA[11gR1]]></category>
		<category><![CDATA[11gR2]]></category>
		<category><![CDATA[Query Optimizer]]></category>

		<guid isPermaLink="false">http://antognini.ch/?p=1572</guid>
		<description><![CDATA[The STATISTICS_LEVEL parameter controls a bunch of features. In addition to the documentation, also the V$STATISTICS_LEVEL view provides a list of the ones it controls.
SQL&#62; SELECT statistics_name, description, activation_level
  2  FROM v$statistics_level
  3  ORDER BY 3 DESC, 1;

STATISTICS_NAME              [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://download.oracle.com/docs/cd/E11882_01/server.112/e17110/initparams249.htm#CHDDAHJD">STATISTICS_LEVEL</a> parameter controls a bunch of features. In addition to the <a href="http://download.oracle.com/docs/cd/E11882_01/server.112/e17110/initparams249.htm#CHDDAHJD">documentation</a>, also the <a href="http://download.oracle.com/docs/cd/E11882_01/server.112/e17110/dynviews_3075.htm#I1030264">V$STATISTICS_LEVEL</a> view provides a list of the ones it controls.</p>
<p><pre>SQL&gt; SELECT statistics_name, description, activation_level
  2  FROM v$statistics_level
  3  ORDER BY 3 DESC, 1;

STATISTICS_NAME                        DESCRIPTION                                                  ACTIVATION_LEVEL
-------------------------------------- ------------------------------------------------------------ ----------------
Active Session History                 Monitors active session activity using MMNL                  TYPICAL
Adaptive Thresholds Enabled            Controls if Adaptive Thresholds should be enabled            TYPICAL
Automated Maintenance Tasks            Controls if Automated Maintenance should be enabled          TYPICAL
Bind Data Capture                      Enables capture of bind values used by SQL statements        TYPICAL
Buffer Cache Advice                    Predicts the impact of different cache sizes on number of    TYPICAL
                                       physical reads
Global Cache Statistics                RAC Buffer Cache statistics                                  TYPICAL
Longops Statistics                     Enables Longops Statistics                                   TYPICAL
MTTR Advice                            Predicts the impact of different MTTR settings on number of  TYPICAL
                                       physical I/Os
Modification Monitoring                Enables modification monitoring                              TYPICAL
PGA Advice                             Predicts the impact of different values of pga_aggregate_tar TYPICAL
                                       get on the performance of memory intensive SQL operators
Plan Execution Sampling                Enables plan lines sampling                                  TYPICAL
SQL Monitoring                         Controls if SQL Monitoring should be enabled                 TYPICAL
Segment Level Statistics               Enables gathering of segment access statistics               TYPICAL
Shared Pool Advice                     Predicts the impact of different values of shared_pool_size  TYPICAL
                                       on elapsed parse time saved
Streams Pool Advice                    Predicts impact on Streams perfomance of different  Streams  TYPICAL
                                       pool sizes
Threshold-based Alerts                 Controls if Threshold-based Alerts should be enabled         TYPICAL
Time Model Events                      Enables Statics collection for time events                   TYPICAL
Timed Statistics                       Enables gathering of timed statistics                        TYPICAL
Ultrafast Latch Statistics             Maintains statistics for ultrafast latches in the fast path  TYPICAL
Undo Advisor, Alerts and Fast Ramp up  Transaction layer manageability features                     TYPICAL
V$IOSTAT_* statistics                  Controls if I/O stats in v$iostat_ should be enabled         TYPICAL
Plan Execution Statistics              Enables collection of plan execution statistics              ALL
Timed OS Statistics                    Enables gathering of timed operating system statistics       ALL
</pre></p>
<p>Something that I learned only recently is that STATISTICS_LEVEL also controls <em>cardinality feedback</em> and <em>adaptive cursor sharing</em>. This fact, according to me, is neither (clearly) documented nor pointed out by the information provided by V$STATISTICS_LEVEL. In any case, when STATISTICS_LEVEL is set to BASIC at the <em>system level</em> both features are disabled. Interestingly, an ALTER SESSION SET STATISTICS_LEVEL = TYPICAL it is not enough to enable them… For adaptive cursor sharing it is possible to use the BIND_AWARE hint, though.</p>
<p>Note that I never advise to set STATISTICS_LEVEL at the system level to a value that is different from the default (TYPICAL). Probably for this reason I didn’t notice its impact for such a long time… </p>
<p>In any case I find it a bit disappointing that this information is not clearly stated somewhere. Or I’m the only one that was not aware?</p>
]]></content:encoded>
			<wfw:commentRss>http://antognini.ch/2011/09/impact-of-statistics_level-on-cardinality-feedback-and-adaptive-cursor-sharing/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>optimizer_secure_view_merging and VPD</title>
		<link>http://antognini.ch/2011/09/optimizer_secure_view_merging-and-vpd/</link>
		<comments>http://antognini.ch/2011/09/optimizer_secure_view_merging-and-vpd/#comments</comments>
		<pubDate>Sun, 11 Sep 2011 09:21:49 +0000</pubDate>
		<dc:creator>Christian Antognini</dc:creator>
				<category><![CDATA[10gR2]]></category>
		<category><![CDATA[11gR1]]></category>
		<category><![CDATA[11gR2]]></category>
		<category><![CDATA[Query Optimizer]]></category>
		<category><![CDATA[TOP]]></category>

		<guid isPermaLink="false">http://antognini.ch/?p=1552</guid>
		<description><![CDATA[At page 189 of TOP I wrote the following piece of text:
In summary, with the initialization parameter optimizer_secure_view_merging set to TRUE, the query optimizer checks whether view merging could lead to security issues. If this is the case, no view merging will be performed, and performance could be suboptimal as a result. For this reason, [...]]]></description>
			<content:encoded><![CDATA[<p>At page 189 of <a href="/top">TOP</a> I wrote the following piece of text:</p>
<blockquote><p>In summary, with the initialization parameter optimizer_secure_view_merging set to TRUE, the query optimizer checks whether view merging could lead to security issues. If this is the case, no view merging will be performed, and performance could be suboptimal as a result. For this reason, if you are not using views for security purposes, it is better to set this initialization parameter to FALSE.</p></blockquote>
<p>What I didn’t consider when I wrote it, it is the implication of predicate move-around related to Virtual Private Database (VPD). In fact, as described in the <a href="http://download.oracle.com/docs/cd/E11882_01/server.112/e17110/initparams168.htm#I1010262">documentation</a>, that parameter controls <em>view merging</em> as well as <em>predicate move-around</em>. </p>
<p>To point out what the impact is, let’s have a look to an example based on the description provided in <a href="/top">TOP</a>:</p>
<ul>
<li>Say you have a very simple table with one primary key and two more columns.</li>
</ul>
<p><pre>CREATE TABLE t (
  id NUMBER(10) PRIMARY KEY,
  class NUMBER(10),
  pad VARCHAR2(10)
);</pre></p>
<ul>
<li>For security reasons, you define the following policy. Notice the filter that is applied with the function to partially show the content of the table. How this function is implemented and what it does exactly is not important.</li>
</ul>
<p><pre>CREATE OR REPLACE FUNCTION s (schema IN VARCHAR2, tab IN VARCHAR2) RETURN VARCHAR2 AS
BEGIN
  RETURN &#039;f(class) = 1&#039;;
END;
/

BEGIN
  dbms_rls.add_policy(object_schema   =&gt; &#039;U1&#039;,
                      object_name     =&gt; &#039;T&#039;,
                      policy_name     =&gt; &#039;T_SEC&#039;,
                      function_schema =&gt; &#039;U1&#039;,
                      policy_function =&gt; &#039;S&#039;);
END;
/</pre></p>
<ul>
<li>Now let’s say that a user who has access to the table creates the following PL/SQL function. As you can see, it will just display the value of the input parameters through a call to the package dbms_output.</li>
</ul>
<p><pre>CREATE OR REPLACE FUNCTION spy (id IN NUMBER, pad IN VARCHAR2) RETURN NUMBER AS
BEGIN
  dbms_output.put_line(&#039;id=&#039; || id || &#039; pad=&#039; || pad);
  RETURN 1;
END;
/</pre></p>
<ul>
<li>With the initialization parameter optimizer_secure_view_merging set to FALSE, you can run two test queries. Both return only the values that the user is allowed to see. In the second one, however, you are able to see data that you should not be able to access.</li>
</ul>
<p><pre>SQL&gt; SELECT id, pad
  2  FROM t
  3  WHERE id BETWEEN 1 AND 5;

        ID PAD
---------- ----------
         1 DrMLTDXxxq
         4 AszBGEUGEL

SQL&gt; SELECT id, pad
  2  FROM t
  3  WHERE id BETWEEN 1 AND 5
  4  AND spy(id, pad) = 1;

        ID PAD
---------- ----------
         1 DrMLTDXxxq
         4 AszBGEUGEL
id=1 pad=DrMLTDXxxq
id=2 pad=XOZnqYRJwI
id=3 pad=nlGfGBTxNk
id=4 pad=AszBGEUGEL
id=5 pad=qTSRnFjRGb</pre></p>
<ul>
<li>With the initialization parameter optimizer_secure_view_merging set to TRUE, the second query returns the following output. As you can see, the function and the query display the same data.</li>
</ul>
<p><pre>SQL&gt; SELECT id, pad
  2  FROM t
  3  WHERE id BETWEEN 1 AND 5
  4  AND spy(id, pad) = 1;

        ID PAD
---------- ----------
         1 DrMLTDXxxq
         4 AszBGEUGEL
id=1 pad=DrMLTDXxxq
id=4 pad=AszBGEUGEL</pre></p>
<p>The execution plans that are used in the two situations are the following. As you can see only the second one guarantee that the policy defined via VPD is applied before the predicate based on the SPY function. Interestingly enough the other predicate based on the ID column is applied before the one of the policy. Hence, the query optimizer can choose an access path that takes advantage of the primary key.</p>
<ul>
<li>optimizer_secure_view_merging = FALSE</li>
</ul>
<p><pre>---------------------------------------------------
| Id  | Operation                   | Name        |
---------------------------------------------------
|   0 | SELECT STATEMENT            |             |
|*  1 |  TABLE ACCESS BY INDEX ROWID| T           |
|*  2 |   INDEX RANGE SCAN          | SYS_C009970 |
---------------------------------------------------

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

   1 - filter((&quot;SPY&quot;(&quot;ID&quot;,&quot;PAD&quot;)=1 AND &quot;F&quot;(&quot;CLASS&quot;)=1))
   2 - access(&quot;ID&quot;&gt;=1 AND &quot;ID&quot;&lt;=5)</pre></p>
<ul>
<li>optimizer_secure_view_merging = TRUE</li>
</ul>
<p><pre>----------------------------------------------------
| Id  | Operation                    | Name        |
----------------------------------------------------
|   0 | SELECT STATEMENT             |             |
|*  1 |  VIEW                        | T           |
|*  2 |   TABLE ACCESS BY INDEX ROWID| T           |
|*  3 |    INDEX RANGE SCAN          | SYS_C009971 |
----------------------------------------------------

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

   1 - filter(&quot;SPY&quot;(&quot;ID&quot;,&quot;PAD&quot;)=1)
   2 - filter(&quot;F&quot;(&quot;CLASS&quot;)=1)
   3 - access(&quot;ID&quot;&gt;=1 AND &quot;ID&quot;&lt;=5)</pre></p>
<p>Based on these observations, the summary that is provided by <a href="/top">TOP</a> at page 189 should be amended as follows:</p>
<blockquote><p>In summary, with the initialization parameter optimizer_secure_view_merging set to TRUE, the query optimizer checks whether view merging or predicate move-around could lead to security issues. If this is the case, they will not be performed, and performance could be suboptimal as a result. For this reason, if you are not using views or VPD for security purposes, it is better to set this initialization parameter to FALSE.</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://antognini.ch/2011/09/optimizer_secure_view_merging-and-vpd/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>IS NULL Conditions and B-tree Indexes</title>
		<link>http://antognini.ch/2011/02/is-null-conditions-and-b-tree-indexes/</link>
		<comments>http://antognini.ch/2011/02/is-null-conditions-and-b-tree-indexes/#comments</comments>
		<pubDate>Thu, 17 Feb 2011 10:01:56 +0000</pubDate>
		<dc:creator>Christian Antognini</dc:creator>
				<category><![CDATA[10gR1]]></category>
		<category><![CDATA[10gR2]]></category>
		<category><![CDATA[11gR1]]></category>
		<category><![CDATA[11gR2]]></category>
		<category><![CDATA[9iR2]]></category>
		<category><![CDATA[Indexes]]></category>
		<category><![CDATA[Query Optimizer]]></category>
		<category><![CDATA[TOP]]></category>

		<guid isPermaLink="false">http://antognini.ch/?p=1479</guid>
		<description><![CDATA[At page 383 of my book I wrote the following sentence (BTW, the same information is also provided by Table 9-3 at page 381):
With B-tree indexes, IS NULL conditions can be applied only through composite B-tree indexes when several SQL conditions are applied and at least one of them is not based on IS NULL [...]]]></description>
			<content:encoded><![CDATA[<p>At page 383 of <a href="/top">my book</a> I wrote the following sentence (BTW, the same information is also provided by Table 9-3 at page 381):</p>
<blockquote><p>With B-tree indexes, IS NULL conditions can be applied only through composite B-tree indexes when several SQL conditions are applied and at least one of them is not based on IS NULL or an inequality.</p></blockquote>
<p>The text continues by showing the following examples (notice that in both cases the IS NULL predicate is applied through an access predicate):</p>
<p><pre>SELECT /*+ index(t) */ * FROM t WHERE n1 = 6 AND n2 IS NULL

Plan hash value: 780655320

----------------------------------------------
| Id  | Operation                   | Name   |
----------------------------------------------
|   0 | SELECT STATEMENT            |        |
|   1 |  TABLE ACCESS BY INDEX ROWID| T      |
|*  2 |   INDEX RANGE SCAN          | I_N123 |
----------------------------------------------

   2 - access(&quot;N1&quot;=6 AND &quot;N2&quot; IS NULL)

SELECT /*+ index(t) */ * FROM t WHERE n1 IS NULL AND n2 = 8

Plan hash value: 780655320

----------------------------------------------
| Id  | Operation                   | Name   |
----------------------------------------------
|   0 | SELECT STATEMENT            |        |
|   1 |  TABLE ACCESS BY INDEX ROWID| T      |
|*  2 |   INDEX RANGE SCAN          | I_N123 |
----------------------------------------------

   2 - access(&quot;N1&quot; IS NULL AND &quot;N2&quot;=8)
       filter(&quot;N2&quot;=8)</pre></p>
<p>When I wrote that sentence I didn&#8217;t think about one case that, according to it, specifically the part &#8220;is not based on IS NULL or an inequality&#8221;, is not covered. In fact, as the following examples show, it is also possible to apply an IS NULL predicate when the other one is an IS NOT NULL. It is especially interesting to notice that the access predicate doesn&#8217;t reference at all the NOT NULL column!</p>
<p><pre>SELECT /*+ index(t) */ * FROM t WHERE n1 IS NULL AND n2 IS NOT NULL

Plan hash value: 780655320

----------------------------------------------
| Id  | Operation                   | Name   |
----------------------------------------------
|   0 | SELECT STATEMENT            |        |
|   1 |  TABLE ACCESS BY INDEX ROWID| T      |
|*  2 |   INDEX RANGE SCAN          | I_N123 |
----------------------------------------------

   2 - access(&quot;N1&quot; IS NULL)
       filter(&quot;N2&quot; IS NOT NULL)

SELECT /*+ index(t) */ * FROM t WHERE n1 IS NOT NULL AND n2 IS NULL

Plan hash value: 3029444779

----------------------------------------------
| Id  | Operation                   | Name   |
----------------------------------------------
|   0 | SELECT STATEMENT            |        |
|   1 |  TABLE ACCESS BY INDEX ROWID| T      |
|*  2 |   INDEX SKIP SCAN           | I_N123 |
----------------------------------------------

   2 - access(&quot;N2&quot; IS NULL)
       filter((&quot;N2&quot; IS NULL AND &quot;N1&quot; IS NOT NULL))</pre></p>
]]></content:encoded>
			<wfw:commentRss>http://antognini.ch/2011/02/is-null-conditions-and-b-tree-indexes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DBMS_AUTO_SQLTUNE: ORA-01748 and Documentation Bugs</title>
		<link>http://antognini.ch/2010/10/dbms_auto_sqltune-ora-01748-and-documentation-bugs/</link>
		<comments>http://antognini.ch/2010/10/dbms_auto_sqltune-ora-01748-and-documentation-bugs/#comments</comments>
		<pubDate>Wed, 06 Oct 2010 15:04:00 +0000</pubDate>
		<dc:creator>Christian Antognini</dc:creator>
				<category><![CDATA[11gR2]]></category>
		<category><![CDATA[Bug]]></category>
		<category><![CDATA[Query Optimizer]]></category>

		<guid isPermaLink="false">http://antognini.ch/?p=1328</guid>
		<description><![CDATA[As of 11.2.0.2 a new package, DBMS_AUTO_SQLTUNE, is available for accessing and configuring Automatic SQL Tuning. The package provides three features:

Execution of the Automatic SQL Tuning task (EXECUTE_AUTO_TUNING_TASK)
Generation of a report showing the output generated by the Automatic SQL Tuning tasks (REPORT_AUTO_TUNING_TASK)
Configuration of the Automatic SQL Tuning parameters (SET_AUTO_TUNING_TASK_PARAMETER)

In this post I would like to [...]]]></description>
			<content:encoded><![CDATA[<p>As of 11.2.0.2 a new package, DBMS_AUTO_SQLTUNE, is available for accessing and configuring Automatic SQL Tuning. The package provides three features:</p>
<ul>
<li>Execution of the Automatic SQL Tuning task (EXECUTE_AUTO_TUNING_TASK)</li>
<li>Generation of a report showing the output generated by the Automatic SQL Tuning tasks (REPORT_AUTO_TUNING_TASK)</li>
<li>Configuration of the Automatic SQL Tuning parameters (SET_AUTO_TUNING_TASK_PARAMETER)</li>
</ul>
<p>In this post I would like to focus on the second functionality. With it you can for example execute the following commands in SQL*Plus to get a report for the most recent task:</p>
<p><pre>SET LONG 1000000 PAGESIZE 0 LINESIZE 200
COLUMN report_auto_tuning_task FORMAT A200
SELECT dbms_auto_sqltune.report_auto_tuning_task FROM dual;</pre></p>
<p>The REPORT_AUTO_TUNING_TASK function is not limited to being used without parameters. In fact, its signature is the following:</p>
<p><pre>FUNCTION REPORT_AUTO_TUNING_TASK RETURNS CLOB
 Argument Name                  Type                    In/Out Default?
 ------------------------------ ----------------------- ------ --------
 BEGIN_EXEC                     VARCHAR2                IN     DEFAULT
 END_EXEC                       VARCHAR2                IN     DEFAULT
 TYPE                           VARCHAR2                IN     DEFAULT
 LEVEL                          VARCHAR2                IN     DEFAULT
 SECTION                        VARCHAR2                IN     DEFAULT
 OBJECT_ID                      NUMBER                  IN     DEFAULT
 RESULT_LIMIT                   NUMBER                  IN     DEFAULT</pre></p>
<p>The purpose of the parameters is the following:</p>
<ul>
<li>BEGIN_EXEC/END_EXEC specifies which tasks have to be reported. With the default value, NULL, the most recent task is shown.</li>
<li>TYPE specifies the type of the report. Presently only TEXT is supported.</li>
<li>LEVEL specifies the level of detail shown in the report. This is somewhat similar to the FORMAT parameter of DBMS_XPLAN. The supported values are BASIC, TYPICAL (default) and ALL.</li>
<li>SECTION specifies which sections are shown in the report. The supported values are SUMMARY, FINDINGS, PLANS, INFORMATION, ERRORS and ALL (default). Possibly there is a bug with the value ERRORS. In fact, during my tests, it always returned the same report as the value INFORMATION!?!?</li>
<li>OBJECT_ID restricts the report to a single SQL statement. You can get the ID either from the report or by querying DBA_ADVISOR_OBJECTS.OBJECT_ID.</li>
<li>RESULT_LIMIT specifies the maximum number of SQL statements shown in the report.</li>
</ul>
<p>The essential thing I would like to point out is that two parameters have as name a reserved word.</p>
<p><pre>SQL&gt; SELECT keyword
  2  FROM v$reserved_words
  3  WHERE keyword IN (&#039;BEGIN_EXEC&#039;,&#039;END_EXEC&#039;,&#039;TYPE&#039;,&#039;LEVEL&#039;,&#039;SECTION&#039;,&#039;OBJECT_ID&#039;,&#039;RESULT_LIMIT&#039;);

KEYWORD
------------------------------
LEVEL
TYPE</pre></p>
<p>As a result expect to get an error like the following one if you want to use named notation:</p>
<p><pre>SELECT dbms_auto_sqltune.report_auto_tuning_task(level =&gt; &#039;basic&#039;) FROM dual
                                                       *
ERROR at line 1:
ORA-01748: only simple column names allowed here</pre></p>
<p>These are the kind of things I cannot understand! It&#8217;s so easy to choose a name that doesn&#8217;t lead to such problems. For one time I hope that Oracle will break backward compatibility and change the name of these parameters.</p>
<p>Another thing I wanted to point out is that the values supported by the SECTION parameter are the ones I reported above, and not the ones documented <a href="http://download.oracle.com/docs/cd/E11882_01/appdev.112/e16760/d_autosta.htm#CIAEHBGA">here</a>. In fact, there are two typos in the documentation. This is probably because the same typos can also be seen in $ORACLE_HOME/rdbms/admin/dbmssqlt.sql. In other words, it seems that the guy who wrote the documentation did a simple copy/paste of the content of the SQL file.</p>
]]></content:encoded>
			<wfw:commentRss>http://antognini.ch/2010/10/dbms_auto_sqltune-ora-01748-and-documentation-bugs/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Exadata Storage Server and the Query Optimizer – Part 4</title>
		<link>http://antognini.ch/2010/08/exadata-storage-server-and-the-query-optimizer-%e2%80%93-part-4/</link>
		<comments>http://antognini.ch/2010/08/exadata-storage-server-and-the-query-optimizer-%e2%80%93-part-4/#comments</comments>
		<pubDate>Mon, 09 Aug 2010 15:37:40 +0000</pubDate>
		<dc:creator>Christian Antognini</dc:creator>
				<category><![CDATA[11gR2]]></category>
		<category><![CDATA[Bug]]></category>
		<category><![CDATA[Exadata]]></category>
		<category><![CDATA[Query Optimizer]]></category>

		<guid isPermaLink="false">http://antognini.ch/?p=1227</guid>
		<description><![CDATA[When I started writing the series of posts about Exadata Storage Server and the query optimizer, I didn’t expect to write more than three posts (part 1, part 2, part 3). Of course, things change. Hence, here is part 4 to cover a couple of things that I learned in the next couple of months.
In [...]]]></description>
			<content:encoded><![CDATA[<p>When I started writing the series of posts about Exadata Storage Server and the query optimizer, I didn’t expect to write more than three posts (<a href="/2010/04/exadata-storage-server-and-the-query-optimizer-part-1/">part 1</a>, <a href="/2010/05/exadata-storage-server-and-the-query-optimizer-%e2%80%93-part-2/">part 2</a>, <a href="/2010/05/exadata-storage-server-and-the-query-optimizer-part-3/">part 3</a>). Of course, things change. Hence, here is part 4 to cover a couple of things that I learned in the next couple of months.</p>
<p>In <a href="/2010/05/exadata-storage-server-and-the-query-optimizer-%e2%80%93-part-2/">part 2</a> I pointed out that Oracle Database is not able to offload the processing of all datetime functions. This fact, to my surprise, was also referenced by <a href="http://www.netezza.com/">Netezza</a> in a recent paper entitled <a href="http://www.netezza.com/exadata-twinfin-compared/index.aspx">Oracle Exadata and Netezza TwinFin Compared</a>. The essential thing to understand is that this limitation is due to bug 9682721. The fix is expected to be part of 11.2.0.2. According to my test cases (that <a href="http://structureddata.org/">Greg Rahn</a> was so kind to execute against an early release of 11.2.0.2), offloading works correctly for all datetime functions but for the following three predicates.</p>
<ul>
<li>months_between(d,sysdate) = 0</li>
<li>months_between(d,current_date) = 0</li>
<li>months_between(d,to_date(&#8216;01-01-2010&#8242;,&#8217;DD-MM-YYYY&#8217;)) = 0</li>
</ul>
<p>Note that the MONTHS_BETWEEN function can basically be offloaded. The problem in these cases is that the offloading does not work when, for example, SYSDATE is used as parameter.</p>
<p>To have a full list of the functions supporting offloading, the “official reference” is available through the V$SQLFN_METADATA view. Here is a simple query to summarize the current situation.</p>
<p><pre>SQL&gt; SELECT offloadable, count(DISTINCT name)
  2  FROM v$sqlfn_metadata
  3  GROUP BY offloadable;

OFF COUNT(DISTINCTNAME)
--- -------------------
NO                  511
YES                 319</pre></p>
<p>Another thing I would like to point out about offloading is that the feature can be controlled through the CELL_OFFLOAD_PROCESSING initialization parameter. By default it is set to TRUE and, therefore, offloading is used whenever possible. It goes without saying that offloading is disabled when it is set to FALSE. Note that it can not only be set at the instance and session level, but also at the statement level. The following example illustrate this (notice that only the first query uses offloading).</p>
<p><pre>SQL&gt; ALTER SESSION SET cell_offload_plan_display = always;

SQL&gt; ALTER SESSION SET cell_offload_processing = true;

SQL&gt; EXPLAIN PLAN FOR SELECT * FROM t WHERE id = 101;

SQL&gt; SELECT * FROM table(dbms_xplan.display(format=&gt;&#039;basic +predicate&#039;));

PLAN_TABLE_OUTPUT
---------------------------------------------------
Plan hash value: 3557914527

-------------------------------------------
| Id  | Operation                  | Name |
-------------------------------------------
|   0 | SELECT STATEMENT           |      |
|   1 |  PARTITION RANGE ALL       |      |
|*  2 |   TABLE ACCESS STORAGE FULL| T    |
-------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------
   2 - storage(&quot;ID&quot;=101)
       filter(&quot;ID&quot;=101)

SQL&gt; EXPLAIN PLAN FOR SELECT /*+ opt_param(&#039;cell_offload_processing&#039; &#039;false&#039;) */  * FROM t WHERE id = 101;

SQL&gt; SELECT * FROM table(dbms_xplan.display(format=&gt;&#039;basic +predicate&#039;));

PLAN_TABLE_OUTPUT
---------------------------------------------------
Plan hash value: 3557914527

-------------------------------------------
| Id  | Operation                  | Name |
-------------------------------------------
|   0 | SELECT STATEMENT           |      |
|   1 |  PARTITION RANGE ALL       |      |
|*  2 |   TABLE ACCESS STORAGE FULL| T    |
-------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------
   2 - filter(&quot;ID&quot;=101)</pre></p>
<p>Another initialization parameter that controls offloading is CELL_OFFLOAD_DECRYPTION. This parameter is relevant for encrypted tablespaces only. With it you can specify whether the keys necessary to decrypt the data can be shipped to the cells. By default it is set to TRUE and, therefore, the keys are shipped. For security reasons you might want to set it to FALSE and disable offloading for encrypted tablespaces. Note that this parameter can only be changed at the instance level.</p>
]]></content:encoded>
			<wfw:commentRss>http://antognini.ch/2010/08/exadata-storage-server-and-the-query-optimizer-%e2%80%93-part-4/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Partition-Wise Join of List-Partitioned Tables</title>
		<link>http://antognini.ch/2010/08/partition-wise-join-of-list-partitioned-tables/</link>
		<comments>http://antognini.ch/2010/08/partition-wise-join-of-list-partitioned-tables/#comments</comments>
		<pubDate>Mon, 02 Aug 2010 09:59:08 +0000</pubDate>
		<dc:creator>Christian Antognini</dc:creator>
				<category><![CDATA[10gR1]]></category>
		<category><![CDATA[10gR2]]></category>
		<category><![CDATA[11gR1]]></category>
		<category><![CDATA[11gR2]]></category>
		<category><![CDATA[Partitioning]]></category>
		<category><![CDATA[Query Optimizer]]></category>

		<guid isPermaLink="false">http://antognini.ch/?p=1202</guid>
		<description><![CDATA[When two tables are equi-partitioned on their join keys, the query optimizer is able to take advantage of partition-wise joins. To make sure that the tables are equi-partitioned, as of Oracle Database 11g reference partitioning can be used. In fact, per definition, with reference partitioning all “related” tables have exactly the same partitioning schema. If [...]]]></description>
			<content:encoded><![CDATA[<p>When two tables are equi-partitioned on their join keys, the query optimizer is able to take advantage of partition-wise joins. To make sure that the tables are equi-partitioned, as of Oracle Database 11g reference partitioning can be used. In fact, per definition, with reference partitioning all “related” tables have exactly the same partitioning schema. If you are not using reference partitioning, you must be very careful that the tables are effectively partitioned in very same way. For range and hash partitioned tables this is usually not a problem. However, when using list partitioning, it is quite easy to make a mistake. The reason is that the partitions can be defined in any order. Let’s have a look to an example based on the following two tables.</p>
<p><pre>SQL&gt; CREATE TABLE t1p
  2  PARTITION BY LIST (pkey) (
  3    PARTITION p_0 VALUES (0),
  4    PARTITION p_1 VALUES (1),
  5    PARTITION p_2 VALUES (2),
  6    PARTITION p_3 VALUES (3),
  7    PARTITION p_4 VALUES (4),
  8    PARTITION p_5 VALUES (5),
  9    PARTITION p_6 VALUES (6),
 10    PARTITION p_7 VALUES (7),
 11    PARTITION p_8 VALUES (8),
 12    PARTITION p_9 VALUES (9)
 13  )
 14  AS
 15  SELECT rownum AS num, mod(rownum,10) AS pkey, dbms_random.string(&#039;p&#039;,50) AS pad
 16  FROM dual
 17  CONNECT BY level &lt;= 10000;

SQL&gt; CREATE TABLE t2p
  2  PARTITION BY LIST (pkey) (
  3    PARTITION p_0 VALUES (0),
  4    PARTITION p_1 VALUES (1),
  5    PARTITION p_2 VALUES (2),
  6    PARTITION p_3 VALUES (3),
  7    PARTITION p_5 VALUES (5),
  8    PARTITION p_4 VALUES (4),
  9    PARTITION p_6 VALUES (6),
 10    PARTITION p_7 VALUES (7),
 11    PARTITION p_8 VALUES (8),
 12    PARTITION p_9 VALUES (9)
 13  )
 14  AS
 15  SELECT rownum AS num, mod(rownum,10) AS pkey, dbms_random.string(&#039;p&#039;,50) AS pad
 16  FROM dual
 17  CONNECT BY level &lt;= 10000;

SQL&gt; BEGIN
  2    dbms_stats.gather_table_stats(user,&#039;t1p&#039;);
  3    dbms_stats.gather_table_stats(user,&#039;t2p&#039;);
  4  END;
  5  /</pre></p>
<p>Even though they are logically equivalent, as shown in the following execution plan, with them partition-wise joins cannot be used.</p>
<p><pre>SQL&gt; EXPLAIN PLAN FOR SELECT * FROM t1p JOIN t2p USING (num, pkey);

SQL&gt; SELECT * FROM table(dbms_xplan.display(format=&gt;&#039;basic&#039;));

PLAN_TABLE_OUTPUT
------------------------------------

Plan hash value: 3059592055

------------------------------------
| Id  | Operation           | Name |
------------------------------------
|   0 | SELECT STATEMENT    |      |
|   1 |  HASH JOIN          |      |
|   2 |   PARTITION LIST ALL|      |
|   3 |    TABLE ACCESS FULL| T1P  |
|   4 |   PARTITION LIST ALL|      |
|   5 |    TABLE ACCESS FULL| T2P  |
------------------------------------</pre></p>
<p>The difference in the order of the partitions can also be confirmed by a query like the following one.</p>
<p><pre>SQL&gt; SELECT t1p.high_value,
  2         t1p.partition_position AS pos_t1p,
  3         t2p.partition_position AS pos_t2p,
  4         decode(t1p.partition_position, t2p.partition_position, &#039;Y&#039;, &#039;N&#039;) AS equal
  5  FROM user_tab_partitions t1p JOIN user_tab_partitions t2p ON t1p.partition_name = t2p.partition_name
  6  WHERE t1p.table_name = &#039;T1P&#039;
  7  AND t2p.table_name = &#039;T2P&#039;;

HIGH_VALUE   POS_T1P  POS_T2P EQUAL
----------- -------- -------- ------
0                  1        1 Y
1                  2        2 Y
2                  3        3 Y
3                  4        4 Y
5                  6        5 N
4                  5        6 N
6                  7        7 Y
7                  8        8 Y
8                  9        9 Y
9                 10       10 Y</pre></p>
<p>It goes without saying that to solve the problem it is necessary to reorder the partitions. To do so it is enough to move the out-of-order partitions. To avoid a double storage of the data a series of ALTER TABLE EXCHANGE/DROP/ADD/EXCHANGE statements can be used.</p>
<ul>
<li>Move the P5 partition of the T1P table</li>
</ul>
<p><pre>SQL&gt; CREATE TABLE t1p_5 AS
  2  SELECT *
  3  FROM t1p PARTITION (p_5)
  4  WHERE 1 = 0;

SQL&gt; ALTER TABLE t1p EXCHANGE PARTITION p_5 WITH TABLE t1p_5;

SQL&gt; ALTER TABLE t1p DROP PARTITION p_5;

SQL&gt; ALTER TABLE t1p ADD PARTITION p_5 VALUES (5);

SQL&gt; ALTER TABLE t1p EXCHANGE PARTITION p_5 WITH TABLE t1p_5;

SQL&gt; DROP TABLE t1p_5 PURGE;</pre></p>
<ul>
<li>Move the P5 partition of the T2P table</li>
</ul>
<p><pre>SQL&gt; CREATE TABLE t2p_5 AS
  2  SELECT *
  3  FROM t2p PARTITION (p_5)
  4  WHERE 1 = 0;

SQL&gt; ALTER TABLE t2p EXCHANGE PARTITION p_5 WITH TABLE t2p_5;

SQL&gt; ALTER TABLE t2p DROP PARTITION p_5;

SQL&gt; ALTER TABLE t2p ADD PARTITION p_5 VALUES (5);

SQL&gt; ALTER TABLE t2p EXCHANGE PARTITION p_5 WITH TABLE t2p_5;

SQL&gt; DROP TABLE t2p_5 PURGE;</pre></p>
<ul>
<li>Check whether the order is ok</li>
</ul>
<p><pre>SQL&gt; SELECT t1p.high_value,
  2         t1p.partition_position AS pos_t1p,
  3         t2p.partition_position AS pos_t2p,
  4         decode(t1p.partition_position, t2p.partition_position, &#039;Y&#039;, &#039;N&#039;) AS equal
  5  FROM user_tab_partitions t1p JOIN user_tab_partitions t2p ON t1p.partition_name = t2p.partition_name
  6  WHERE t1p.table_name = &#039;T1P&#039;
  7  AND t2p.table_name = &#039;T2P&#039;;

HIGH_VALUE   POS_T1P  POS_T2P EQUAL
----------- -------- -------- ------
0                  1        1 Y
1                  2        2 Y
2                  3        3 Y
3                  4        4 Y
4                  5        5 Y
6                  6        6 Y
7                  7        7 Y
8                  8        8 Y
9                  9        9 Y
5                 10       10 Y</pre></p>
<p>After these operations partition-wise joins are allowed. The following execution plan confirms this. </p>
<p><pre>SQL&gt; SELECT * FROM table(dbms_xplan.display(format=&gt;&#039;basic&#039;));

PLAN_TABLE_OUTPUT
------------------------------------

Plan hash value: 1324269388

------------------------------------
| Id  | Operation           | Name |
------------------------------------
|   0 | SELECT STATEMENT    |      |
|   1 |  PARTITION LIST ALL |      |
|   2 |   HASH JOIN         |      |
|   3 |    TABLE ACCESS FULL| T1P  |
|   4 |    TABLE ACCESS FULL| T2P  |
------------------------------------</pre></p>
]]></content:encoded>
			<wfw:commentRss>http://antognini.ch/2010/08/partition-wise-join-of-list-partitioned-tables/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Related-Combine Operation „UNION ALL (RECURSIVE WITH)“</title>
		<link>http://antognini.ch/2010/06/related-combine-operation-union-all-recursive-with/</link>
		<comments>http://antognini.ch/2010/06/related-combine-operation-union-all-recursive-with/#comments</comments>
		<pubDate>Thu, 10 Jun 2010 06:53:37 +0000</pubDate>
		<dc:creator>Christian Antognini</dc:creator>
				<category><![CDATA[11gR2]]></category>
		<category><![CDATA[Query Optimizer]]></category>
		<category><![CDATA[TOP]]></category>

		<guid isPermaLink="false">http://antognini.ch/?p=1160</guid>
		<description><![CDATA[To make easier the interpretation of execution plans, in chapter 6 of TOP I defined three types of operations: standalone operations, unrelated-combine operations, and related-combine operations. For combine operations I also added a list of all operations of each type. Since in 11.2 a new related-combine operation is available, I decided to write this short [...]]]></description>
			<content:encoded><![CDATA[<p>To make easier the interpretation of execution plans, in chapter 6 of <a href="/top">TOP</a> I defined three types of operations: standalone operations, unrelated-combine operations, and related-combine operations. For combine operations I also added a list of all operations of each type. Since in 11.2 a new related-combine operation is available, I decided to write this short post as addenda to the content of the book.</p>
<p>The new related-combine operation, named “UNION ALL (RECURSIVE WITH)”, is available to support the new recursive <a href="http://download.oracle.com/docs/cd/E11882_01/server.112/e10592/statements_10002.htm#i2077142">subquery factoring clause</a>. Hence, it is used for hierarchical queries. The following query and its execution plan show an example:<br />
<pre>SQL&gt; WITH
  2    e (xlevel, empno, ename, job, mgr, hiredate, sal, comm, deptno)
  3    AS (
  4      SELECT 1, empno, ename, job, mgr, hiredate, sal, comm, deptno
  5      FROM emp
  6      WHERE mgr IS NULL
  7      UNION ALL
  8      SELECT mgr.xlevel+1, emp.empno, emp.ename, emp.job, emp.mgr, emp.hiredate, emp.sal, emp.comm, emp.deptno
  9      FROM emp, e mgr
 10      WHERE emp.mgr = mgr.empno
 11    )
 12  SELECT *
 13  FROM e;

-------------------------------------------------------------------------------
| Id  | Operation                                 | Name    | Starts | A-Rows |
-------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                          |         |      1 |     14 |
|   1 |  VIEW                                     |         |      1 |     14 |
|   2 |   UNION ALL (RECURSIVE WITH) BREADTH FIRST|         |      1 |     14 |
|*  3 |    TABLE ACCESS FULL                      | EMP     |      1 |      1 |
|   4 |    NESTED LOOPS                           |         |      4 |     13 |
|   5 |     NESTED LOOPS                          |         |      4 |     13 |
|   6 |      RECURSIVE WITH PUMP                  |         |      4 |     14 |
|*  7 |      INDEX RANGE SCAN                     | EMP_MGR |     14 |     13 |
|   8 |     TABLE ACCESS BY INDEX ROWID           | EMP     |     13 |     13 |
-------------------------------------------------------------------------------

   3 - filter(&quot;MGR&quot; IS NULL)
   7 - access(&quot;EMP&quot;.&quot;MGR&quot;=&quot;MGR&quot;.&quot;EMPNO&quot;)
       filter(&quot;EMP&quot;.&quot;MGR&quot; IS NOT NULL)</pre></p>
<p>Notice that there are actually two operations:</p>
<ul>
<li>UNION ALL (RECURSIVE WITH) BREADTH FIRST</li>
<li>UNION ALL (RECURSIVE WITH) DEPTH FIRST</li>
</ul>
<p>As their name suggest, the difference is due to the <a href="http://download.oracle.com/docs/cd/E11882_01/server.112/e10592/statements_10002.htm#BCEDDGGE">search clause</a> that you can set to either BREADTH FIRST BY or DEPTH FIRST BY.</p>
<p>Reading an execution plan containing the “UNION ALL (RECURSIVE WITH)” operation is the same as reading one containing the “CONNECT BY WITH FILTERING” operation. As a matter of fact, the purpose of both operations is basically the same. Just notice that also the “PUMP” operation used in the execution plan differs. While in the former it is called “RECURSIVE WITH PUMP”, in the latter it is called “CONNECT BY PUMP”. But the difference, for the purpose of reading the execution plan, does not matter.</p>
<p>You find a full description on how to read such an execution plan in <a href="/2008/06/operation-connect-by-with-filtering/">this post</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://antognini.ch/2010/06/related-combine-operation-union-all-recursive-with/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Evolution of a SQL Plan Baseline Based on a DELETE Statement</title>
		<link>http://antognini.ch/2010/06/evolution-of-a-sql-plan-baseline-based-on-a-delete-statement/</link>
		<comments>http://antognini.ch/2010/06/evolution-of-a-sql-plan-baseline-based-on-a-delete-statement/#comments</comments>
		<pubDate>Mon, 07 Jun 2010 14:21:26 +0000</pubDate>
		<dc:creator>Christian Antognini</dc:creator>
				<category><![CDATA[11gR1]]></category>
		<category><![CDATA[11gR2]]></category>
		<category><![CDATA[Query Optimizer]]></category>
		<category><![CDATA[SQL Trace]]></category>

		<guid isPermaLink="false">http://antognini.ch/?p=1139</guid>
		<description><![CDATA[During an evolution the database engine compares the performance of two execution plans. The aim is to find out which one provides the better performance. For that purpose it has to run the SQL statement on which the SQL plan baseline is based and compare some execution statistics. The following output of the DBMS_SPM.EVOLVE_SQL_PLAN_BASELINE function [...]]]></description>
			<content:encoded><![CDATA[<p>During an evolution the database engine compares the performance of two execution plans. The aim is to find out which one provides the better performance. For that purpose it has to run the SQL statement on which the SQL plan baseline is based and compare some execution statistics. The following output of the DBMS_SPM.EVOLVE_SQL_PLAN_BASELINE function shows which statistics are compared.</p>
<p><pre>Plan was verified: Time used .06 seconds.
Plan passed performance criterion: 360.12 times better than baseline plan.
Plan was changed to an accepted plan.

                          Baseline Plan      Test Plan       Stats Ratio
                          -------------      ---------       -----------
Execution Status:              COMPLETE       COMPLETE
Rows Processed:                     100            100
Elapsed Time(ms):                 2.173           .033             65.85
CPU Time(ms):                     2.444              0
Buffer Gets:                        720              2               360
Physical Read Requests:               0              0
Physical Write Requests:              0              0
Physical Read Bytes:                  0              0
Physical Write Bytes:                 0              0
Executions:                           1              1</pre></p>
<p>For queries a regular execution can be performed. But, what happens for INSERT/UPDATE/MERGE/DELETE statements? Do the SQL engine really execute them and modify data?</p>
<p>To answer these questions let’s have a look to an example based on a DELETE statement…</p>
<ul>
<li>Setup a table used for the test:</li>
</ul>
<p><pre>SQL&gt; CREATE TABLE t (id, n, pad, CONSTRAINT t_pk PRIMARY KEY (id))
  2  AS
  3  SELECT rownum, mod(rownum,100), rpad(&#039;*&#039;,500,&#039;*&#039;)
  4  FROM dual
  5  CONNECT BY level &lt;= 10000;

SQL&gt; execute dbms_stats.gather_table_stats(ownname =&gt; user, tabname =&gt; &#039;t&#039;, method_opt =&gt; &#039;for all columns size 254&#039;)</pre></p>
<ul>
<li>Create a SQL plan baseline:</li>
</ul>
<p><pre>SQL&gt; ALTER SESSION SET optimizer_capture_sql_plan_baselines = TRUE;

SQL&gt; DELETE t WHERE n = 42;

SQL&gt; ROLLBACK;

SQL&gt; DELETE t WHERE n = 42;

SQL&gt; ROLLBACK;

SQL&gt; ALTER SESSION SET optimizer_capture_sql_plan_baselines = FALSE;</pre></p>
<ul>
<li>Add a non-accepted execution plan to the SQL plan baseline:</li>
</ul>
<p><pre>SQL&gt; CREATE INDEX i ON t (n);

SQL&gt; DELETE t WHERE n = 42;

SQL&gt; ROLLBACK;

SQL&gt; DELETE t WHERE n = 42;

SQL&gt; ROLLBACK;</pre></p>
<ul>
<li>Display the content of the SQL plan baseline (notice that two execution plans are available):</li>
</ul>
<p><pre>SQL&gt; SELECT * FROM table(dbms_xplan.display_sql_plan_baseline(&#039;SYS_SQL_373d78bbba048c24&#039;, NULL, &#039;basic&#039;));

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
SQL handle: SYS_SQL_373d78bbba048c24
SQL text: DELETE t WHERE n = 42
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
Plan name: SQL_PLAN_3fgbsrfx093143bad20a0         Plan id: 1001201824
Enabled: YES     Fixed: NO      Accepted: YES     Origin: AUTO-CAPTURE
--------------------------------------------------------------------------------

Plan hash value: 3335594643

-----------------------------------
| Id  | Operation          | Name |
-----------------------------------
|   0 | DELETE STATEMENT   |      |
|   1 |  DELETE            | T    |
|   2 |   TABLE ACCESS FULL| T    |
-----------------------------------

--------------------------------------------------------------------------------
Plan name: SQL_PLAN_3fgbsrfx093144198692b         Plan id: 1100507435
Enabled: YES     Fixed: NO      Accepted: NO      Origin: AUTO-CAPTURE
--------------------------------------------------------------------------------

Plan hash value: 1582806765

----------------------------------
| Id  | Operation         | Name |
----------------------------------
|   0 | DELETE STATEMENT  |      |
|   1 |  DELETE           | T    |
|   2 |   INDEX RANGE SCAN| I    |
----------------------------------</pre></p>
<ul>
<li>Trace the evolution to find out what happens (notice that I deleted the output of the function because it is the one it is shown at the top of this post):</li>
</ul>
<p><pre>SQL&gt; execute dbms_monitor.session_trace_enable(plan_stat=&gt;&#039;ALL_EXECUTIONS&#039;)

SQL&gt; SELECT dbms_spm.evolve_sql_plan_baseline(
  2           sql_handle =&gt; &#039;SYS_SQL_373d78bbba048c24&#039;,
  3           plan_name  =&gt; &#039;&#039;,
  4           time_limit =&gt; 10,
  5           verify     =&gt; &#039;yes&#039;,
  6           commit     =&gt; &#039;yes&#039;
  7         )
  8  FROM dual;

SQL&gt; execute dbms_monitor.session_trace_disable

SQL&gt; SELECT value
  2  FROM v$diag_info
  3  WHERE name = &#039;Default Trace File&#039;;

VALUE
-------------------------------------------------------------------
/u00/app/oracle/diag/rdbms/dba112/DBA112/trace/DBA112_ora_17200.trc</pre></p>
<p>Now that the trace file was generated, let&#8217;s have a look to its content. The relevant parts are two: the first one is related to the execution of the accepted execution plan, and the second one is related to the execution of the non-accepted one.</p>
<p><pre>PARSING IN CURSOR #11 len=45 dep=1 uid=90 oct=7 lid=90 tim=1275524159625080 hv=4077337184 ad=&#039;325c9f10&#039; sqlid=&#039;5fwyncmthffm0&#039;
/* SQL Analyze(25,0) */ DELETE t WHERE n = 42
END OF STMT
PARSE #11:c=1000,e=652,p=0,cr=0,cu=0,mis=1,r=0,dep=1,og=1,plh=1001201824,tim=1275524159625078
EXEC #11:c=4999,e=5670,p=0,cr=720,cu=0,mis=0,r=0,dep=1,og=1,plh=1001201824,tim=1275524159630752
EXEC #11:c=2000,e=1718,p=0,cr=720,cu=0,mis=0,r=0,dep=1,og=1,plh=1001201824,tim=1275524159632613
EXEC #11:c=2000,e=1511,p=0,cr=720,cu=0,mis=0,r=0,dep=1,og=1,plh=1001201824,tim=1275524159634156
EXEC #11:c=2000,e=1542,p=0,cr=720,cu=0,mis=0,r=0,dep=1,og=1,plh=1001201824,tim=1275524159636144
EXEC #11:c=2000,e=1552,p=0,cr=720,cu=0,mis=0,r=0,dep=1,og=1,plh=1001201824,tim=1275524159638151
EXEC #11:c=3998,e=4015,p=0,cr=720,cu=0,mis=0,r=0,dep=1,og=1,plh=1001201824,tim=1275524159642613
EXEC #11:c=3000,e=2905,p=0,cr=720,cu=0,mis=0,r=0,dep=1,og=1,plh=1001201824,tim=1275524159645549
EXEC #11:c=2000,e=1506,p=0,cr=720,cu=0,mis=0,r=0,dep=1,og=1,plh=1001201824,tim=1275524159647151
EXEC #11:c=2000,e=1562,p=0,cr=720,cu=0,mis=0,r=0,dep=1,og=1,plh=1001201824,tim=1275524159649160
EXEC #11:c=2999,e=2440,p=0,cr=720,cu=0,mis=0,r=0,dep=1,og=1,plh=1001201824,tim=1275524159652037
CLOSE #11:c=0,e=3,dep=1,type=0,tim=1275524159652065</pre></p>
<p><pre>PARSING IN CURSOR #5 len=45 dep=1 uid=90 oct=7 lid=90 tim=1275524159657503 hv=4077337184 ad=&#039;325c9f10&#039; sqlid=&#039;5fwyncmthffm0&#039;
/* SQL Analyze(25,0) */ DELETE t WHERE n = 42
END OF STMT
PARSE #5:c=1000,e=859,p=0,cr=0,cu=0,mis=1,r=0,dep=1,og=1,plh=1100507435,tim=1275524159657499
EXEC #5:c=0,e=52,p=0,cr=2,cu=0,mis=0,r=0,dep=1,og=1,plh=1100507435,tim=1275524159657625
EXEC #5:c=0,e=0,p=0,cr=2,cu=0,mis=0,r=0,dep=1,og=1,plh=1100507435,tim=1275524159657647
EXEC #5:c=0,e=31,p=0,cr=2,cu=0,mis=0,r=0,dep=1,og=1,plh=1100507435,tim=1275524159657972
EXEC #5:c=0,e=5,p=0,cr=2,cu=0,mis=0,r=0,dep=1,og=1,plh=1100507435,tim=1275524159658071
EXEC #5:c=0,e=0,p=0,cr=2,cu=0,mis=0,r=0,dep=1,og=1,plh=1100507435,tim=1275524159658071
EXEC #5:c=0,e=0,p=0,cr=2,cu=0,mis=0,r=0,dep=1,og=1,plh=1100507435,tim=1275524159658071
EXEC #5:c=0,e=0,p=0,cr=2,cu=0,mis=0,r=0,dep=1,og=1,plh=1100507435,tim=1275524159658071
EXEC #5:c=0,e=0,p=0,cr=2,cu=0,mis=0,r=0,dep=1,og=1,plh=1100507435,tim=1275524159658071
EXEC #5:c=0,e=0,p=0,cr=2,cu=0,mis=0,r=0,dep=1,og=1,plh=1100507435,tim=1275524159658071
EXEC #5:c=0,e=0,p=0,cr=2,cu=0,mis=0,r=0,dep=1,og=1,plh=1100507435,tim=1275524159658071
CLOSE #5:c=0,e=0,dep=1,type=0,tim=1275524159658071</pre></p>
<p>In the previous output notice that:</p>
<ul>
<li>The PLH attribute of the EXEC lines shows that two execution plans are used.</li>
<li>Each execution plan was executed 10 times (in practice the number varies according to the elapsed time; i.e. for longer executions a single run might be enough to determine whether an execution plan has to be accepted).</li>
<li>Even though I set the PLAN_STAT parameter to ALL_EXECUTIONS (if you don’t know what the PLAN_STAT parameter is for, have a look to <a href="/2009/02/11g-new-feature-in-dbms_monitor/">this post</a>) the STAT lines (the execution plan) are not available in the trace file.</li>
</ul>
<p>According to this information the SQL statement is executed. But, if you check the table after the evolution, the data is still there. And that, honestly, is not an option! In addition, no ROLLBACK is executed (no XCTEND lines are present in the trace file). So, it seems that the SQL statement is <em>not</em> executed.</p>
<p>What I really miss in the trace file are the execution plans associated to the executions to check what the different operations of the execution plan did. The only way I found to have them, it is to add the GATHER_PLAN_STATISTICS hint into the SQL statement itself (also setting the STATISTICS_LEVEL parameter and checking a view like V$SQL_PLAN_STATISTICS_ALL did not help). The content of the trace file, formatted by <a href="/category/apmtools/tvdxtat/">TVD$XTAT</a>, is the following:</p>
<p><pre>Optimizer Mode       ALL_ROWS
Hash Value           1001201824
Number of Executions 10

        Rows Operation
------------ ---------------------------------------------------------------------------------------
           0 DELETE  T (cr=720 pr=25 pw=0 time=0 us)
           0   TABLE ACCESS FULL T (cr=720 pr=25 pw=0 time=0 us cost=84 size=700 card=100)

Optimizer Mode       ALL_ROWS
Hash Value           1100507435
Number of Executions 10

        Rows Operation
------------ ---------------------------------------------------------------------------------------
           0 DELETE  T (cr=2 pr=0 pw=0 time=0 us)
           0   INDEX RANGE SCAN I (cr=2 pr=0 pw=0 time=0 us cost=1 size=700 card=100) (object id 93840)</pre></p>
<p>Notice that while the number of logical reads (CR attribute) matches the report generated by the evolution, the number of rows returned by both steps of the execution plans is 0. And that, even though the index range scan should return 100 rows.</p>
<p>In summary, during an evolution the SQL engine processes the SQL statements in a special way. The data is accessed, but not modified. Hence, SQL statements are only partially executed. I do not regard this fact as a problem, though. In fact, the operations that modify data should always perform the same work independently on how the data to be modified is located (in the example given here, either with a full table scan or an index range scan).</p>
]]></content:encoded>
			<wfw:commentRss>http://antognini.ch/2010/06/evolution-of-a-sql-plan-baseline-based-on-a-delete-statement/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

