使用coe_xfr_sql_profile 绑定执行计划简单练习


应用有时候时快时慢,这时候可能会用到sql绑定。

一个简单的例子手动构造

适用于9.2之后的版本

需要

1)coe_xfr_sql_profile.sql (目前有两个版本一个是9.2和10.1的版本http://download.csdn.net/detail/huoshuyinhua/9652926;一个是之后的新版本地址http://download.csdn.net/detail/huoshuyinhua/9652918)

2)sql的sql_id  hash 值

sql_id   该语句的id

hash 值  记录该语句执行计划的一个值

下面的实验纯粹为了演示步骤,实际上所需的hash是效率比较好的执行计划产生的。(awr 报告,手动构造 sqlrpt等等)

已表ttvv为例(该表为ctas emp创建)
查询该表(实际生成一条语句)
SQL> select ename from ttvv where deptno=10;   


ENAME
----------
CLARK
KING
MILLER

1)查看该语句对应的sql_id

SQL> select sql_id,sql_text from v$sql where sql_text like 'select ename from ttvv%';


SQL_ID
-------------
SQL_TEXT
--------------------------------------------------------------------------------
16vjcwfzy5qk3
select ename from ttvv where deptno=10

2)查看执行计划(实际为了获取hash)
SQL> select * from table(dbms_xplan.display_cursor('16vjcwfzy5qk3',null));


PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID  16vjcwfzy5qk3, child number 0
-------------------------------------
select ename from ttvv where deptno=10


Plan hash value: 3876991463


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


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


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


   1 - filter("DEPTNO"=10)


Note
-----
   - dynamic sampling used for this statement (level=2)




22 rows selected.

3)生成该语句的执行计划的profile文件 (如果查询条件使用字符可以加force_match => true来强制匹配)

SQL> START coe_xfr_sql_profile.sql 16vjcwfzy5qk3 3876991463

Parameter 1:
SQL_ID (required)

PLAN_HASH_VALUE AVG_ET_SECS
--------------- -----------
     3876991463        .001


Parameter 2:
PLAN_HASH_VALUE (required)




Values passed to coe_xfr_sql_profile:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SQL_ID         : "16vjcwfzy5qk3"
PLAN_HASH_VALUE: "3876991463"


SQL>BEGIN
  2    IF :sql_text IS NULL THEN
  3      RAISE_APPLICATION_ERROR(-20100, 'SQL_TEXT for SQL_ID &&sql_id. was not found in memory (gv$sqltext_with_newlines) or AWR (dba_hist_sqltext).');
  4    END IF;
  5  END;
  6  /
SQL>SET TERM OFF;
SQL>BEGIN
  2    IF :other_xml IS NULL THEN
  3      RAISE_APPLICATION_ERROR(-20101, 'PLAN for SQL_ID &&sql_id. and PHV &&plan_hash_value. was not found in memory (gv$sql_plan) or AWR (dba_hist_sql_plan).');
  4    END IF;
  5  END;
  6  /
SQL>SET TERM OFF;

Execute coe_xfr_sql_profile_16vjcwfzy5qk3_3876991463.sql
on TARGET system in order to create a custom SQL Profile
with plan 3876991463 linked to adjusted sql_text.

COE_XFR_SQL_PROFILE completed.

4.上面生成了一个文件coe_xfr_sql_profile_16vjcwfzy5qk3_3876991463.sql 执行它
SQL>START coe_xfr_sql_profile.sql 16vjcwfzy5qk3 3876991463 


Parameter 1:
SQL_ID (required)


PLAN_HASH_VALUE AVG_ET_SECS
--------------- -----------
     3876991463        .001


Parameter 2:
PLAN_HASH_VALUE (required)




Values passed to coe_xfr_sql_profile:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SQL_ID         : "16vjcwfzy5qk3"
PLAN_HASH_VALUE: "3876991463"


SQL>BEGIN
  2    IF :sql_text IS NULL THEN
  3      RAISE_APPLICATION_ERROR(-20100, 'SQL_TEXT for SQL_ID &&sql_id. was not found in memory (gv$sqltext_with_newlines) or AWR (dba_hist_sqltext).');
  4    END IF;
  5  END;
  6  /
SQL>SET TERM OFF;
SQL>BEGIN
  2    IF :other_xml IS NULL THEN
  3      RAISE_APPLICATION_ERROR(-20101, 'PLAN for SQL_ID &&sql_id. and PHV &&plan_hash_value. was not found in memory (gv$sql_plan) or AWR (dba_hist_sql_plan).');
  4    END IF;
  5  END;
  6  /
SQL>SET TERM OFF;


Execute coe_xfr_sql_profile_16vjcwfzy5qk3_3876991463.sql
on TARGET system in order to create a custom SQL Profile
with plan 3876991463 linked to adjusted sql_text.


COE_XFR_SQL_PROFILE completed.
SQL>SQL>@coe_xfr_sql_profile_16vjcwfzy5qk3_3876991463.sql
SQL>REM
SQL>REM $Header: 215187.1 coe_xfr_sql_profile_16vjcwfzy5qk3_3876991463.sql 11.4.4.4 2016/10/13 carlos.sierra $
SQL>REM
SQL>REM Copyright (c) 2000-2012, Oracle Corporation. All rights reserved.
SQL>REM
SQL>REM AUTHOR
SQL>REM   [email protected]
SQL>REM
SQL>REM SCRIPT
SQL>REM   coe_xfr_sql_profile_16vjcwfzy5qk3_3876991463.sql
SQL>REM
SQL>REM DESCRIPTION
SQL>REM   This script is generated by coe_xfr_sql_profile.sql
SQL>REM   It contains the SQL*Plus commands to create a custom
SQL>REM   SQL Profile for SQL_ID 16vjcwfzy5qk3 based on plan hash
SQL>REM   value 3876991463.
SQL>REM   The custom SQL Profile to be created by this script
SQL>REM   will affect plans for SQL commands with signature
SQL>REM   matching the one for SQL Text below.
SQL>REM   Review SQL Text and adjust accordingly.
SQL>REM
SQL>REM PARAMETERS
SQL>REM   None.
SQL>REM
SQL>REM EXAMPLE
SQL>REM   SQL> START coe_xfr_sql_profile_16vjcwfzy5qk3_3876991463.sql;
SQL>REM
SQL>REM NOTES
SQL>REM   1. Should be run as SYSTEM or SYSDBA.
SQL>REM   2. User must have CREATE ANY SQL PROFILE privilege.
SQL>REM   3. SOURCE and TARGET systems can be the same or similar.
SQL>REM   4. To drop this custom SQL Profile after it has been created:
SQL>REM  EXEC DBMS_SQLTUNE.DROP_SQL_PROFILE('coe_16vjcwfzy5qk3_3876991463');
SQL>REM   5. Be aware that using DBMS_SQLTUNE requires a license
SQL>REM  for the Oracle Tuning Pack.
SQL>REM   6. If you modified a SQL putting Hints in order to produce a desired
SQL>REM  Plan, you can remove the artifical Hints from SQL Text pieces below.
SQL>REM  By doing so you can create a custom SQL Profile for the original
SQL>REM  SQL but with the Plan captured from the modified SQL (with Hints).
SQL>REM
SQL>WHENEVER SQLERROR EXIT SQL.SQLCODE;
SQL>REM
SQL>VAR signature NUMBER;
SQL>VAR signaturef NUMBER;
SQL>REM
SQL>DECLARE
  2  sql_txt CLOB;
  3  h       SYS.SQLPROF_ATTR;
  4  PROCEDURE wa (p_line IN VARCHAR2) IS
  5  BEGIN
  6  DBMS_LOB.WRITEAPPEND(sql_txt, LENGTH(p_line), p_line);
  7  END wa;
  8  BEGIN
  9  DBMS_LOB.CREATETEMPORARY(sql_txt, TRUE);
 10  DBMS_LOB.OPEN(sql_txt, DBMS_LOB.LOB_READWRITE);
 11  -- SQL Text pieces below do not have to be of same length.
 12  -- So if you edit SQL Text (i.e. removing temporary Hints),
 13  -- there is no need to edit or re-align unmodified pieces.
 14  wa(q'[select ename from ttvv where deptno=10]');
 15  DBMS_LOB.CLOSE(sql_txt);
 16  h := SYS.SQLPROF_ATTR(
 17  q'[BEGIN_OUTLINE_DATA]',
 18  q'[IGNORE_OPTIM_EMBEDDED_HINTS]',
 19  q'[OPTIMIZER_FEATURES_ENABLE('11.2.0.4')]',
 20  q'[DB_VERSION('11.2.0.4')]',
 21  q'[ALL_ROWS]',
 22  q'[OUTLINE_LEAF(@"SEL$1")]',
 23  q'[FULL(@"SEL$1" "TTVV"@"SEL$1")]',
 24  q'[END_OUTLINE_DATA]');
 25  :signature := DBMS_SQLTUNE.SQLTEXT_TO_SIGNATURE(sql_txt);
 26  :signaturef := DBMS_SQLTUNE.SQLTEXT_TO_SIGNATURE(sql_txt, TRUE);
 27  DBMS_SQLTUNE.IMPORT_SQL_PROFILE (
 28  sql_text    => sql_txt,
 29  profile     => h,
 30  name        => 'coe_16vjcwfzy5qk3_3876991463',
 31  description => 'coe 16vjcwfzy5qk3 3876991463 '||:signature||' '||:signaturef||'',
 32  category    => 'DEFAULT',
 33  validate    => TRUE,
 34  replace     => TRUE,
 35  force_match => FALSE /* TRUE:FORCE (match even when different literals in SQL). FALSE:EXACT (similar to CURSOR_SHARING) */ );
 36  DBMS_LOB.FREETEMPORARY(sql_txt);
 37  END;
 38  /


PL/SQL procedure successfully completed.


SQL>WHENEVER SQLERROR CONTINUE
SQL>SET ECHO OFF;


            SIGNATURE
---------------------
 15680234406244522204




           SIGNATUREF
---------------------
 10847459596262927715




... manual custom SQL Profile has been created




COE_XFR_SQL_PROFILE_16vjcwfzy5qk3_3876991463 completed
SQL>


5.执行完查看是否更改成功



SQL>select SQL_ID, SQL_PROFILE,PLAN_HASH_VALUE from V$SQL where SQL_ID='16vjcwfzy5qk3';


SQL_ID        SQL_PROFILE
------------- ----------------------------------------------------------------
PLAN_HASH_VALUE
---------------
16vjcwfzy5qk3
     3876991463

你可能感兴趣的:(oracle,优化)