{
  "date": "2014-04-08T11:12:26",
  "slug": "best-practice-to-send-an-oracle-execution-plan",
  "link": "https://www.dbi-services.com/blog/best-practice-to-send-an-oracle-execution-plan/",
  "title": {
    "rendered": "Best practice for the sending of an Oracle execution plan"
  },
  "content": {
    "rendered": "<h2>By Franck Pachot</h2>\n<p>.<br />\nYou have a query that takes too long and you want help to analyze the execution plan? Then you need to get it with relevant information, and correctly formatted. Autotrace is not a good option as it does not bind the variables in the same way as your application. Explain plan only shows estimations, but if we have a performance issue, this probably means that the estimation is wrong. I prefer SQL Monitoring when we have Tuning Pack, or the cursor plan with row-source statistics in all other cases. If you post it in a forum, don&#8217;t forget to keep it formatted or it&#8217;s impossible to read.</p>\n<p>Here are the two ways I prefer to get an execution plan, depending on whether you have the tuning pack licence or not.</p>\n<h3>Method 1 &#8211; Without Tuning Pack</h3>\n<p>I set the sqlplus environment to nicely spool to text file and set the STATISTICS_LEVEL to ALL in order to gather plan execution statistics:</p>\n<pre><code>\nset pagesize 10000 linesize 300 trimspool on serveroutput off\nalter session set statistics_level=all;\n</code></pre>\n<p>Then I execute the query. Don&#8217;t forget to set the current_schema to the user that executes the statement, and to bind variables:</p>\n<pre><code>\n-- set the schema\nalter session set current_schema=SCOTT;\n-- define variables\nvariable minimum_salary number\n-- bind values\nexec :minimum_salary := 3000\n-- execute the query\nSELECT * FROM DEPT JOIN EMP USING(DEPTNO) WHERE SAL&gt;:minimum_salary;\n</code></pre>\n<p>Finally I get the execution plan to a text file:</p>\n<pre><code>\nspool plan.txt\nselect * from table(dbms_xplan.display_cursor(format=&gt;'allstats last +outline +peeked_binds +cost'));\nspool off\n</code></pre>\n<p>The plan will have both the estimations (E-Rows) and the actual number of rows (A-Rows) from the last execution. Note that if it is a parallel query statement, you must omit the &#8216;last&#8217; in the format or you will have statistics only for the coordinator process.</p>\n<h3>Method 2 &#8211; With Tuning Pack</h3>\n<p>When you have tuning pack, you have access to the great SQL monitoring feature.</p>\n<pre><code>\nSQL&gt; show parameter pack\nNAME                                 TYPE        VALUE\n ------------------------------------ ----------- ------------------------------\n control_management_pack_access       string      DIAGNOSTIC+TUNING\n</code></pre>\n<p>I set the sqlplus environment to nicely spool to html file:</p>\n<pre><code>\nset pagesize 0 linesize 10000 trimspool on serveroutput off long 1000000 longc 1000000 echo off feedback off\n</code></pre>\n<p>Then I execute the query. Don&#8217;t forget to set the current_schema to the user that executes the statement, and to bind variables.</p>\n<p>One difference here: the MONITOR hint to force SQL Monitoring.</p>\n<pre><code>\n-- set the schema\nalter session set current_schema=SCOTT;\n-- define variables\nvariable minimum_salary number\n-- bind values\nexec :minimum_salary := 3000\n-- execute the query\nSELECT /*+ monitor */ * FROM DEPT JOIN EMP USING(DEPTNO) WHERE SAL&gt;:minimum_salary;</code></pre>\n<p>Finally, get the execution plan to a html file:</p>\n<pre><code>\n-- in 12c avoid compression of xml because there is additional information:\nalter session set events='emx_control compress_xml=none';\n-- errors with French NLS_LANG\nalter session set NLS_NUMERIC_CHARACTERS=',.';\n</code></pre>\n<pre><code>\nspool plan.htm\nselect dbms_sqltune.report_sql_monitor(report_level=&gt;'all',type=&gt;'active') from dual;\nspool off\n</code></pre>\n<p>The html file is very small but will be rendered by an online flash automatically loaded from the oracle.com website.</p>\n<p>You can see both output, plan.txt, and plan.htm from this zip: <a title=\"title\" href=\"http://dbi-services.com/blog/images/easyblog_images/139/xplans.zip\" target=\"_self\" rel=\"noopener noreferrer\">xplans.zip</a></p>\n<p>Here is how they look like (but please never send me screenshots of execution plans&#8230;):</p>\n<p><a class=\"easyblog-thumb-preview\" title=\"CapturePlantxt.PNG\" href=\"https://www.dbi-services.com/blog/wp-content/uploads/sites/2/2022/04/CapturePlantxt.png\"><img decoding=\"async\" title=\"CapturePlantxt.PNG\" src=\"https://www.dbi-services.com/blog/wp-content/uploads/sites/2/2022/04/CapturePlantxt.png\" alt=\"CapturePlantxt.PNG\" /></a></p>\n<p>And the colorful active report from SQL Monitoring:</p>\n<p><a class=\"easyblog-thumb-preview\" title=\"CapturePlanHtm.PNG\" href=\"https://www.dbi-services.com/blog/wp-content/uploads/sites/2/2022/04/CapturePlanHtm.png\"><img decoding=\"async\" title=\"CapturePlanHtm.PNG\" src=\"https://www.dbi-services.com/blog/wp-content/uploads/sites/2/2022/04/CapturePlanHtm.png\" alt=\"CapturePlanHtm.PNG\" /></a></p>\n<p>The goal of this blog post is to simply (copy/paste) the formatting and the plan gathering code, so if you have any improvement ideas, please share.</p>\n<h3>Update 25-JUL-2014</h3>\n<p>Thanks to Tyler Muth (<a href=\"https://twitter.com/tmuth\">@tmuth</a>) I added the emx_control event to avoid compression of xml in 12c. See <a href=\"https://github.com/tmuth/Query-Test-Framework/blob/master/source/query-capture.sql#L78\">his Query Test Framework</a></p>\n",
    "protected": false
  }
}
