SQL Profile 实验02

实验说明:本实验利用工具coe_xfr_sql_profile.sql改变执行计划。
实验描述
通过创建表T3和索引T3_INDEX,执行SQL语句,正常情况下该走索引A情况,通过hint强制查询走全表扫描B情况,利用profile将A情况的执行计划改为B情况的执行计划,从而达到利用SQL Profile更改SQL执行计划,让所有查询走全表扫描的目的。(本文仅用于便于理解profile改变执行计划使用


##创建测试表和数据
SQL> conn scott/tiger;
Connected.
SQL> create table t3 as select * from all_objects;
Table created.
SQL> create index t3_index on t3(object_id);
Index created.
##开启trace跟踪
SQL> set autotrace trace explain;
##正常执行查询SQL,其走索引
SQL> select count(*) from t3 where object_id<=5000;

Execution Plan
----------------------------------------------------------
Plan hash value: 129501593

------------------------------------------------------------------------------
| Id  | Operation         | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |          |     1 |    13 |     9   (0)| 00:00:01 |
|   1 |  SORT AGGREGATE   |          |     1 |    13 |            |          |
|*  2 |   INDEX RANGE SCAN| T3_INDEX |  2500 | 32500 |     9   (0)| 00:00:01 |
------------------------------------------------------------------------------

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

   2 - access("OBJECT_ID"<=5000)

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

##给执行的SQL加hint,强制走全表扫描
SQL> select /*+full(t3)*/ count(*) from t3 where object_id<=5000;

Execution Plan
----------------------------------------------------------
Plan hash value: 463314188

---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |     1 |    13 |   327   (1)| 00:00:04 |
|   1 |  SORT AGGREGATE    |      |     1 |    13 |            |          |
|*  2 |   TABLE ACCESS FULL| T3   |  2500 | 32500 |   327   (1)| 00:00:04 |
---------------------------------------------------------------------------

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

   2 - filter("OBJECT_ID"<=5000)

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

#查询SQL_ID,PLAN_HASH_VALUE,SQL_TEXT内容
SQL> col sql_id for a20;
SQL> col sql_text for a100;
SQL> set linesize 200;
SQL> set pagesize 20000;
SQL> conn /as sysdba;
SQL> select sql_id,plan_hash_value,sql_text from v$sql where sql_text like '%count(*)%t3%where%';
SQL_ID        PLAN_HASH_VALUE SQL_TEXT
------------- --------------- ------------------------------------------------------------------------------------------------------------------------
4jtu9dktxjhgk       129501593 EXPLAIN PLAN SET STATEMENT_ID='PLUS735315' FOR select count(*) from t3 where object_id<=5000
01zbb82vy2af3       463314188 EXPLAIN PLAN SET STATEMENT_ID='PLUS735315' FOR select /*+full(t3)*/ count(*) from t3 where object_id<=5000
0gmwrzua0usax       129501593 select count(*) from t3 where object_id<=5000
fdv618tykuv26       463314188 select /*+full(t3)*/ count(*) from t3 where object_id<=5000
5872fg2ftfz76       903671040 select sql_id,sql_text from v$sql where sql_text like '%count(*)%t3%where%'
4qunptrts3tpg       903671040 select sql_id,plan_hash_value,sql_text from v$sql where sql_text like '%count(*)%t3%where%'


##根据要修改的SQL_ID生成Profile文件,其中让本走索引的SQL_ID走全表扫描的执行计划
SQL> @coe_xfr_sql_profile.sql

Parameter 1:
SQL_ID (required)

Enter value for 1: 0gmwrzua0usax                ##要修改的SQL语句的SQL_ID


PLAN_HASH_VALUE AVG_ET_SECS
--------------- -----------
      129501593

Parameter 2:
PLAN_HASH_VALUE (required)

Enter value for 2: 463314188                        ##将来要实现的SQL执行计划的PLAN_HASH_VALUE
    
Values passed to coe_xfr_sql_profile:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SQL_ID         : "0gmwrzua0usax"
PLAN_HASH_VALUE: "463314188"

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


COE_XFR_SQL_PROFILE completed.

#执行Profile导入操作
SQL>@coe_xfr_sql_profile_0gmwrzua0usax_463314188.sql

SQL>REM
SQL>REM $Header: 215187.1 coe_xfr_sql_profile_0gmwrzua0usax_463314188.sql 11.4.4.4 2016/04/05 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_0gmwrzua0usax_463314188.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 0gmwrzua0usax based on plan hash
SQL>REM   value 463314188.
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_0gmwrzua0usax_463314188.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_0gmwrzua0usax_463314188');
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 count(*) from t3 where object_id<=5000]');
 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" "T3"@"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_0gmwrzua0usax_463314188',
 31  description => 'coe 0gmwrzua0usax 463314188 '||: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
---------------------
  3834930897575772753


           SIGNATUREF
---------------------
 15295415828971277362


... manual custom SQL Profile has been created

COE_XFR_SQL_PROFILE_0gmwrzua0usax_463314188 completed

#测试执行计划是不是用到profile,并且走全表扫描

SQL> select count(*) from t3 where object_id<=5000;

Execution Plan
----------------------------------------------------------
Plan hash value: 463314188

---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |     1 |    13 |   277   (1)| 00:00:04 |
|   1 |  SORT AGGREGATE    |      |     1 |    13 |            |          |
|*  2 |   TABLE ACCESS FULL| T3   |  4129 | 53677 |   277   (1)| 00:00:04 |
---------------------------------------------------------------------------

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

   2 - filter("OBJECT_ID"<=5000)

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


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

转载于:http://blog.itpub.net/27067062/viewspace-2075505/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值