设为首页 收藏本站
查看: 617|回复: 0

[经验分享] Oracle 11g SQL Repair Advisor(原创)

[复制链接]

尚未签到

发表于 2016-7-27 10:28:13 | 显示全部楼层 |阅读模式
  SQL Repair Advisor
  When a SQL statement failure results in a critical error, the new SQL Repair Advisor analyzes the statement and in many cases recommends a patch to fix the failed statement. The fix is usually in the form of a recommendation to apply a SQL patch to bypass the failure, without changing the SQL statement itself. Applying the recommended patch leads the query optimizer to select an alternate execution plan for the problem statement.
  The SQL Repair Advisor tries to recommend a SQL patch when it’s unable to find a fix for the problem SQL statement(s). A SQL patch is very similar to a SQL profile, but it’s used mostly as a workaround to fix a failing SQL statement.
  There are two ways you can invoke the SQL Repair Advisor. The first way is to use the Support Workbench to launch the SQL Repair Advisor. You can also use the new DBMS_SQLDIAG package to invoke the SQL Repair Advisor.
  Using the DBMS_SQLDIAG Package
  Although using the Enterprise Manager is the most straightforward way to invoke the SQL Repair Advisor, you can also use the new DBMS_SQLDIAG package to invoke the advisor. In the following example, I first create a SQL Repair Advisor task and then apply and test the SQL patch offered by it:
  1. Identify the problem SQL statement, as shown in the following example,
  which results in an error:
  SQL> delete from t t1 where t1.a = 'a'
  and rowid <> (select max(rowid)
  from t t2 where t1.a= t2.a and t1.b = t2.b and t1.d=t2.d);
  You can fix the error resulting from executing the SQL statement by using the SQL Repair Advisor.
  2. Create a SQL Repair Advisor diagnostic task by passing the offending SQL query as the value for the sql_text attribute:
  SQL> declare
  2 report_out clob;
  3 task_id varchar2(50);
  4 begin
  5 task_id := dbms_sqldiag.create_diagnosis_task(
  6 sql_text=>' delete from t t1 where t1.a = 'a'
  and rowid <> (select max(rowid) from t t2
  where t1.a= t2.a and t1.b = t2.b
  and t1.d=t2.d)',
  8 task_name =>'test_task1',
  9 problem_type=>dbms_sqldiag.problem_type_compilation
  _error);
  10* end;
  PL/SQL procedure successfully completed.
  The previous code specifies the SQL statement you want the SQL Repair Advisor to analyze. In addition, it specifies the task name and the problem type. I chose problem_type_compilation_error as the value for the problem_type parameter in this example. The other possible value for the problem_type parameter is problem_type_execution_type.
  You are now ready to execute the task, as shown in the next step.
  3. Execute the diagnostic task you created earlier, by passing the task name as a parameter to the EXECUTE_DIAGNOSTIC_TASK procedure:
  SQL> exec dbms_sqldiag.execute_diagnosis_task('test_task1');
  PL/SQL procedure successfully completed.
  The EXECUTE_DIAGNOSTIC_TASK procedure has only a single param-eter, task_name.
  4. Use the REPORT_DIAGNOSTIC_TASK procedure to get an analysis of the diagnostic task you executed:
  SQL> declare rep_out clob;
  2 begin
  3 rep_out := dbms_sqldiag.report_diagnosis_task
  4 ('test_task1',dbms_sqldiag.type_text);
  5 dbms_output.put_line ('Report : ' || rep_out);
  6*end;
  SQL> /
   
  Report : GENERAL INFORMATION
  SECTION
  -------------------------------------------------
  Tuning Task Name : test_task1
  Tuning Task Owner : SYS
  Tuning Task ID : 3219
  Workload Type : Single SQL Statement
  Execution Count : 1
  Current Execution : EXEC_3219
  Execution Type : SQL DIAGNOSIS
  Scope : COMPREHENSIVE
  Time Limit(seconds) : 1800
  Completion Status : COMPLETED
  Started at : 10/20/2007 06:33:42
  Completed at : 10/20/2007 06:36:45
  Schema Name : SYS
  SQL ID : 44wx3x03jx01v
  SQL Text : delete from t t1 where t1.a = 'a'
  and rowid <> (select max(rowid)
  from t t2 where t1.a= t2.a
  and t1.b = t2.b and t1.d=t2.d)
  ...
  PL/SQL procedure successfully completed.
  5. If the SQL Repair Advisor recommends a patch, you can accept the patch by executing the ACCEPT_SQL_PATCH procedure, as shown here:
  SQL> exec dbms_sqldiag.accept_sql_patch (task_name=> 'test_task1',task_owner=> 'SYS');
  You can now execute the problem SQL statement to ensure that the workaround patch did fix the problem. Check the explain plan output for the SQL statement to make sure it shows use of the SQL patch. The DBA_SQL_PATCHES view contains the names of all the patches recommended by the SQL Repair Advisor.
  If you want to drop the SQL patch for any reason, you can do so by using the DROP_SQL_PATCH procedure. You can remove the SQL patch, for example, if you receive an official patch from Oracle to fix the problem. You can also drop the SQL patches when you upgrade your database to the next patch set or Oracle release.
  You can export a SQL patch into another database by using a staging table. Inserting a patch is called packing the staging table and creating patches using the staging table is called unpacking. The following exercise shows how to export a SQL Patch.
  Exporting a SQL Patch to Another Database
1. Create a staging table by executing the CREATE_STGTB_SQLPATCH procedure:
SQL> exec dbms_sqldiag.create_stgtab_sqlpatch (
table_name => 'mystagetab1',
schema_name => 'hr');
In the next step, you’ll use this table to store the SQL patch information.
2. Execute the PACK_STGTAB_SQLPATCH procedure to write SQL patch information to the staged table you created in Step 1.
SQL> exec dbms_sqldiag.pack_stgtab_sqlpatch (
staging_table_name => 'mystagetab1');
This will copy all SQL patches in the DEFAULT category to the staging table mystgtab1. You can now move the staging table to the new database using the Data Pump Export and Import utilities.
3. Use the UNPACK_STGTAB_SQLPATCH procedure to create SQL patches on the new system using the patches in the staging table.
SQL> exec dbms_sqldiag.unpack_stgtab_sqlpatch
(staging_table_name => 'mystgtab1');
By default, Oracle will unpack all patches in the staging table and apply those patches to the target database.
   
  参考至:《McGraw.Hill.OCP.Oracle.Database.11g.New.Features.for.Administrators.Exam.Guide.Apr.2008》
  本文原创,转载请注明出处、作者
  如有错误,欢迎指正
  邮箱:czmcj@163.com

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-250133-1-1.html 上篇帖子: oracle 建表之前先删除语句 下篇帖子: oracle跟踪程序执行的SQL
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表