ITPub博客

首页 > 数据库 > Oracle > 利用coe_xfr_sql_profile 改变执行计划

利用coe_xfr_sql_profile 改变执行计划

原创 Oracle 作者:wzz123snow 时间:2015-04-27 14:43:41 0 删除 编辑
有的时候针对一些线上的SQL出现的性能问题,可能由于种种原因当时没法去修改SQL,那么就可以使用SQL_PROFILE去改变SQL的执行计划,
来达到不修改SQL的方法。下面就是利用coe_xfr_sql_profile 改变执行计划的一个小例子。


--首先创建测试表
SQL> create table t2 as select * from dba_objects;


表已创建。
--对测试表建立索引
SQL> create index t2_id on t2(object_id);


索引已创建。


SQL> set linesize 9999 pagesize 9999
--我们发现,默认走的是索引
SQL> set autotrace traceonly
SQL> select * from t2 where object_id=2000;




执行计划
----------------------------------------------------------
Plan hash value: 3119810522


-------------------------------------------------------------------------------------
| Id  | Operation                   | Name  | Rows  | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |       |     1 |   207 |     4   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| T2    |     1 |   207 |     4   (0)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN          | T2_ID |     1 |       |     3   (0)| 00:00:01 |
-------------------------------------------------------------------------------------


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


   2 - access("OBJECT_ID"=2000)


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




统计信息
----------------------------------------------------------
        312  recursive calls
          0  db block gets
        143  consistent gets
        310  physical reads
          0  redo size
       1270  bytes sent via SQL*Net to client
        359  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed


--我们加入HINT,强制SQL不走索引,走全表扫描
SQL> select /*+ full(t2) */ * from t2 where object_id=2000;




执行计划
----------------------------------------------------------
Plan hash value: 1513984157


--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |     1 |   207 |  1971   (1)| 00:00:24 |
|*  1 |  TABLE ACCESS FULL| T2   |     1 |   207 |  1971   (1)| 00:00:24 |
--------------------------------------------------------------------------


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


   1 - filter("OBJECT_ID"=2000)


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




统计信息
----------------------------------------------------------
          7  recursive calls
          0  db block gets
       7222  consistent gets
       7124  physical reads
          0  redo size
       1267  bytes sent via SQL*Net to client
        359  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed




--根据SQL_TEXT查询SQL_ID
SQL> select sql_id,sql_text from v$sql where sql_text like '%select /*+ full(t2) */ * from t2 where object_id=2000%';


SQL_ID        SQL_TEXT
------------- -----------------------------------------------------------------------------------------------------------------------------
--------------
cyuf40p4509zh select sql_id from v$sql where sql_text like '%select /*+ full(t2) */ * from t2 where object_id=2000%'
7dgv9kgj9htvy EXPLAIN PLAN SET STATEMENT_ID='PLUS13840030' FOR select /*+ full(t2) */ * from t2 where object_id=2000
dt7mc2ws2x9rt select /*+ full(t2) */ * from t2 where object_id=2000
8y44mnwb6umyb select sql_id,sql_text from v$sql where sql_text like '%select /*+ full(t2) */ * from t2 where object_id=2000%'


--根据SQL_ID查询PLAN_HASH_VALUE,后面要用到这两个值
SQL> select sql_id,plan_hash_value from gv$sql where sql_id='dt7mc2ws2x9rt';


SQL_ID        PLAN_HASH_VALUE
------------- ---------------
dt7mc2ws2x9rt      1513984157






--运行脚本coe_xfr_sql_profile.sql,要求输入SQL_ID和PLAN_HASH_VALUE
SQL> @ F:\coe_xfr_sql_profile.sql


Parameter 1:
SQL_ID (required)


输入 1 的值:  dt7mc2ws2x9rt




PLAN_HASH_VALUE AVG_ET_SECS
--------------- -----------
     1513984157       1.014


Parameter 2:
PLAN_HASH_VALUE (required)


输入 2 的值:  1513984157


Values passed to coe_xfr_sql_profile:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SQL_ID         : "dt7mc2ws2x9rt"
PLAN_HASH_VALUE: "1513984157"


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_dt7mc2ws2x9rt_1513984157.sql
on TARGET system in order to create a custom SQL Profile
with plan 1513984157 linked to adjusted sql_text.




COE_XFR_SQL_PROFILE completed.
--会生成一个新的coe_xfr_sql_profile_dt7mc2ws2x9rt_1513984157.sql
--修改新生成的coe_xfr_sql_profile_dt7mc2ws2x9rt_1513984157.sql
需要修改的内容:SQL_TEXT(让SQL_TEXT走咱们想要走的全表扫描)
                force_match=>TRUE
                name        => 'TEST11'


然后重新运行这个改过的脚本
SQL>@ C:\Users\lenovo\coe_xfr_sql_profile_dt7mc2ws2x9rt_1513984157.sql
SQL>REM
SQL>REM $Header: 215187.1 coe_xfr_sql_profile_dt7mc2ws2x9rt_1513984157.sql 11.4.4.4 2015/04/27 carlos.sierra $
SQL>REM
SQL>REM Copyright (c) 2000-2012, Oracle Corporation. All rights reserved.
SQL>REM
SQL>REM AUTHOR
SQL>REM   carlos.sierra@oracle.com
SQL>REM
SQL>REM SCRIPT
SQL>REM   coe_xfr_sql_profile_dt7mc2ws2x9rt_1513984157.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 dt7mc2ws2x9rt based on plan hash
SQL>REM   value 1513984157.
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_dt7mc2ws2x9rt_1513984157.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_dt7mc2ws2x9rt_1513984157');
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 * from t2 where object_id=2000 ]');
 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.1')]',
 20  q'[DB_VERSION('11.2.0.1')]',
 21  q'[ALL_ROWS]',
 22  q'[OUTLINE_LEAF(@"SEL$1")]',
 23  q'[FULL(@"SEL$1" "T2"@"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        => 'TEST11',
 31  description => 'coe dt7mc2ws2x9rt 1513984157 '||:signature||' '||:signaturef||'',
 32  category    => 'DEFAULT',
 33  validate    => TRUE,
 34  replace     => TRUE,
 35  force_match => TRUE /* 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 过程已成功完成。


SQL>WHENEVER SQLERROR CONTINUE
SQL>SET ECHO OFF;


            SIGNATURE
---------------------
  1059328793824096756




           SIGNATUREF
---------------------
  7052402994300291970




... manual custom SQL Profile has been created




COE_XFR_SQL_PROFILE_dt7mc2ws2x9rt_1513984157 completed


--再次看看原来的SQL,是否走了新的执行计划呢?


SQL>set autotrace traceonly
SQL>select * from t2 where object_id=2000;




执行计划
----------------------------------------------------------
Plan hash value: 1513984157


--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |  5919 |  1196K|  1970   (1)| 00:00:24 |
|*  1 |  TABLE ACCESS FULL| T2   |  5919 |  1196K|  1970   (1)| 00:00:24 |
--------------------------------------------------------------------------


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


   1 - filter("OBJECT_ID"=2000)


Note
-----
   - SQL profile "TEST11" used for this statement--我们发现SQL_PROFILE使用上了




统计信息
----------------------------------------------------------
          0  recursive calls
          0  db block gets
       7129  consistent gets
       7124  physical reads
          0  redo size
       1267  bytes sent via SQL*Net to client
        359  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26084062/viewspace-1606551/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2012-11-13

  • 博文量
    27
  • 访问量
    404583