文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

oracle sql_profile绑定异常处理

2024-04-02 19:55

关注

一 建立测试表
create table a nologging as select * from all_objects;
二 准备工作
找到sql_Id='aq03p7muwgvq5'
select * from V$sql where sql_text like '% from a where object_id=3%';
找到全表的outline:
方法一:dba_hist_sql_plan/v$sql_plan都可以

select extractvalue(value(d), '/hint') as outline_hints
from
xmltable('
declare
  v_hints sys.sqlprof_attr;
  v_sqltext clob;
begin
  select sql_fulltext into v_sqltext from v$sql where sql_id='aq03p7muwgvq5' and rownum<2;
  v_hints:=sys.sqlprof_attr(q'[FULL(@"SEL$1" "A"@"SEL$1")]');
  dbms_sqltune.import_sql_profile(v_sqltext,v_hints,'sql_full',force_match=>true,replace=>true);
end;

建立索引
create index I_ind_object_id_com on a(object_id,object_name) nologging;
查看执行计划,并没有走索引:

 Execution Plan
----------------------------------------------------------
Plan hash value: 2248738933 
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |     1 |    98 |   177   (1)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| A    |     1 |    98 |   177   (1)| 00:00:01 |
-------------------------------------------------------------------------- 
Predicate Information (identified by operation id):
--------------------------------------------------- 
   1 - filter("OBJECT_ID"=3) 
Note
-----
   - SQL profile "sql_full" used for this statement
Statistics
----------------------------------------------------------
          7  recursive calls
          0  db block gets
       1254  consistent gets
       1246  physical reads
          0  redo size
       1606  bytes sent via SQL*Net to client
        519  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed

删除profile

begin
   dbms_sqltune.drop_sql_profile('sql_full');
   end;

再次执行sql,找到走索引的outline

select * from dbms_xplan.display_awr('aq03p7muwgvq5',1,'outline');
  
declare
  v_hints sys.sqlprof_attr;
  v_sqltext clob;
begin
  select sql_fulltext into v_sqltext from v$sql where sql_id='aq03p7muwgvq5' and rownum<2;
  v_hints:=sys.sqlprof_attr(  q'[INDEX_RS_ASC(@"SEL$1" "A"@"SEL$1" ("A"."OBJECT_ID" "A"."OBJECT_NAME"))]');
  dbms_sqltune.import_sql_profile(v_sqltext,v_hints,'sql_comp_ind',force_match=>true,replace=>true);
end;    

建立更优的索引
create index I_ind_object_id on a(object_Id) nologging;
再次查看执行计划,并没有走更优的索引

Execution Plan
----------------------------------------------------------
Plan hash value: 3075844428 
---------------------------------------------------------------------------------------------------
| Id  | Operation                   | Name                | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |                     |     1 |    98 |     3   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| A                   |     1 |    98 |     3   (0)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN          | I_IND_OBJECT_ID_COM |     1 |       |     2   (0)| 00:00:01 |
------------------------------------------------------------------------
 Predicate Information (identified by operation id):
-------------------------------------------------- 
   2 - access("OBJECT_ID"=3)
 Note
-----
   - SQL profile "sql_comp_ind" used for this statement
 Statistics

      7  recursive calls
      0  db block gets
     10  consistent gets
      1  physical reads
      0  redo size
   1609  bytes sent via SQL*Net to client
    519  bytes received via SQL*Net from client
      2  SQL*Net roundtrips to/from client
      0  sorts (memory)
      0  sorts (disk)
      1  rows processed

当然你也可以用SQLT里的coe_xfr_sql_profile.sql或者create_sql_profile.sql生成sql_profile;

这里有一点比较扯的是用完整的outline,写进去不报错,但执行计划不走sql_profile里约定的内容;

declare
  v_hints sys.sqlprof_attr;
  v_sqltext clob;
begin
  select sql_fulltext into v_sqltext from v$sql where sql_id='aq03p7muwgvq5' and rownum<2;
  v_hints:=sys.sqlprof_attr(
q'[BEGIN_OUTLINE_DATA]',
q'[IGNORE_OPTIM_EMBEDDED_HINTS]',
q'[OPTIMIZER_FEATURES_ENABLE('11.2.0.4')]',
q'[DB_VERSION('11.2.0.4')],
q'[ALL_ROWS]',
q'[OUTLINE_LEAF(@"SEL$1")]',
q'[FULL(@"SEL$1" "A"@"SEL$1")]',
q'[END_OUTLINE_DATA]');
  dbms_sqltune.import_sql_profile(v_sqltext,v_hints,'sql_full',force_match=>true,replace=>true);
end;

oracle sql_profile绑定异常处理
做10053:

select value from v$parameter where name='sql_trace';
alter session set sql_trace=true;
alter session set tracefile_identifier='mytrace';

--single instance
select tracefile from v$process where addr in (select paddr from v$session where sid in (select sid from v$Mystat where rownum<2));
--/u01/app/oracle/diag/rdbms/orcl/ORCL/trace/ORCL_ora_22354_mytrace.trc
ALTER SESSION SET EVENTS='10053 trace name context forever, level 1';
select * from a where object_id=3

ALTER SESSION SET EVENTS '10053 trace name context off';
没有看出什么异常,和traceonly看到的一致,做sql_trace跟踪sql_profile写入的基表sys.sqlobj$data:
select to_char(comp_data),signature from sys.sqlobj$data where plan_Id=0
发现:

<outline_data
<hint><![CDATA[BEGIN_OUTLINE_DATA]]></hint>
<hint><![CDATA[IGNORE_OPTIM_EMBEDDED_HINTS]]></hint>
<hint><![CDATA[OPTIMIZER_FEATURES_ENABLE('11.2.0.4')]]></hint>
<hint><![CDATA[DB_VERSION('11.2.0.4')],q'[ALL_ROWS]]></hint>
<hint><![CDATA[OUTLINE_LEAF(@"SEL$1")]]></hint>
<hint><![CDATA[FULL(@"SEL$1" "A"@"SEL$1")]]></hint>
<hint><![CDATA[END_OUTLINE_DATA]]></hint>
</outline_data>

这一段里,<hint><![CDATA[DB_VERSION('11.2.0.4')],q'[ALL_ROWS]]></hint>有点异常,删除重新执行:

declare
  v_hints sys.sqlprof_attr;
  v_sqltext clob;
begin
  select sql_fulltext into v_sqltext from v$sql where sql_id='aq03p7muwgvq5' and rownum<2;
  v_hints:=sys.sqlprof_attr(
q'[BEGIN_OUTLINE_DATA]',
q'[IGNORE_OPTIM_EMBEDDED_HINTS]',
q'[OPTIMIZER_FEATURES_ENABLE('11.2.0.4')]',
q'[ALL_ROWS]',
q'[OUTLINE_LEAF(@"SEL$1")]',
q'[FULL(@"SEL$1" "A"@"SEL$1")]',
q'[END_OUTLINE_DATA]');
  dbms_sqltune.import_sql_profile(v_sqltext,v_hints,'sql_full',force_match=>true,replace=>true);
end;

正常:

Execution Plan
----------------------------------------------------------
Plan hash value: 2248738933 
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |     1 |    98 |   177   (1)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| A    |     1 |    98 |   177   (1)| 00:00:01 |
--------------------------------------------------------------------------

Predicate Information (identified by operation id):
--------------------------------------------------- 
   1 - filter("OBJECT_ID"=3) 
Note
-----
   - SQL profile "sql_full" used for this statement 
Statistics
----------------------------------------------------------
          1  recursive calls
          0  db block gets
       1248  consistent gets
       1245  physical reads
          0  redo size
       1606  bytes sent via SQL*Net to client
        519  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed
阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-数据库
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯