ITPub博客

首页 > 数据库 > Oracle > 基于DB time的调优分析

基于DB time的调优分析

Oracle 作者:hunanliuweidong 时间:2016-05-02 11:35:50 0 删除 编辑
继昨天使用DB time能够快速灵活的定位sql语句之后,发现分析问题更快捷,高效了。今天就牛刀小试,把一个数据库从500%的负载调到不到100%的负载。前提是确实有可提升可改进的空间。<br /> 首先查看了在快照57611的时间段里DB time很快,也收到了zabbix的邮件通知,<br /> <span style="line-height:1.5;" lang="EN-US"> <p class="MsoNormal"> <span lang="EN-US">ZABBIX-</span>监控系统<span lang="EN-US">: <br /> ------------------------------------<br /> </span>报警内容<span lang="EN-US">: DB time is too high<br /> ------------------------------------<br /> </span>报警级别<span lang="EN-US">: PROBLEM<br /> ------------------------------------<br /> </span>监控项目<span lang="EN-US">: DBtime</span>:<span lang="EN-US">530 % <br /> ------------------------------------ <br /> </span>报警时间<span lang="EN-US">:2015.10.04-07:58:55</span> </p> </span> <p class="MsoNormal"> <span lang="EN-US"><br /> </span><span lang="EN-US"><!--?xml:namespace prefix = o /--><?xml:namespace prefix = o /><o:p></o:p></span> </p> 对于OLAP目前设定的阀值是500%,相对来说是一个较高的阀值了。来分析一下看看是否由于sql的影响较大。<br /> $ ksh showsnapsql.sh 57611<br /> &nbsp; &nbsp;SNAP_ID SQL_ID &nbsp; &nbsp; &nbsp; &nbsp;EXECUTIONS_DELTA ELAPSED_TI PER_TOTAL<br /> ---------- ------------- ---------------- ---------- ----------<br /> &nbsp; &nbsp; &nbsp;57611 8tmf11fvxy09j &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 30 2451s &nbsp; &nbsp; &nbsp;32%<br /> &nbsp; &nbsp; &nbsp;57611 cy55p6nrd31db &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 30 2346s &nbsp; &nbsp; &nbsp;31%<br /> &nbsp; &nbsp; &nbsp;57611 4rhpc838qfsmy &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;143 655s &nbsp; &nbsp; &nbsp; 8%<br /> &nbsp; &nbsp; &nbsp;57611 c7k4g2urpu1sc &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 426s &nbsp; &nbsp; &nbsp; 5%<br /> &nbsp; &nbsp; &nbsp;57611 29tdwfv5d9s4f &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 30 441s &nbsp; &nbsp; &nbsp; 5%<br /> 可以看到在快照57611的时间段内,有两个sql是尤其需要关注的,占用了60%多的DB time,也就意味着如果这两个语句能够改进,对于DB time的提升就很高了,同理,对于系统的负载也会减轻很多。<br /> 我们来看看sql_id&nbsp;<span style="line-height:16px;white-space:normal;">8tmf11fvxy09j这个语句。这个语句一个快照时间范围内执行了近30次,每次执行大概是80秒左右。</span>发现这个语句是一个看似简单的查询。<br /> $ ksh showsql.sh 8tmf11fvxy09j<br /> SQL_FULLTEXT<br /> ----------------------------------------------------------------------------------------------------<br /> &nbsp; &nbsp; &nbsp; &nbsp; SELECT ROUND(AVG(SUM(END_TIME-START_TIME)*1440)) AVG FROM TES_BILLDETAIL &nbsp; &nbsp; &nbsp; WHERE END_TIME &gt;<br /> &nbsp;TRUNC(SYSDATE - 30) AND CN = :1 GROUP BY TRUNC(END_TIME)&nbsp;<br /> 而查看执行计划发现,指标还是正常的,可以看到这个表TES_BILLDETAIL是一个分区表,分区有900多个。<br /> Plan hash value: 2129377450<br /> --------------------------------------------------------------------------------------------------------<br /> | Id &nbsp;| Operation &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| Name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| Rows &nbsp;| Bytes | Cost (%CPU)<br /> --------------------------------------------------------------------------------------------------------<br /> | &nbsp; 0 | SELECT STATEMENT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | &nbsp; &nbsp; &nbsp; | &nbsp; &nbsp; &nbsp; | &nbsp; &nbsp;13 (100)<br /> | &nbsp; 1 | &nbsp;SORT AGGREGATE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | &nbsp; &nbsp; 1 | &nbsp; &nbsp;32 | &nbsp; &nbsp;13 &nbsp; (8)<br /> | &nbsp; 2 | &nbsp; HASH GROUP BY &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | &nbsp; &nbsp; 1 | &nbsp; &nbsp;32 | &nbsp; &nbsp;13 &nbsp; (8)<br /> | &nbsp; 3 | &nbsp; &nbsp;PARTITION RANGE ITERATOR &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | &nbsp; &nbsp; 1 | &nbsp; &nbsp;32 | &nbsp; &nbsp;12 &nbsp; (0)<br /> |* &nbsp;4 | &nbsp; &nbsp; TABLE ACCESS BY LOCAL INDEX ROWID| TES_BILLDETAIL &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp; 1 | &nbsp; &nbsp;32 | &nbsp; &nbsp;12 &nbsp; (0)<br /> |* &nbsp;5 | &nbsp; &nbsp; &nbsp;INDEX RANGE SCAN &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| IND_TES_BILLDETAIL_END_TIME | &nbsp; &nbsp;49 | &nbsp; &nbsp; &nbsp; | &nbsp; &nbsp; 1 &nbsp; (0)<br /> --------------------------------------------------------------------------------------------------------<br /> Query Block Name / Object Alias (identified by operation id):<br /> -------------------------------------------------------------<br /> &nbsp; &nbsp;1 - SEL$1<br /> &nbsp; &nbsp;4 - SEL$1 / TES_BILLDETAIL@SEL$1<br /> &nbsp; &nbsp;5 - SEL$1 / TES_BILLDETAIL@SEL$1<br /> Predicate Information (identified by operation id):<br /> ---------------------------------------------------<br /> &nbsp; &nbsp;4 - filter("CN"=:1)<br /> &nbsp; &nbsp;5 - access("END_TIME"&gt;TRUNC(SYSDATE@!-30))<br /> 通过谓词信息可以看到走了end_date相关的索引。<br /> <br /> 执行计划看起来还是不错的,但是执行的情况确实还是不够理想,那么我们怎么查看执行的细节呢,如果是11g的库,sqlmonitor就是一个利器,使用下面的语句抓取了正在执行的sql情况。<br /> set pages 0<br /> set long 99999999<br /> set linesize 150<br /> col comm format a200<br /> set long 99999<br /> SELECT dbms_sqltune.report_sql_monitor(<br /> sql_id =&gt; '8tmf11fvxy09j',<br /> report_level =&gt; 'ALL',<br /> type=&gt;'HTML'<br /> ) comm&nbsp;<br /> FROM dual;<br /> 可以看到<br /> <span class="s9"><b>SQL Plan Monitoring Details (Plan Hash Value=2129377450)</b></span><br /> <table style="width:491pt;" class="ke-zeroborder" border="2" cellspacing="1" cellpadding="1" width="652"> <tbody> <tr> <td height="57" rowspan="2" width="77" colspan="2"> Id </td> <td rowspan="2" width="214"> Operation </td> <td rowspan="2" width="125"> Name </td> <td width="72"> Estimated </td> <td rowspan="2" width="53"> Cost </td> <td rowspan="2" width="53"> Execs </td> <td rowspan="2" width="58"> Rows </td> </tr> <tr> <td height="19" width="72"> Rows </td> </tr> <tr> <td height="45" width="49"> . </td> <td width="28"> 0 </td> <td width="214"> SELECT STATEMENT </td> <td width="125"> . </td> <td width="72"> . </td> <td width="53"> . </td> <td width="53"> 1 </td> <td width="58"> . </td> </tr> <tr> <td height="45" width="49"> . </td> <td width="28"> 1 </td> <td width="214"> .<span>&nbsp;SORT AGGREGATE</span> </td> <td width="125"> . </td> <td width="72"> 1 </td> <td width="53"> 13 </td> <td width="53"> 1 </td> <td width="58"> . </td> </tr> <tr> <td height="36" rowspan="2" width="49"> -&gt; </td> <td rowspan="2" width="28"> 2 </td> <td rowspan="2" width="214"> ..<span>&nbsp;HASH GROUP BY</span> </td> <td rowspan="2" width="125"> . </td> <td rowspan="2" width="72"> 1 </td> <td rowspan="2" width="53"> 13 </td> <td rowspan="2" width="53"> 1 </td> <td rowspan="2" width="58"> 0 </td> </tr> <tr> </tr> <tr> <td height="60" rowspan="2" width="49"> -&gt; </td> <td rowspan="2" width="28"> 3 </td> <td rowspan="2" width="214"> ...<span>&nbsp;PARTITION RANGE ITERATOR</span> </td> <td rowspan="2" width="125"> . </td> <td rowspan="2" width="72"> 1 </td> <td rowspan="2" width="53"> 12 </td> <td rowspan="2" width="53"> 1 </td> <td rowspan="2" width="58"> 3 </td> </tr> <tr> </tr> <tr> <td height="75" rowspan="2" width="49"> -&gt; </td> <td rowspan="2" width="28"> 4 </td> <td rowspan="2" width="214"> ....<span>&nbsp;TABLE ACCESS BY LOCAL INDEX ROWID</span> </td> <td rowspan="2" width="125"> TES_BILLDETAIL </td> <td rowspan="2" width="72"> 1 </td> <td rowspan="2" width="53"> 12 </td> <td rowspan="2" width="53"> 5 </td> <td rowspan="2" width="58"> 3 </td> </tr> <tr> </tr> <tr> <td height="45" rowspan="2" width="49"> -&gt; </td> <td rowspan="2" width="28"> 5 </td> <td rowspan="2" width="214"> .....<span>&nbsp;INDEX RANGE SCAN</span> </td> <td rowspan="2" width="125"> IND_TES_BILLDETAIL<br /> _END_TIME </td> <td rowspan="2" width="72"> 49 </td> <td rowspan="2" width="53"> 1 </td> <td rowspan="2" width="53"> 5 </td> <td rowspan="2" width="58"> 9M </td> </tr> <tr> </tr> </tbody> </table> <br /> 绑定变量值也能抓取到。<span class="s9"><br /> <hr align="left" size="1" /> </span> <table border="1" cellspacing="0" cellpadding="0"> <tbody> <tr class="s16"> <th align="middle"> Name </th> <th align="middle"> Position </th> <th align="middle"> Type </th> <th align="middle"> Value </th> </tr> <tr class="s17a"> <td align="left"> :1 </td> <td align="middle"> 1 </td> <td align="left"> VARCHAR2(128) </td> <td align="left"> 582891946 </td> </tr> </tbody> </table> 带着疑问查看了下数据的情况,发现使用字段CN来过滤,只有1000多条记录,相比通过时间来过滤的900多万记录来说差别实在是太大了。<br /> SQL&gt; select count(*)from TES_BILLDETAIL where cn='582891946';<br /> &nbsp; &nbsp; &nbsp; 1052<br /> Elapsed: 00:00:00.01<br /> <br /> 关于这个表的索引情况如下:<br /> INDEX_NAME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TABLESPACE INDEX_TYPE UNIQUENES PAR COLUMN_LIST &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TABLE_TYPE STATUS &nbsp; NUM_ROWS LAST_ANALYZED &nbsp; &nbsp; &nbsp; G<br /> ------------------------------ ---------- ---------- --------- --- ------------------------------ ---------- ------ ---------- ------------------- -<br /> IND_TES_BILLDETAIL_CN &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NORMAL &nbsp; &nbsp; NONUNIQUE YES CN &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TABLE &nbsp; &nbsp; &nbsp;N/A &nbsp; &nbsp;3145619347 2015-06-26 04:52:11 N<br /> IND_TES_BILLDETAIL_END_TIME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NORMAL &nbsp; &nbsp; NONUNIQUE YES END_TIME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TABLE &nbsp; &nbsp; &nbsp;N/A &nbsp; &nbsp;2926456256 2015-06-26 04:52:23 N<br /> <br /> 在字段CN上也是有索引的,同时还有一个索引是end_time上,但是通过end_time过滤的数据实在是有点多,尽管相比整张表几十亿的记录来说确实是很大的改进,但是相比于字段CN过滤的数据结果来说,差别更加的悬殊。<br /> 所以采用CN来过滤着实是一个不错的选择。可以简单打一个比方,比如我去网上购物,购物的所以记录都存放在一张表里,如果需要把我最近这些天的消费记录抓取出来,肯定是通过我的id号直接关联,然后再过滤时间要好一些,如果直接来过滤时间,先把全国人民购物的这些天的数据都抓出来再过滤,这样的差别着实很大,也不高效。<br /> 为什么走了end_time的索引而不是CN字段的索引,一个原因就是分区字段是根据end_time来做的,刚好有prefix的local index,加上直方图的信息,所以优化器做评判的时候根据这些信息分析觉得还是end_time的索引要好一些,而CN字段的信息是non-prefix的,CN的信息在各个分区中就很难评估,这个时候优化器评判的基准也有一定的限制了。<br /> <br /> 当然,查看这个索引的并行度的时候发现竟然是12,可见之前也有同学尝试改进过这个问题,似乎从并行上下工夫了。<br /> SQL&gt; SELECT DEGREE FROM DBA_INDEXES WHERE INDEX_NAME='IND_TES_BILLDETAIL_CN';<br /> 12<br /> 当然这个的效果也有,但是解决的力度还是不大。<br /> 我们加入hint先来看看效果。<br /> <span style="line-height:16px;white-space:normal;">EXPLAIN PLAN FOR SELECT /*+INDEX(TES_BILLDETAIL IND_TES_BILLDETAIL_CN)*/ROUND(AVG(SUM(END_TIME-START_TIME)*1440)) AVG FROM TES_BILLDETAIL WHERE END_TIME &gt; TRUNC(SYSDATE - 30) AND CN = :1 GROUP BY TRUNC(END_TIME)&nbsp;</span><br style="line-height:16px;white-space:normal;" /> <span style="line-height:16px;">发现索引能够正常启用,而且cost也不高,就按照这个方子来试试。</span><br /> 如果想有一种更好更全面的改进,就是添加复合索引,但是有一些隐患,一来这个表实在太大了,加索引时间,资源都是很大的消耗,二来更重要的是不确定添加索引之后会对其它的语句有很大的影响,所以为了保险起见,我先修改这两个相关的sql,修改它的执行计划,在线就可以做,如果不合适,撤销就可以。<br /> 修改执行计划可以使用sqlt来做,coe提供的这套工具还是很不错的。<br /> 因为里面有绑定变量,所以尝试用下面的方式来做。<br /> declare<br /> cursor temp_cur is select '582891946' id from dual;<br /> begin<br /> for i in temp_cur loop<br /> execute immediate 'SELECT /*+INDEX(TES_BILLDETAIL IND_TES_BILLDETAIL_CN)*/ROUND(AVG(SUM(END_TIME-START_TIME)*1440)) AVG FROM TES_BILLDETAIL WHERE END_TIME &gt; TRUNC(SYSDATE - 30) AND CN = :1 GROUP BY TRUNC(END_TIME) '<br /> using i.id;<br /> end loop;<br /> end;<br /> /<br /> <br /> SQL&gt; @a.sql<br /> PL/SQL procedure successfully completed.<br /> 然后从v$sql里面去抓取,可以找到修改后的sql的sql_id<br /> <br /> SQL_ID &nbsp; &nbsp; &nbsp; &nbsp;SQL_FULLTEXT<br /> ------------- ----------------------------------------------------------------------------------------------------<br /> 9h0kr4s3365mt SELECT /*+INDEX(TES_BILLDETAIL IND_TES_BILLDETAIL_CN)*/ROUND(AVG(SUM(END_TIME-START_TIME)*1440)) AVG<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FROM TES_BILLDETAIL WHERE END_TIME &gt; TRUNC(SYSDATE - 30) AND CN = :1 GROUP BY TRUNC(END_TIME)<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br /> 所以sql_id&nbsp;<span style="line-height:16px;white-space:normal;">9h0kr4s3365mt就是我们努力的目标,使用提供的脚本来得到执行计划的profile信息。输入sql_id和hash_value即可。</span><br /> SQL&gt;@coe_xfr_sql_profile.sql<br /> Parameter 1:<br /> SQL_ID (required)<br /> Enter value for 1: 8tmf11fvxy09j<br /> PLAN_HASH_VALUE AVG_ET_SECS<br /> --------------- -----------<br /> &nbsp; &nbsp; &nbsp;2129377450 &nbsp; &nbsp; &nbsp;70.743<br /> Parameter 2:<br /> PLAN_HASH_VALUE (required)<br /> Enter value for 2: 2129377450<br /> Values passed to coe_xfr_sql_profile:<br /> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br /> SQL_ID &nbsp; &nbsp; &nbsp; &nbsp; : "8tmf11fvxy09j"<br /> PLAN_HASH_VALUE: "2129377450"<br /> 然后输入修改后的sql_id,也得到一个执行计划的proifle信息,我们需要做一个置换。<br /> SQL&gt; @coe_xfr_sql_profile.sql<br /> Parameter 1:<br /> SQL_ID (required)<br /> Enter value for 1: 9h0kr4s3365mt<br /> PLAN_HASH_VALUE AVG_ET_SECS<br /> --------------- -----------<br /> &nbsp; &nbsp; &nbsp;3361841277 &nbsp; &nbsp; &nbsp; &nbsp;.003<br /> Parameter 2:<br /> PLAN_HASH_VALUE (required)<br /> Enter value for 2: 3361841277<br /> Values passed to coe_xfr_sql_profile:<br /> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br /> SQL_ID &nbsp; &nbsp; &nbsp; &nbsp; : "9h0kr4s3365mt"<br /> PLAN_HASH_VALUE: "3361841277"<br /> 得到的关于hint部分的信息如下,原来的sql中的信息为:<br /> h := SYS.SQLPROF_ATTR(<br /> q'[BEGIN_OUTLINE_DATA]',<br /> q'[IGNORE_OPTIM_EMBEDDED_HINTS]',<br /> q'[OPTIMIZER_FEATURES_ENABLE('11.2.0.3')]',<br /> q'[DB_VERSION('11.2.0.3')]',<br /> q'[OPT_PARAM('optimizer_index_cost_adj' 30)]',<br /> q'[OPT_PARAM('optimizer_index_caching' 90)]',<br /> q'[ALL_ROWS]',<br /> q'[OUTLINE_LEAF(@"SEL$1")]',<br /> q'[INDEX_RS_ASC(@"SEL$1" "TES_BILLDETAIL"@"SEL$1" ("TES_BILLDETAIL"."CN"))]',<br /> q'[USE_HASH_AGGREGATION(@"SEL$1")]',<br /> q'[END_OUTLINE_DATA]');<br /> <br /> 添加了hint之后启用了CN相关的索引的hint信息如下:<br /> h := SYS.SQLPROF_ATTR(<br /> q'[BEGIN_OUTLINE_DATA]',<br /> q'[IGNORE_OPTIM_EMBEDDED_HINTS]',<br /> q'[OPTIMIZER_FEATURES_ENABLE('11.2.0.3')]',<br /> q'[DB_VERSION('11.2.0.3')]',<br /> q'[OPT_PARAM('optimizer_index_cost_adj' 30)]',<br /> q'[OPT_PARAM('optimizer_index_caching' 90)]',<br /> q'[ALL_ROWS]',<br /> q'[OUTLINE_LEAF(@"SEL$1")]',<br /> q'[INDEX_RS_ASC(@"SEL$1" "TES_BILLDETAIL"@"SEL$1" ("TES_BILLDETAIL"."CN"))]',<br /> q'[USE_HASH_AGGREGATION(@"SEL$1")]',<br /> q'[END_OUTLINE_DATA]');<br /> 需要把这部分内容替换原来sqlprofile中的hint部分。<br /> 拷贝替换之后,就可以生成sqlprofile了,运行脚本的结果如下:<br /> SQL&gt; DECLARE<br /> &nbsp; 2 &nbsp;sql_txt CLOB;<br /> &nbsp; 3 &nbsp;h &nbsp; &nbsp; &nbsp; SYS.SQLPROF_ATTR;<br /> &nbsp; 4 &nbsp;PROCEDURE wa (p_line IN VARCHAR2) IS<br /> &nbsp; 5 &nbsp;BEGIN<br /> &nbsp; 6 &nbsp;DBMS_LOB.WRITEAPPEND(sql_txt, LENGTH(p_line), p_line);<br /> &nbsp; 7 &nbsp;END wa;<br /> &nbsp; 8 &nbsp;BEGIN<br /> &nbsp; 9 &nbsp;DBMS_LOB.CREATETEMPORARY(sql_txt, TRUE);<br /> &nbsp;10 &nbsp;DBMS_LOB.OPEN(sql_txt, DBMS_LOB.LOB_READWRITE);<br /> &nbsp;11 &nbsp;-- SQL Text pieces below do not have to be of same length.<br /> &nbsp;12 &nbsp;-- So if you edit SQL Text (i.e. removing temporary Hints),<br /> &nbsp;13 &nbsp;-- there is no need to edit or re-align unmodified pieces.<br /> &nbsp;14 &nbsp;wa(q'[ &nbsp; &nbsp; SELECT ROUND(AVG(SUM(END_TIME-START_TIME)*1440)) AVG FROM TES_B]');<br /> &nbsp;15 &nbsp;wa(q'[ILLDETAIL &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHERE END_TIME &gt; TRUNC(SYSDATE - 30) AND CN ]');<br /> &nbsp;16 &nbsp;wa(q'[= :1 GROUP BY TRUNC(END_TIME) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;]');<br /> &nbsp;17 &nbsp;DBMS_LOB.CLOSE(sql_txt);<br /> &nbsp;18 &nbsp;h := SYS.SQLPROF_ATTR(<br /> &nbsp;19 &nbsp;q'[BEGIN_OUTLINE_DATA]',<br /> &nbsp;20 &nbsp;q'[IGNORE_OPTIM_EMBEDDED_HINTS]',<br /> &nbsp;21 &nbsp;q'[OPTIMIZER_FEATURES_ENABLE('11.2.0.3')]',<br /> &nbsp;22 &nbsp;q'[DB_VERSION('11.2.0.3')]',<br /> &nbsp;23 &nbsp;q'[OPT_PARAM('optimizer_index_cost_adj' 30)]',<br /> &nbsp;24 &nbsp;q'[OPT_PARAM('optimizer_index_caching' 90)]',<br /> &nbsp;25 &nbsp;q'[ALL_ROWS]',<br /> &nbsp;26 &nbsp;q'[OUTLINE_LEAF(@"SEL$1")]',<br /> &nbsp;27 &nbsp;q'[INDEX_RS_ASC(@"SEL$1" "TES_BILLDETAIL"@"SEL$1" ("TES_BILLDETAIL"."CN"))]',<br /> &nbsp;28 &nbsp;q'[USE_HASH_AGGREGATION(@"SEL$1")]',<br /> &nbsp;29 &nbsp;q'[END_OUTLINE_DATA]');<br /> &nbsp;30 &nbsp;:signature := DBMS_SQLTUNE.SQLTEXT_TO_SIGNATURE(sql_txt);<br /> &nbsp;31 &nbsp;:signaturef := DBMS_SQLTUNE.SQLTEXT_TO_SIGNATURE(sql_txt, TRUE);<br /> &nbsp;32 &nbsp;DBMS_SQLTUNE.IMPORT_SQL_PROFILE (<br /> &nbsp;33 &nbsp;sql_text &nbsp; &nbsp;=&gt; sql_txt,<br /> &nbsp;34 &nbsp;profile &nbsp; &nbsp; =&gt; h,<br /> &nbsp;35 &nbsp;name &nbsp; &nbsp; &nbsp; &nbsp;=&gt; 'coe_8tmf11fvxy09j_2129377450',<br /> &nbsp;36 &nbsp;description =&gt; 'coe 8tmf11fvxy09j 2129377450 '||:signature||' '||:signaturef||'',<br /> &nbsp;37 &nbsp;category &nbsp; &nbsp;=&gt; 'DEFAULT',<br /> &nbsp;38 &nbsp;validate &nbsp; &nbsp;=&gt; TRUE,<br /> &nbsp;39 &nbsp;replace &nbsp; &nbsp; =&gt; TRUE,<br /> &nbsp;40 &nbsp;force_match =&gt; FALSE /* TRUE:FORCE (match even when different literals in SQL). FALSE:EXACT (similar to CURSOR_SHARING) */ );<br /> &nbsp;41 &nbsp;DBMS_LOB.FREETEMPORARY(sql_txt);<br /> &nbsp;42 &nbsp;END;<br /> &nbsp;43 &nbsp;/<br /> PL/SQL procedure successfully completed.<br /> SQL&gt; WHENEVER SQLERROR CONTINUE<br /> SQL&gt; SET ECHO OFF;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SIGNATURE<br /> ---------------------<br /> &nbsp; 4228059721940760383<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SIGNATUREF<br /> ---------------------<br /> &nbsp; 4228059721940760383<br /> ... manual custom SQL Profile has been created<br /> COE_XFR_SQL_PROFILE_8tmf11fvxy09j_2129377450 completed<br /> sql profile已经启用了,再次查看就可以看到已经生效了。<br /> SQL&gt; SELECT *FROM TABLE(DBMS_XPLAN.DISPLAY);<br /> PLAN_TABLE_OUTPUT<br /> ---------------------------------------------------------------------<br /> Plan hash value: 3361841277<br /> ------------------------------------------------------------------------------------------------------------------------------<br /> | Id &nbsp;| Operation &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| Name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| Rows &nbsp;| Bytes | Cost (%CPU)| Time &nbsp; &nbsp; | Pstart| Pstop |<br /> ------------------------------------------------------------------------------------------------------------------------------<br /> | &nbsp; 0 | SELECT STATEMENT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | &nbsp; &nbsp; 1 | &nbsp; &nbsp;32 | &nbsp; &nbsp;41 &nbsp; (3)| 00:00:01 | &nbsp; &nbsp; &nbsp; | &nbsp; &nbsp; &nbsp; |<br /> | &nbsp; 1 | &nbsp;SORT AGGREGATE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | &nbsp; &nbsp; 1 | &nbsp; &nbsp;32 | &nbsp; &nbsp;41 &nbsp; (3)| 00:00:01 | &nbsp; &nbsp; &nbsp; | &nbsp; &nbsp; &nbsp; |<br /> | &nbsp; 2 | &nbsp; HASH GROUP BY &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | &nbsp; &nbsp; 1 | &nbsp; &nbsp;32 | &nbsp; &nbsp;41 &nbsp; (3)| 00:00:01 | &nbsp; &nbsp; &nbsp; | &nbsp; &nbsp; &nbsp; |<br /> | &nbsp; 3 | &nbsp; &nbsp;PARTITION RANGE ITERATOR &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | &nbsp; &nbsp; 1 | &nbsp; &nbsp;32 | &nbsp; &nbsp;40 &nbsp; (0)| 00:00:01 | &nbsp; KEY | &nbsp; 970 |<br /> |* &nbsp;4 | &nbsp; &nbsp; TABLE ACCESS BY LOCAL INDEX ROWID| TES_BILLDETAIL &nbsp; &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp; 1 | &nbsp; &nbsp;32 | &nbsp; &nbsp;40 &nbsp; (0)| 00:00:01 | &nbsp; KEY | &nbsp; 970 |<br /> |* &nbsp;5 | &nbsp; &nbsp; &nbsp;INDEX RANGE SCAN &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| IND_TES_BILLDETAIL_CN | &nbsp; &nbsp;42 | &nbsp; &nbsp; &nbsp; | &nbsp; &nbsp;35 &nbsp; (0)| 00:00:01 | &nbsp; KEY | &nbsp; 970 |<br /> PLAN_TABLE_OUTPUT<br /> Predicate Information (identified by operation id):<br /> ---------------------------------------------------<br /> &nbsp; &nbsp;4 - filter("END_TIME"&gt;TRUNC(SYSDATE@!-30))<br /> &nbsp; &nbsp;5 - access("CN"=:1)<br /> Note<br /> -----<br /> &nbsp; &nbsp;- SQL profile "coe_8tmf11fvxy09j_2129377450" used for this statement<br /> 按照这个思路,又对另外两个sql进行类类似的分析,结果也是使用Hint来调整,使用sql monitor来查看实时的执行计划情况,使用sqlt来修改执行计划。<br /> 调优之后的效果还是很明显的。DB time大大降低,逻辑读的效果更加明显。<br /> <img alt="" src="/attachment/201510/5/23718752_1444059250krLv.png" width="700" height="237" /><br /> <img alt="" src="/attachment/201510/6/23718752_1444102664kuQK.png" width="700" height="244" /><br />

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/25444422/viewspace-2091985/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2016-04-06

  • 博文量
    98
  • 访问量
    175957