当前位置: 首页 > article >正文

Oracle 使用 sql profile 固定执行计划

测试使用 sql profile 固定执行计划:

Oracle 10g之前有outlines,10g之后 sql profile 。如果针对非绑定变量的sql,outlines则效果不佳,不建议使用 。

1、准备测试用表
SQL> create table zzh_ob as select * from dba_objects;
SQL> create index ind_obid on zzh_ob(object_id);
SQL> select object_id from zzh_ob where rownum<2;
 OBJECT_ID
----------
        16
SQL> exec dbms_stats.gather_table_stats(user,'zzh_ob',cascade=>true);
 
原sql执行计划:
SQL> set autot trace explain
SQL> select * from zzh_ob where object_id=16;
Execution Plan
----------------------------------------------------------
Plan hash value: 3485916696

------------------------------------------------------------------------------------------------
| Id  | Operation                           | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                    |          |     1 |   127 |     2   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID BATCHED| ZZH_OB   |     1 |   127 |     2   (0)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN                  | IND_OBID |     1 |       |     1   (0)| 00:00:01 |
------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("OBJECT_ID"=16)
 
新sql执行计划
SQL> select /*+ full(zzh_ob) */ * from zzh_ob where object_id=16;
Execution Plan
----------------------------------------------------------
Plan hash value: 474274488

----------------------------------------------------------------------------
| Id  | Operation         | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |        |     1 |   127 |   388   (1)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| ZZH_OB |     1 |   127 |   388   (1)| 00:00:01 |
----------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("OBJECT_ID"=16)

2、获取新sql的sql_id
SQL> col sql_id for a20
SQL> col sql_text for a120
SQL> select sql_id,sql_text from v$sql where sql_text like '%full(zzh_ob)%';
SQL_ID               SQL_TEXT
-------------------- ------------------------------------------------------------------------------------------------------------------------
g5btxvsh51ct5        EXPLAIN PLAN SET STATEMENT_ID='PLUS1200001' FOR select sql_id,sql_text from v$sql where sql_text like '%full(zzh_ob)%'
4kn9w9q3xf73k        EXPLAIN PLAN SET STATEMENT_ID='PLUS1200001' FOR select /*+ full(zzh_ob) */ * from zzh_ob where object_id=16
68x7hd7uaqqk1        select sql_id,sql_text from v$sql where sql_text like '%full(zzh_ob)%'
1syfgv2q5ggn2        select /*+ full(zzh_ob) */ * from zzh_ob where object_id=16
                     
3、获取新sql的outline
SQL> set pagesize 1000
SQL> select * from table(dbms_xplan.display_cursor('1syfgv2q5ggn2',null,'outline'));
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------
SQL_ID  1syfgv2q5ggn2, child number 0
-------------------------------------
select /*+ full(zzh_ob) */ * from zzh_ob where object_id=16

Plan hash value: 474274488

----------------------------------------------------------------------------
| Id  | Operation         | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |        |       |       |   388 (100)|          |
|*  1 |  TABLE ACCESS FULL| ZZH_OB |     1 |   127 |   388   (1)| 00:00:01 |

PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------

Outline Data
-------------

  /*+
      BEGIN_OUTLINE_DATA
      IGNORE_OPTIM_EMBEDDED_HINTS
      OPTIMIZER_FEATURES_ENABLE('19.1.0')
      DB_VERSION('19.1.0')
      ALL_ROWS

PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------
      OUTLINE_LEAF(@"SEL$1")
      FULL(@"SEL$1" "ZZH_OB"@"SEL$1")
      END_OUTLINE_DATA
  */

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("OBJECT_ID"=16)

32 rows selected.
 
4、创建sql profile(SQLPROFILE_01)                     
SQL> declare
   v_hints sys.sqlprof_attr;
   begin
   v_hints:=sys.sqlprof_attr(
     'BEGIN_OUTLINE_DATA',
     'IGNORE_OPTIM_EMBEDDED_HINTS',
     'OPTIMIZER_FEATURES_ENABLE(''19.1.0'')',
  'DB_VERSION(''19.1.0'')',
     'ALL_ROWS',
     'OUTLINE_LEAF(@"SEL$1")',
     'FULL(@"SEL$1" "ZZH_OB"@"SEL$1")',
     'END_OUTLINE_DATA');
   dbms_sqltune.import_sql_profile(
     'select * from zzh_ob where object_id=16',
     v_hints,'SQLPROFILE_01',
    force_match=>true,replace=>false);
  end;
  /
 
5、查看是否使用 sql profile
SQL> set autot trace explain
SQL> select * from zzh_ob where object_id=16;
Execution Plan
----------------------------------------------------------
Plan hash value: 474274488

----------------------------------------------------------------------------
| Id  | Operation         | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |        |     1 |   127 |   388   (1)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| ZZH_OB |     1 |   127 |   388   (1)| 00:00:01 |
----------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("OBJECT_ID"=16)

Note
-----
   - SQL profile "SQLPROFILE_01" used for this statement

Statistics
----------------------------------------------------------
        247  recursive calls
          0  db block gets
       1544  consistent gets
          2  physical reads
          0  redo size
       2685  bytes sent via SQL*Net to client
        401  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
         11  sorts (memory)
          0  sorts (disk)
          1  rows processed

查询已经创建的 sql profile : 
SELECT name, created, category, sql_Text from dba_sql_profiles ORDER BY created DESC;


http://www.kler.cn/a/464669.html

相关文章:

  • 基层医联体医院患者历史检验检查数据的快速Python编程分析
  • Linux系统在任意目录运行py脚本
  • RabbitMQ基础篇之Java客户端快速入门
  • C# 服务调用RFC函数获取物料信息,并输出生成Excel文件
  • 《深度学习梯度消失问题:原因与解决之道》
  • Git命令行的使用
  • 国产编辑器EverEdit - 常用资源汇总
  • QT---------自定义插件和库
  • Unity3D Huatuo:零成本高性能的全平台原生C#热更新方案详解
  • 玩《燕云十六声》时游戏文件丢失怎么办?
  • RabbitMQ基础篇之Java客户端 Topic交换机
  • 基于VSCode软件框架的RISC-V IDE MRS2正式上线发布
  • 使用Pandoc复制ChatGPT生成的高数公式文本
  • 学习笔记078——Java Properties类使用详解
  • js按日期按数量进行倒序排序,然后再新增一个字段,给这个字段赋值 10 到1
  • Lua迭代器如何使用?
  • element ui 组件 时间选择器出现转换问题的解决办法
  • 【门铃工作原理】2021-12-25
  • scipy和statsmodels之Python实现
  • Unity网络通信相关
  • 算命网站源码PHP框架_附2025新版设计书教程
  • Linux系统编程:深入理解计算机软硬件体系和架构
  • 前端页面上传文件:解决 ERR_UPLOAD_FILE_CHANGED
  • 基于Python flask 的微博高校舆情分析系统,高校微博情感分析大屏可视化
  • sql优化(1)
  • 优化现金流:CRM回款自动化的关键步骤