[20180301]sql profile 非绑定变量.txt

简介: [20180301]sql profile 非绑定变量.txt http://www.itpub.net/thread-2097379-1-1.html 1.环境: SCOTT@book> @ &r/ver1 PORT_STRING            ...

[20180301]sql profile 非绑定变量.txt

http://www.itpub.net/thread-2097379-1-1.html

1.环境:

SCOTT@book> @ &r/ver1
PORT_STRING                    VERSION        BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx            11.2.0.4.0     Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

create table t as select rownum id ,rownum||'test' from dual connect by levle<=1e5;
create unique index pk_t on t(id);

select /*+ full(t) */ * from t where id=42;

--//定位sql_id=657njzq307t7y,执行计划选择全表扫描,因为提示使用全表扫描.

2.使用sql profile:

--//执行@ &r/sp1 657njzq307t7y,完成sql profile的分析.关于sp1的脚本可以参考.http://blog.itpub.net/267265/viewspace-1340660/

SCOTT@book> @ &r/sp1 657njzq307t7y
PL/SQL procedure successfully completed.

=================================================================================================================================================
tuning sql_id=657njzq307t7y : report
=================================================================================================================================================
REPORT_TUNING_TASK
-----------------------------
GENERAL INFORMATION SECTION
-------------------------------------------------------------------------------
Tuning Task Name   : tuning 657njzq307t7y
Tuning Task Owner  : SCOTT
Workload Type      : Single SQL Statement
Scope              : COMPREHENSIVE
Time Limit(seconds): 1800
Completion Status  : COMPLETED
Started at         : 03/01/2018 08:48:47
Completed at       : 03/01/2018 08:48:48

-------------------------------------------------------------------------------
Schema Name: SCOTT
SQL ID     : 657njzq307t7y
SQL Text   : select /*+ full(t) */ * from t where id=42

-------------------------------------------------------------------------------
FINDINGS SECTION (2 findings)
-------------------------------------------------------------------------------

1- Statistics Finding
---------------------
  Table "SCOTT"."T" was not analyzed.

  Recommendation
  --------------
  - Consider collecting optimizer statistics for this table.
    execute dbms_stats.gather_table_stats(ownname => 'SCOTT', tabname => 'T',
            estimate_percent => DBMS_STATS.AUTO_SAMPLE_SIZE, method_opt =>
            'FOR ALL COLUMNS SIZE AUTO');

  Rationale
  ---------
    The optimizer requires up-to-date statistics for the table in order to
    select a good execution plan.

2- SQL Profile Finding (see explain plans section below)
--------------------------------------------------------
  A potentially better execution plan was found for this statement.

  Recommendation (estimated benefit: 98.92%)
  ------------------------------------------
  - Consider accepting the recommended SQL profile.
    execute dbms_sqltune.accept_sql_profile(task_name => 'tuning
            657njzq307t7y', task_owner => 'SCOTT', replace => TRUE);

  Validation results
  ------------------
  The SQL profile was tested by executing both its plan and the original plan
  and measuring their respective execution statistics. A plan may have been
  only partially executed if the other could be run to completion in less time.
                           Original Plan  With SQL Profile  % Improved
                           -------------  ----------------  ----------
  Completion Status:            COMPLETE          COMPLETE
  Elapsed Time (s):             .001198           .000028      97.66 %
  CPU Time (s):                 .001199             .0001      91.65 %
  User I/O Time (s):                  0                 0
  Buffer Gets:                      279                 3      98.92 %
  Physical Read Requests:             0                 0
  Physical Write Requests:            0                 0
  Physical Read Bytes:                0                 0
  Physical Write Bytes:               0                 0
  Rows Processed:                     1                 1
  Fetches:                            1                 1
  Executions:                         1                 1

  Notes
  -----
  1. Statistics for the original plan were averaged over 10 executions.
  2. Statistics for the SQL profile plan were averaged over 10 executions.

-------------------------------------------------------------------------------
EXPLAIN PLANS SECTION
-------------------------------------------------------------------------------

1- Original With Adjusted Cost
------------------------------
Plan hash value: 1601196873

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |     1 |    13 |    81   (2)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| T    |     1 |    13 |    81   (2)| 00:00:01 |
--------------------------------------------------------------------------

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

   1 - filter("ID"=42)

2- Using SQL Profile
--------------------
Plan hash value: 2454218153

------------------------------------------------------------------------------------
| Id  | Operation                   | Name | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |      |     1 |    13 |     2   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| T    |     1 |    13 |     2   (0)| 00:00:01 |
|*  2 |   INDEX UNIQUE SCAN         | PK_T |     1 |       |     1   (0)| 00:00:01 |
------------------------------------------------------------------------------------

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

   2 - access("ID"=42)

-------------------------------------------------------------------------------
=================================================================================================================================================
if finished,drop tuning task , run:
execute dbms_sqltune.drop_tuning_task('tuning 657njzq307t7y')
if accept sql profile, run:
execute dbms_sqltune.accept_sql_profile(task_name => 'tuning 657njzq307t7y', replace => TRUE ,name=>'tuning 657njzq307t7y');
execute dbms_sqltune.accept_sql_profile(task_name => 'tuning 657njzq307t7y', replace => TRUE, name=>'tuning 657njzq307t7y', FORCE_MATCH=>True)
if drop or alter sql profile ,run :
execute dbms_sqltune.drop_sql_profile(name => 'tuning 657njzq307t7y')
execute dbms_sqltune.alter_sql_profile(name => 'tuning 657njzq307t7y',attribute_name=>'STATUS',value=>'DISABLED')
=================================================================================================================================================

3.测试不同的值是否有效:

SCOTT@book> execute dbms_sqltune.accept_sql_profile(task_name => 'tuning 657njzq307t7y', replace => TRUE, name=>'tuning 657njzq307t7y', FORCE_MATCH=>True);
PL/SQL procedure successfully completed.

SCOTT@book> select /*+ full(t) */ * from t where id=41;
        ID NAME
---------- --------------------
        41 41test

--//使用不同的值测试看看sql profile是否有效.

SCOTT@book> @ &r/dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  57rqfa0t3p3rn, child number 0
-------------------------------------
select /*+ full(t) */ * from t where id=41
Plan hash value: 2454218153
-------------------------------------------------------------------------------------
| Id  | Operation                   | Name | E-Rows |E-Bytes| Cost (%CPU)| E-Time   |
-------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |      |        |       |     2 (100)|          |
|   1 |  TABLE ACCESS BY INDEX ROWID| T    |      1 |    13 |     2   (0)| 00:00:01 |
|*  2 |   INDEX UNIQUE SCAN         | PK_T |      1 |       |     1   (0)| 00:00:01 |
-------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
   1 - SEL$1 / T@SEL$1
   2 - SEL$1 / T@SEL$1
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - access("ID"=41)
Note
-----
   - SQL profile tuning 657njzq307t7y used for this statement
   - Warning: basic plan statistics not available. These are only collected when:
       * hint 'gather_plan_statistics' is used for the statement or
       * parameter 'statistics_level' is set to 'ALL', at session or system level
32 rows selected.
--//使用sql profile tuning 657njzq307t7y.
--//换各种方式执行:

SCOTT@book> alter system flush shared_pool;
System altered.

SCOTT@book> select sql_id,sql_text,EXACT_MATCHING_SIGNATURE, FORCE_MATCHING_SIGNATURE,sql_profile from v$sql where FORCE_MATCHING_SIGNATURE=8045308685519010163;
SQL_ID        SQL_TEXT                                                     EXACT_MATCHING_SIGNATURE FORCE_MATCHING_SIGNATURE SQL_PROFILE
------------- ------------------------------------------------------------ ------------------------ ------------------------ ---------------------
5pjxf22kq49cd select /*+ full(t) */ * from t where id=1                         9666810874799682817      8045308685519010163 tuning 657njzq307t7y
57rqfa0t3p3rn select /*+ full(t) */ * from t where id=41                        7008154288030403093      8045308685519010163 tuning 657njzq307t7y
7grmh3jz0dcyc select /*+ full(t) */ * from t where id=        21                4036573941054301337      8045308685519010163 tuning 657njzq307t7y
agpq3dtz7kpnm Select /*+ full(t) */ * from t where id=        21                4036573941054301337      8045308685519010163 tuning 657njzq307t7y
gukbt51cn7f76 select /*+ full(t) */ * from t where id=21                        4036573941054301337      8045308685519010163 tuning 657njzq307t7y
657njzq307t7y select /*+ full(t) */ * from t where id=42                        4762379214270820258      8045308685519010163 tuning 657njzq307t7y
0ab2yuh6t6fm8 Select /*+ Full(t) */ * from t where id=        21                4036573941054301337      8045308685519010163 tuning 657njzq307t7y
6 rows selected.

--//可以发现使用非绑定变量,,sql profile 还是选择正确的执行计划.只要FORCE_MATCHING_SIGNATURE一样,都可以选择正确的执行计划.
--//只要在接受sql profile时FORCE_MATCH=>True,这样相关非绑定变量的语句都会使用指定的执行计划.
--//ZALBB的想法我的理解就是通过sql profile,原来有多个执行计划的语句,通过这样的方式选择正确的执行计划,也减少了子光标.也减少了硬解析.
--//不知道我的理解是否正确.

--//更正一些错误:使用sql profile并不能减少硬解析,要想减少修改语句为绑定变量,或者通过cursor_sharing=force来控制.
--//总之开发还是应该知道合理使用绑定变量的好处.

目录
相关文章
|
7月前
|
SQL 存储 Oracle
Oracle的PL/SQL定义变量和常量:数据的稳定与灵动
【4月更文挑战第19天】在Oracle PL/SQL中,变量和常量扮演着数据存储的关键角色。变量是可变的“魔术盒”,用于存储程序运行时的动态数据,通过`DECLARE`定义,可在循环和条件判断中体现其灵活性。常量则是不可变的“固定牌”,一旦设定值便保持不变,用`CONSTANT`声明,提供程序稳定性和易维护性。通过 `%TYPE`、`NOT NULL`等特性,可以更高效地管理和控制变量与常量,提升代码质量。善用两者,能优化PL/SQL程序的结构和性能。
|
5月前
|
SQL 运维 分布式计算
DataWorks产品使用合集之ODPPS中如何使用SQL查询从表中获取值并将其赋值给临时变量以供后续使用
DataWorks作为一站式的数据开发与治理平台,提供了从数据采集、清洗、开发、调度、服务化、质量监控到安全管理的全套解决方案,帮助企业构建高效、规范、安全的大数据处理体系。以下是对DataWorks产品使用合集的概述,涵盖数据处理的各个环节。
|
5月前
|
SQL 存储 数据库
MySQL设计规约问题之性能分析工具如Sql explain、show profile和mysqlsla在数据库性能优化中有什么作用
MySQL设计规约问题之性能分析工具如Sql explain、show profile和mysqlsla在数据库性能优化中有什么作用
|
6月前
|
SQL 关系型数据库 MySQL
MySQL数据库——索引(4)-SQL性能分析-profile详情、explain(profile查看指令,explain执行计划中各个字段的含义)
MySQL数据库——索引(4)-SQL性能分析-profile详情、explain(profile查看指令,explain执行计划中各个字段的含义)
81 2
|
6月前
|
SQL 存储 分布式计算
MaxCompute产品使用问题之odps sql如何定义变量
MaxCompute作为一款全面的大数据处理平台,广泛应用于各类大数据分析、数据挖掘、BI及机器学习场景。掌握其核心功能、熟练操作流程、遵循最佳实践,可以帮助用户高效、安全地管理和利用海量数据。以下是一个关于MaxCompute产品使用的合集,涵盖了其核心功能、应用场景、操作流程以及最佳实践等内容。
226 0
|
6月前
|
SQL 缓存 Oracle
SQL调优之绑定变量用法简介
SQL调优之绑定变量用法简介
|
SQL 关系型数据库 MySQL
可以使用SQL语句来声明变量、设置自变量以及根据计算结果赋值
可以使用SQL语句来声明变量、设置自变量以及根据计算结果赋值
147 3
|
SQL 缓存 监控
为什么Oracle中包含绑定变量的SQL会有多个游标?(译文)
为了改进包含绑定变量的SQL的执行计划,Oracle在11g版本中引入了一项名为自适应游标共享(ACS,Adaptive Cursor Sharing)的新功能。
182 0
|
SQL Perl
PL/SQL编程—变量
PL/SQL编程—变量
93 0