---
title: "使用SQL PROFILE固定SHARED POOL中的执行计划"
author: "jayghost"
date: "2013-12-12T15:57:38Z"
tags: []
categories: ["数据库"]
slug: "sql-profile-shared-pool"
draft: false
lastmod: "2013-12-12T15:57:38Z"
id: 1988628
url: "https://wujay.iteye.com/blog/1988628"
---

如何使用SQL PROFILE固定当前SHARED POOL的执行计划。

 

如果采用手工的方法，需要用到 sqlprof\_attr 类型。

自己手工构造相对麻烦点。

ORACLE提供了一个脚本coe\_xfr\_sql\_profile.sql 用来用共享池中获得SQL语句及其执行计划，

并生成一个创建SQL PROFLE的脚本，这个脚本附属在SQLT工具中。

看一下这个脚本的使用方法。

 

 

 

SQL&gt; desc test

 名称                                                  是否为空? 类型

 ----------------------------------------------------- -------- ------------------------------------

 OWNER                                                 NOT NULL VARCHAR2(30)

 OBJECT\_NAME                                           NOT NULL VARCHAR2(30)

 SUBOBJECT\_NAME                                                 VARCHAR2(30)

 OBJECT\_ID                                             NOT NULL NUMBER

 DATA\_OBJECT\_ID                                                 NUMBER

 OBJECT\_TYPE                                                    VARCHAR2(19)

 CREATED                                               NOT NULL DATE

 LAST\_DDL\_TIME                                         NOT NULL DATE

 TIMESTAMP                                                      VARCHAR2(19)

 STATUS                                                         VARCHAR2(7)

 TEMPORARY                                                      VARCHAR2(1)

 GENERATED                                                      VARCHAR2(1)

 SECONDARY                                                      VARCHAR2(1)

 

SQL&gt; set autot traceonly exp

SQL&gt; select count(1) from test where object\_type='SYNONYM';

 

执行计划

\----------------------------------------------------------

Plan hash value: 3958077978

 

\-------------------------------------------------------------------------------------

| Id  | Operation             | Name        | Rows  | Bytes | Cost (%CPU)| Time     |

\-------------------------------------------------------------------------------------

|   0 | SELECT STATEMENT      |             |     1 |     9 |    35   (3)| 00:00:01 |

|   1 |  SORT AGGREGATE       |             |     1 |     9 |            |          |

|*  2 |   INDEX FAST FULL SCAN| IDX\_CPIC\_01 | 19221 |   168K|    35   (3)| 00:00:01 |

\-------------------------------------------------------------------------------------

 

Predicate Information (identified by operation id):

\---------------------------------------------------

 

   2 - filter("OBJECT\_TYPE"='SYNONYM')

 

SQL&gt; set autot off

SQL&gt; column sql\_text format a40

SQL&gt; select sql\_id,sql\_text from v$sql where sql\_text like 'select count(1) from test where object\_type=%';

 

SQL\_ID        SQL\_TEXT

\------------- ----------------------------------------

8ma7qaqmmt5bn select count(1) from test where object\_t

              ype='SYNONYM'

 

 

SQL&gt; select plan\_hash\_value from v$sql\_plan where sql\_id='8ma7qaqmmt5bn' and rownum=1;

 

PLAN\_HASH\_VALUE

\---------------

     3958077978

 

SQL&gt;

 

下面我们用coe\_xfr\_sql\_profile.sql脚本生成创建SQL PROFILE的脚本。

 

SQL&gt; @coe\_xfr\_sql\_profile.sql 8ma7qaqmmt5bn 3958077978

 

Parameter 1:

SQL\_ID (required)

 

 

 

PLAN\_HASH\_VALUE AVG\_ET\_SECS

\--------------- -----------

     3958077978

 

Parameter 2:

PLAN\_HASH\_VALUE (required)

 

 

Values passed to coe\_xfr\_sql\_profile:

\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

SQL\_ID         : "8ma7qaqmmt5bn"

PLAN\_HASH\_VALUE: "3958077978"

 

SQL&gt;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&gt;SET TERM OFF;

SQL&gt;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&gt;SET TERM OFF;

 

Execute coe\_xfr\_sql\_profile\_8ma7qaqmmt5bn\_3958077978.sql

on TARGET system in order to create a custom SQL Profile

with plan 3958077978 linked to adjusted sql\_text.

 

 

COE\_XFR\_SQL\_PROFILE completed.

SQL&gt;

 

 

生成的脚本coe\_xfr\_sql\_profile\_8ma7qaqmmt5bn\_3958077978.sql内容如下：

 

SPO coe\_xfr\_sql\_profile\_8ma7qaqmmt5bn\_3958077978.log;

SET ECHO ON TERM ON LIN 2000 TRIMS ON NUMF 99999999999999999999;

REM

REM $Header: 215187.1 coe\_xfr\_sql\_profile\_8ma7qaqmmt5bn\_3958077978.sql 11.4.4.4 2013/01/11 carlos.sierra $

REM

REM Copyright (c) 2000-2012, Oracle Corporation. All rights reserved.

REM

REM AUTHOR

REM   carlos.sierra@[oracle](http://biancheng.dnbcw.info/oracle/).com

REM

REM SCRIPT

REM   coe\_xfr\_sql\_profile\_8ma7qaqmmt5bn\_3958077978.sql

REM

REM DESCRIPTION

REM   This script is generated by coe\_xfr\_sql\_profile.sql

REM   It contains the SQL\*Plus commands to create a custom

REM   SQL Profile for SQL\_ID 8ma7qaqmmt5bn based on plan hash

REM   value 3958077978.

REM   The custom SQL Profile to be created by this script

REM   will affect plans for SQL commands with signature

REM   matching the one for SQL Text below.

REM   Review SQL Text and adjust accordingly.

REM

REM PARAMETERS

REM   None.

REM

REM EXAMPLE

REM   SQL&gt; START coe\_xfr\_sql\_profile\_8ma7qaqmmt5bn\_3958077978.sql;

REM

REM NOTES

REM   1. Should be run as SYSTEM or SYSDBA.

REM   2. User must have CREATE ANY SQL PROFILE privilege.

REM   3. SOURCE and TARGET systems can be the same or similar.

REM   4. To drop this custom SQL Profile after it has been created:

REM      EXEC DBMS\_SQLTUNE.DROP\_SQL\_PROFILE('coe\_8ma7qaqmmt5bn\_3958077978');

REM   5. Be aware that using DBMS\_SQLTUNE requires a license

REM      for the Oracle Tuning Pack.

REM   6. If you modified a SQL putting Hints in order to produce a desired

REM      Plan, you can remove the artifical Hints from SQL Text pieces below.

REM      By doing so you can create a custom SQL Profile for the original

REM      SQL but with the Plan captured from the modified SQL (with Hints).

REM

WHENEVER SQLERROR EXIT SQL.SQLCODE;

REM

VAR signature NUMBER;

VAR signaturef NUMBER;

REM

DECLARE

sql\_txt CLOB;

h       SYS.SQLPROF\_ATTR;

PROCEDURE wa (p\_line IN VARCHAR2) IS

BEGIN

DBMS\_LOB.WRITEAPPEND(sql\_txt, LENGTH(p\_line), p\_line);

END wa;

BEGIN

DBMS\_LOB.CREATETEMPORARY(sql\_txt, TRUE);

DBMS\_LOB.OPEN(sql\_txt, DBMS\_LOB.LOB\_READWRITE);

-- SQL Text pieces below do not have to be of same length.

-- So if you edit SQL Text (i.e. removing temporary Hints),

-- there is no need to edit or re-align unmodified pieces.

wa(q'\[select count(1) from test where object\_type='SYNONYM' ]');

DBMS\_LOB.CLOSE(sql\_txt);

h := SYS.SQLPROF\_ATTR(

q'\[BEGIN\_OUTLINE\_DATA]',

q'\[IGNORE\_OPTIM\_EMBEDDED\_HINTS]',

q'\[OPTIMIZER\_FEATURES\_ENABLE('10.2.0.1')]',

q'\[ALL\_ROWS]',

q'\[OUTLINE\_LEAF(@"SEL$1")]',

q'\[INDEX\_FFS(@"SEL$1" "TEST"@"SEL$1" ("TEST"."OBJECT\_TYPE"))]',

q'\[END\_OUTLINE\_DATA]');

:signature := DBMS\_SQLTUNE.SQLTEXT\_TO\_SIGNATURE(sql\_txt);

:signaturef := DBMS\_SQLTUNE.SQLTEXT\_TO\_SIGNATURE(sql\_txt, TRUE);

DBMS\_SQLTUNE.IMPORT\_SQL\_PROFILE (

sql\_text    =&gt; sql\_txt,

profile     =&gt; h,

name        =&gt; 'coe\_8ma7qaqmmt5bn\_3958077978',

description =&gt; 'coe 8ma7qaqmmt5bn 3958077978 '||:signature||' '||:signaturef||'',

category    =&gt; 'DEFAULT',

validate    =&gt; TRUE,

replace     =&gt; TRUE,

force\_match =&gt; FALSE /* TRUE:FORCE (match even when different literals in SQL). FALSE:EXACT (similar to CURSOR\_SHARING) \*/ );

DBMS\_LOB.FREETEMPORARY(sql\_txt);

END;

/

WHENEVER SQLERROR CONTINUE

SET ECHO OFF;

PRINT signature

PRINT signaturef

PRO

PRO ... manual custom SQL Profile has been created

PRO

SET TERM ON ECHO OFF LIN 80 TRIMS OFF NUMF "";

SPO OFF;

PRO

PRO COE\_XFR\_SQL\_PROFILE\_8ma7qaqmmt5bn\_3958077978 completed

 

 

我们可以直接使用这个脚本，也可以自己根据实际情况进行修改后在执行。

 

SQL&gt;@coe\_xfr\_sql\_profile\_8ma7qaqmmt5bn\_3958077978.sql

SQL&gt;REM

SQL&gt;REM $Header: 215187.1 coe\_xfr\_sql\_profile\_8ma7qaqmmt5bn\_3958077978.sql 11.4.4.4 2013/01/11 carlos.sierra $

SQL&gt;REM

SQL&gt;REM Copyright (c) 2000-2012, Oracle Corporation. All rights reserved.

SQL&gt;REM

SQL&gt;REM AUTHOR

SQL&gt;REM   carlos.sierra@[oracle](http://biancheng.dnbcw.info/oracle/).com

SQL&gt;REM

SQL&gt;REM SCRIPT

SQL&gt;REM   coe\_xfr\_sql\_profile\_8ma7qaqmmt5bn\_3958077978.sql

SQL&gt;REM

SQL&gt;REM DESCRIPTION

SQL&gt;REM   This script is generated by coe\_xfr\_sql\_profile.sql

SQL&gt;REM   It contains the SQL\*Plus commands to create a custom

SQL&gt;REM   SQL Profile for SQL\_ID 8ma7qaqmmt5bn based on plan hash

SQL&gt;REM   value 3958077978.

SQL&gt;REM   The custom SQL Profile to be created by this script

SQL&gt;REM   will affect plans for SQL commands with signature

SQL&gt;REM   matching the one for SQL Text below.

SQL&gt;REM   Review SQL Text and adjust accordingly.

SQL&gt;REM

SQL&gt;REM PARAMETERS

SQL&gt;REM   None.

SQL&gt;REM

SQL&gt;REM EXAMPLE

SQL&gt;REM   SQL&gt; START coe\_xfr\_sql\_profile\_8ma7qaqmmt5bn\_3958077978.sql;

SQL&gt;REM

SQL&gt;REM NOTES

SQL&gt;REM   1. Should be run as SYSTEM or SYSDBA.

SQL&gt;REM   2. User must have CREATE ANY SQL PROFILE privilege.

SQL&gt;REM   3. SOURCE and TARGET systems can be the same or similar.

SQL&gt;REM   4. To drop this custom SQL Profile after it has been created:

SQL&gt;REM  EXEC DBMS\_SQLTUNE.DROP\_SQL\_PROFILE('coe\_8ma7qaqmmt5bn\_3958077978');

SQL&gt;REM   5. Be aware that using DBMS\_SQLTUNE requires a license

SQL&gt;REM  for the Oracle Tuning Pack.

SQL&gt;REM   6. If you modified a SQL putting Hints in order to produce a desired

SQL&gt;REM  Plan, you can remove the artifical Hints from SQL Text pieces below.

SQL&gt;REM  By doing so you can create a custom SQL Profile for the original

SQL&gt;REM  SQL but with the Plan captured from the modified SQL (with Hints).

SQL&gt;REM

SQL&gt;WHENEVER SQLERROR EXIT SQL.SQLCODE;

SQL&gt;REM

SQL&gt;VAR signature NUMBER;

SQL&gt;VAR signaturef NUMBER;

SQL&gt;REM

SQL&gt;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(1) from test where object\_type='SYNONYM' ]');

 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('10.2.0.1')]',

 20  q'\[ALL\_ROWS]',

 21  q'\[OUTLINE\_LEAF(@"SEL$1")]',

 22  q'\[INDEX\_FFS(@"SEL$1" "TEST"@"SEL$1" ("TEST"."OBJECT\_TYPE"))]',

 23  q'\[END\_OUTLINE\_DATA]');

 24  :signature := DBMS\_SQLTUNE.SQLTEXT\_TO\_SIGNATURE(sql\_txt);

 25  :signaturef := DBMS\_SQLTUNE.SQLTEXT\_TO\_SIGNATURE(sql\_txt, TRUE);

 26  DBMS\_SQLTUNE.IMPORT\_SQL\_PROFILE (

 27  sql\_text    =&gt; sql\_txt,

 28  profile     =&gt; h,

 29  name        =&gt; 'coe\_8ma7qaqmmt5bn\_3958077978',

 30  description =&gt; 'coe 8ma7qaqmmt5bn 3958077978 '||:signature||' '||:signaturef||'',

 31  category    =&gt; 'DEFAULT',

 32  validate    =&gt; TRUE,

 33  replace     =&gt; TRUE,

 34  force\_match =&gt; FALSE /* TRUE:FORCE (match even when different literals in SQL). FALSE:EXACT (similar to CURSOR\_SHARING) \*/ );

 35  DBMS\_LOB.FREETEMPORARY(sql\_txt);

 36  END;

 37  /

 

PL/SQL 过程已成功完成。

 

SQL&gt;WHENEVER SQLERROR CONTINUE

SQL&gt;SET ECHO OFF;

 

            SIGNATURE

\---------------------

  5779641010256874563

 

 

           SIGNATUREF

\---------------------

  2423517717540587142

 

 

... manual custom SQL Profile has been created

 

 

COE\_XFR\_SQL\_PROFILE\_8ma7qaqmmt5bn\_3958077978 completed

 

 

SQL&gt;select name,sql\_text,status from dba\_sql\_profiles;

 

NAME                           SQL\_TEXT                                                                 STATUS

\------------------------------ -------------------------------------------------------------------------------- --------

coe\_8ma7qaqmmt5bn\_3958077978   select count(1) from test where object\_type='SYNONYM'                    ENABLED

 

SQL&gt;select sql\_text,sql\_profile from v$sql where sql\_id='8ma7qaqmmt5bn';

 

SQL\_TEXT                       SQL\_PROFILE

\------------------------------ ------------------------------

select count(1) from test wher

e object\_type='SYNONYM'

 

 

SQL&gt;comment on table test is '';--将当期的执行计划淘汰出去

 

注释已创建。

 

SQL&gt;select sql\_text,sql\_profile from v$sql where sql\_id='8ma7qaqmmt5bn';

 

未选定行

 

SQL&gt;set autot traceonly exp

SQL&gt;select count(1) from test where object\_type='SYNONYM';

 

执行计划

\----------------------------------------------------------

Plan hash value: 3958077978

 

\-------------------------------------------------------------------------------------

| Id  | Operation             | Name        | Rows  | Bytes | Cost (%CPU)| Time     |

\-------------------------------------------------------------------------------------

|   0 | SELECT STATEMENT      |             |     1 |     9 |    35   (3)| 00:00:01 |

|   1 |  SORT AGGREGATE       |             |     1 |     9 |            |          |

|*  2 |   INDEX FAST FULL SCAN| IDX\_CPIC\_01 | 19221 |   168K|    35   (3)| 00:00:01 |

\-------------------------------------------------------------------------------------

 

Predicate Information (identified by operation id):

\---------------------------------------------------

 

   2 - filter("OBJECT\_TYPE"='SYNONYM')

 

Note

\-----

   - SQL profile "coe\_8ma7qaqmmt5bn\_3958077978" used for this statement  &lt;-- 可以看到SQL PROFILE已经正常工作了。

 

SQL&gt;set autot off

SQL&gt;select sql\_text,sql\_profile from v$sql where sql\_id='8ma7qaqmmt5bn';

 

SQL\_TEXT                       SQL\_PROFILE

\------------------------------ ------------------------------

select count(1) from test wher coe\_8ma7qaqmmt5bn\_3958077978

e object\_type='SYNONYM'