Oracle 优化篇+使用DBMS包查看SQL的执行计划

说明:本文为Oracle 11gR2中使用DBMS包查看SQL执行计划的指导手册
用途:本文只是提供一种方法和思路,更多更高级的使用方法还期待你不断的探索哦
标签:dbms_sqltune.report_sql_monitor、查看SQL的执行计划、Real-Time SQL Monitoring、
温馨提示:如果您发现本文哪里写的有问题或者有更好的写法请留言或私信我进行修改优化

★ 执行测试SQL
※ 温馨提示:添加hint-monitor是为了让Oracle收集所有的SQL,否则默认只收集耗费资源的SQL

sqlplus / as sysdba
select /*tag zzt_003*//*+ monitor */ *
  from scott.emp e, scott.dept d
 where e.deptno = d.deptno
   and empno > 7800;


★ 查看业务SQL信息(本例中SQL_ID:2kzfxk9vacjh1)

sqlplus / as sysdba
select * from v$sqlarea a where lower(a.SQL_TEXT) like '%/*tag zzt_003*/%';


★ 执行完SQL后使用官方工具Real-Time SQL Monitoring来获取SQL的monitor信息
※ Real-Time SQL Monitoring(主要内容:执行计划)
※ 官档:https://www.oracle.com/database/technologies/real-time-sql-monitoring.html#copy
※ 优化达人刘老虎的(ora monsave)就是采用了该方法
※ type参数常用active,还可以选:txt/html/xml

sqlplus / as sysdba
set trimspool on
set trim on
set pages 0
set linesize 1000
set long 1000000
set longchunksize 1000000
spool sqlmon_active.html
select dbms_sqltune.report_sql_monitor(sql_id       => '2kzfxk9vacjh1',
                                       report_level => 'all',
                                       type         => 'active') as report  from dual;
spool off


★ HTML输出案例截图

★ HTML输出案例

<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <base href="http://download.oracle.com/otn_software/"/>
  <script language="javascript" type="text/javascript" src="emviewers/scripts/flashver.js">
   <!--Test flash version-->
  </script>
  <style>
      body { margin: 0px; overflow:hidden }
    </style>
 </head>
 <body scroll="no">
  <script type="text/xml">
   <!--FXTMODEL-->
   <report db_version="11.2.0.3.0" elapsed_time="0.06" cpu_time="0.06">
    <report_id><![CDATA[/orarep/sqlmonitor/main?bucket_max_count=128&event_detail=yes&report_level=all&sql_id=2kzfxk9vacjh1]]></report_id>
    <sql_monitor_report version="4.0" sysdate="05/20/2020 12:50:46">
     <report_parameters>
      <sql_id>2kzfxk9vacjh1</sql_id>
      <bucket_count>2</bucket_count>
      <interval_start>05/20/2020 12:50:46</interval_start>
      <interval_end>05/20/2020 12:50:47</interval_end>
     </report_parameters>
     <target instance_id="1" session_id="22" session_serial="247" sql_id="2kzfxk9vacjh1" sql_exec_start="05/20/2020 12:50:46" sql_exec_id="16777216" sql_plan_hash="817787759" db_unique_name="orcl" db_platform_name="Linux x86 64-bit" report_host_name="localhost.localdomain">
      <user_id>0</user_id>
      <user>SYS</user>
      <program>[email protected] (TNS V1-V3)</program>
      <module>[email protected] (TNS V1-V3)</module>
      <service>SYS$USERS</service>
      <sql_fulltext is_full="Y">select /*tag zzt003*//*+ monitor */ *
  from scott.emp e, scott.dept d
 where e.deptno = d.deptno
   and empno &gt; 7800</sql_fulltext>
      <status>DONE (ALL ROWS)</status>
      <refresh_count>5</refresh_count>
      <first_refresh_time>05/20/2020 12:50:46</first_refresh_time>
      <last_refresh_time>05/20/2020 12:50:46</last_refresh_time>
      <duration>.000219</duration>
      <optimizer_env>
       <param name="active_instance_count">1</param>
       <param name="is_recur_flags">0</param>
       <param name="parallel_autodop">0</param>
       <param name="parallel_ddl_mode">enabled</param>
       <param name="parallel_ddldml">0</param>
       <param name="parallel_degree">0</param>
       <param name="parallel_dml_mode">disabled</param>
       <param name="parallel_execution_enabled">true</param>
       <param name="parallel_max_degree">4</param>
       <param name="parallel_query_default_dop">0</param>
       <param name="parallel_query_mode">enabled</param>
       <param name="pga_aggregate_target">368640 KB</param>
       <param name="sqlstat_enabled">true</param>
       <param name="statistics_level">all</param>
       <param name="total_cpu_count">2</param>
      </optimizer_env>
     </target>
     <stats type="monitor">
      <stat name="elapsed_time">219</stat>
      <stat name="other_wait_time">219</stat>
      <stat name="user_fetch_count">2</stat>
      <stat name="buffer_gets">6</stat>
     </stats>
     <plan>
      <operation name="SELECT STATEMENT" id="0" depth="0" pos="5">
       <cost>5</cost>
      </operation>
      <operation name="MERGE JOIN" id="1" depth="1" pos="1">
       <card>3</card>
       <bytes>174</bytes>
       <cost>5</cost>
       <io_cost>4</io_cost>
       <cpu_cost>21053326</cpu_cost>
       <time>00:00:01 </time>
       <project>&quot;D&quot;.&quot;DEPTNO&quot;[NUMBER,22], &quot;E&quot;.&quot;DEPTNO&quot;[NUMBER,22], &quot;D&quot;.&quot;LOC&quot;[VARCHAR2,13], &quot;D&quot;.&quot;DNAME&quot;[VARCHAR2,14], &quot;EMPNO&quot;[NUMBER,22], &quot;E&quot;.&quot;ENAME&quot;[VARCHAR2,10], &quot;E&quot;.&quot;JOB&quot;[VARCHAR2,9], &quot;E&quot;.&quot;MGR&quot;[NUMBER,22], &quot;E&quot;.&quot;HIREDATE&quot;[DATE,7], &quot;E&quot;.&quot;SAL&quot;[NUMBER,22], &quot;E&quot;.&quot;COMM&quot;[NUMBER,22]</project>
       <qblock>SEL$1</qblock>
       <other_xml>
        <info type="db_version">11.2.0.3</info>
        <info type="parse_schema"><![CDATA["SYS"]]></info>
        <info type="plan_hash">817787759</info>
        <info type="plan_hash_2">1586662073</info>
        <outline_data>
         <hint><![CDATA[IGNORE_OPTIM_EMBEDDED_HINTS]]></hint>
         <hint><![CDATA[OPTIMIZER_FEATURES_ENABLE('11.2.0.3')]]></hint>
         <hint><![CDATA[DB_VERSION('11.2.0.3')]]></hint>
         <hint><![CDATA[ALL_ROWS]]></hint>
         <hint><![CDATA[OUTLINE_LEAF(@"SEL$1")]]></hint>
         <hint><![CDATA[INDEX(@"SEL$1" "D"@"SEL$1" ("DEPT"."DEPTNO"))]]></hint>
         <hint><![CDATA[INDEX_RS_ASC(@"SEL$1" "E"@"SEL$1" ("EMP"."EMPNO"))]]></hint>
         <hint><![CDATA[LEADING(@"SEL$1" "D"@"SEL$1" "E"@"SEL$1")]]></hint>
         <hint><![CDATA[USE_MERGE(@"SEL$1" "E"@"SEL$1")]]></hint>
        </outline_data>
       </other_xml>
      </operation>
      <operation name="TABLE ACCESS" options="BY INDEX ROWID" id="2" depth="2" pos="1">
       <object>DEPT</object>
       <card>4</card>
       <bytes>80</bytes>
       <cost>2</cost>
       <io_cost>2</io_cost>
       <cpu_cost>15803</cpu_cost>
       <time>00:00:01 </time>
       <project>&quot;D&quot;.&quot;DEPTNO&quot;[NUMBER,22], &quot;D&quot;.&quot;DNAME&quot;[VARCHAR2,14], &quot;D&quot;.&quot;LOC&quot;[VARCHAR2,13]</project>
       <qblock>SEL$1</qblock>
       <object_alias>D@SEL$1</object_alias>
      </operation>
      <operation name="INDEX" options="FULL SCAN" id="3" depth="3" pos="1">
       <object>PK_DEPT</object>
       <card>4</card>
       <cost>1</cost>
       <io_cost>1</io_cost>
       <cpu_cost>7921</cpu_cost>
       <time>00:00:01 </time>
       <project>&quot;D&quot;.ROWID[ROWID,10], &quot;D&quot;.&quot;DEPTNO&quot;[NUMBER,22]</project>
       <qblock>SEL$1</qblock>
       <object_alias>D@SEL$1</object_alias>
      </operation>
      <operation name="SORT" options="JOIN" id="4" depth="2" pos="2">
       <card>3</card>
       <bytes>114</bytes>
       <cost>3</cost>
       <io_cost>2</io_cost>
       <cpu_cost>21037523</cpu_cost>
       <time>00:00:01 </time>
       <project>(#keys=1) &quot;E&quot;.&quot;DEPTNO&quot;[NUMBER,22], &quot;EMPNO&quot;[NUMBER,22], &quot;E&quot;.&quot;ENAME&quot;[VARCHAR2,10], &quot;E&quot;.&quot;JOB&quot;[VARCHAR2,9], &quot;E&quot;.&quot;MGR&quot;[NUMBER,22], &quot;E&quot;.&quot;HIREDATE&quot;[DATE,7], &quot;E&quot;.&quot;SAL&quot;[NUMBER,22], &quot;E&quot;.&quot;COMM&quot;[NUMBER,22]</project>
       <predicates type="access">&quot;E&quot;.&quot;DEPTNO&quot;=&quot;D&quot;.&quot;DEPTNO&quot;</predicates>
       <predicates type="filter">&quot;E&quot;.&quot;DEPTNO&quot;=&quot;D&quot;.&quot;DEPTNO&quot;</predicates>
      </operation>
      <operation name="TABLE ACCESS" options="BY INDEX ROWID" id="5" depth="3" pos="1">
       <object>EMP</object>
       <card>3</card>
       <bytes>114</bytes>
       <cost>2</cost>
       <io_cost>2</io_cost>
       <cpu_cost>16203</cpu_cost>
       <time>00:00:01 </time>
       <project>&quot;EMPNO&quot;[NUMBER,22], &quot;E&quot;.&quot;ENAME&quot;[VARCHAR2,10], &quot;E&quot;.&quot;JOB&quot;[VARCHAR2,9], &quot;E&quot;.&quot;MGR&quot;[NUMBER,22], &quot;E&quot;.&quot;HIREDATE&quot;[DATE,7], &quot;E&quot;.&quot;SAL&quot;[NUMBER,22], &quot;E&quot;.&quot;COMM&quot;[NUMBER,22], &quot;E&quot;.&quot;DEPTNO&quot;[NUMBER,22]</project>
       <qblock>SEL$1</qblock>
       <object_alias>E@SEL$1</object_alias>
      </operation>
      <operation name="INDEX" options="RANGE SCAN" id="6" depth="4" pos="1">
       <object>PK_EMP</object>
       <card>3</card>
       <cost>1</cost>
       <io_cost>1</io_cost>
       <cpu_cost>7921</cpu_cost>
       <time>00:00:01 </time>
       <project>&quot;E&quot;.ROWID[ROWID,10], &quot;EMPNO&quot;[NUMBER,22]</project>
       <predicates type="access">&quot;EMPNO&quot;&gt;7800</predicates>
       <qblock>SEL$1</qblock>
       <object_alias>E@SEL$1</object_alias>
      </operation>
     </plan>
     <plan_monitor max_io_reqs="0" max_io_bytes="0">
      <operation id="0" name="SELECT STATEMENT" depth="0" position="0">
       <stats type="plan_monitor">
        <stat name="first_active">05/20/2020 12:50:46</stat>
        <stat name="last_active">05/20/2020 12:50:46</stat>
        <stat name="duration">1</stat>
        <stat name="from_most_recent">0</stat>
        <stat name="from_sql_exec_start">0</stat>
        <stat name="starts">1</stat>
        <stat name="cardinality">6</stat>
       </stats>
      </operation>
      <operation id="1" parent_id="0" name="MERGE JOIN" depth="1" position="1">
       <optimizer>
        <cardinality>3</cardinality>
        <bytes>174</bytes>
        <cost>5</cost>
        <cpu_cost>21053326</cpu_cost>
        <io_cost>4</io_cost>
        <time>1</time>
       </optimizer>
       <stats type="plan_monitor">
        <stat name="first_active">05/20/2020 12:50:46</stat>
        <stat name="last_active">05/20/2020 12:50:46</stat>
        <stat name="duration">1</stat>
        <stat name="from_most_recent">0</stat>
        <stat name="from_sql_exec_start">0</stat>
        <stat name="starts">1</stat>
        <stat name="cardinality">6</stat>
       </stats>
      </operation>
      <operation id="2" parent_id="1" name="TABLE ACCESS" options="BY INDEX ROWID" depth="2" position="1">
       <object type="TABLE">
        <owner>SCOTT</owner>
        <name>DEPT</name>
       </object>
       <optimizer>
        <cardinality>4</cardinality>
        <bytes>80</bytes>
        <cost>2</cost>
        <cpu_cost>15803</cpu_cost>
        <io_cost>2</io_cost>
        <time>1</time>
       </optimizer>
       <stats type="plan_monitor">
        <stat name="first_active">05/20/2020 12:50:46</stat>
        <stat name="last_active">05/20/2020 12:50:46</stat>
        <stat name="duration">1</stat>
        <stat name="from_most_recent">0</stat>
        <stat name="from_sql_exec_start">0</stat>
        <stat name="starts">1</stat>
        <stat name="cardinality">4</stat>
       </stats>
      </operation>
      <operation id="3" parent_id="2" name="INDEX" options="FULL SCAN" depth="3" position="1">
       <object type="INDEX (UNIQUE)">
        <owner>SCOTT</owner>
        <name>PK_DEPT</name>
       </object>
       <optimizer>
        <cardinality>4</cardinality>
        <cost>1</cost>
        <cpu_cost>7921</cpu_cost>
        <io_cost>1</io_cost>
        <time>1</time>
       </optimizer>
       <stats type="plan_monitor">
        <stat name="first_active">05/20/2020 12:50:46</stat>
        <stat name="last_active">05/20/2020 12:50:46</stat>
        <stat name="duration">1</stat>
        <stat name="from_most_recent">0</stat>
        <stat name="from_sql_exec_start">0</stat>
        <stat name="starts">1</stat>
        <stat name="cardinality">4</stat>
       </stats>
      </operation>
      <operation id="4" parent_id="1" name="SORT" options="JOIN" depth="2" position="2">
       <optimizer>
        <cardinality>3</cardinality>
        <bytes>114</bytes>
        <cost>3</cost>
        <cpu_cost>21037523</cpu_cost>
        <io_cost>2</io_cost>
        <time>1</time>
       </optimizer>
       <stats type="plan_monitor">
        <stat name="first_active">05/20/2020 12:50:46</stat>
        <stat name="last_active">05/20/2020 12:50:46</stat>
        <stat name="duration">1</stat>
        <stat name="from_most_recent">0</stat>
        <stat name="from_sql_exec_start">0</stat>
        <stat name="starts">4</stat>
        <stat name="cardinality">6</stat>
        <stat name="max_memory">2048</stat>
       </stats>
      </operation>
      <operation id="5" parent_id="4" name="TABLE ACCESS" options="BY INDEX ROWID" depth="3" position="1">
       <object type="TABLE">
        <owner>SCOTT</owner>
        <name>EMP</name>
       </object>
       <optimizer>
        <cardinality>3</cardinality>
        <bytes>114</bytes>
        <cost>2</cost>
        <cpu_cost>16203</cpu_cost>
        <io_cost>2</io_cost>
        <time>1</time>
       </optimizer>
       <stats type="plan_monitor">
        <stat name="first_active">05/20/2020 12:50:46</stat>
        <stat name="last_active">05/20/2020 12:50:46</stat>
        <stat name="duration">1</stat>
        <stat name="from_most_recent">0</stat>
        <stat name="from_sql_exec_start">0</stat>
        <stat name="starts">1</stat>
        <stat name="cardinality">6</stat>
       </stats>
      </operation>
      <operation id="6" parent_id="5" name="INDEX" options="RANGE SCAN" depth="4" position="1">
       <object type="INDEX (UNIQUE)">
        <owner>SCOTT</owner>
        <name>PK_EMP</name>
       </object>
       <optimizer>
        <cardinality>3</cardinality>
        <cost>1</cost>
        <cpu_cost>7921</cpu_cost>
        <io_cost>1</io_cost>
        <time>1</time>
       </optimizer>
       <stats type="plan_monitor">
        <stat name="first_active">05/20/2020 12:50:46</stat>
        <stat name="last_active">05/20/2020 12:50:46</stat>
        <stat name="duration">1</stat>
        <stat name="from_most_recent">0</stat>
        <stat name="from_sql_exec_start">0</stat>
        <stat name="starts">1</stat>
        <stat name="cardinality">6</stat>
       </stats>
      </operation>
     </plan_monitor>
    </sql_monitor_report>
   </report>
   <!--FXTMODEL-->
  </script>
  <script language="JavaScript" type="text/javascript" src="emviewers/scripts/loadswf.js">
   <!--Load report viewer-->
  </script>
  <iframe name="_history" frameborder="0" scrolling="no" width="22" height="0">
   <html>
    <head>
     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
     <script type="text/javascript" language="JavaScript1.2" charset="utf-8">
                var v = new top.Vars(top.getSearch(window));
                var fv = v.toString(&apos;$_&apos;);
              </script>
    </head>
    <body>
     <script type="text/javascript" language="JavaScript1.2" charset="utf-8" src="emviewers/scripts/document.js">
      <!--Run document script-->
     </script>
    </body>
   </html>

※ 如果您觉得文章写的还不错, 别忘了在文末给作者点个赞哦 ~

over

猜你喜欢

转载自blog.csdn.net/zzt_2009/article/details/106234161
今日推荐