原来语句执行计划
SELECT t1.*, t2.owner
FROM t1, t2
WHERE t1.object_name LIKE ‘%T1%‘ AND t1.object_id = t2.object_id;
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2500 | 100K| 361 (2)| 00:00:05 |
|* 1 | HASH JOIN | | 2500 | 100K| 361 (2)| 00:00:05 |
|* 2 | TABLE ACCESS FULL| T1 | 2500 | 75000 | 68 (2)| 00:00:01 |
| 3 | TABLE ACCESS FULL| T2 | 74790 | 803K| 292 (1)| 00:00:04 |
---------------------------------------------------------------------------
SQL Profiles 中对 Hints有一定的要求。需要经过加工处理
例如手动加入hints sys.sqlprof_attr (‘USE_NL([email protected]$1 [email protected]$1)‘, ‘INDEX([email protected]$1)‘);
DECLARE
v_hints SYS.sqlprof_attr;
BEGIN
v_hints :=
sys.sqlprof_attr (‘USE_NL([email protected]$1 [email protected]$1)‘, ‘INDEX([email protected]$1)‘);
DBMS_SQLTUNE.import_sql_profile (
‘select t1.*,t2.owner from t1,t2 where t1.object_name like ‘‘%T1%‘‘ and t1.object_id=t2.object_id‘,
v_hints,
‘SQLPROFILE_NAME1‘,
force_match => TRUE,
REPLACE => TRUE);
END;
/* 查看包含的hint */
SELECT attr_val
FROM dba_sql_profiles a, sys.sqlprof$attr b
WHERE a.signature = b.signature AND a.name = ‘SQLPROFILE_NAME1‘;
/* 执行计划已经改变*/
SELECT t1.*, t2.owner
FROM t1, t2
WHERE t1.object_name LIKE ‘%T1%‘ AND t1.object_id = t2.object_id;
---------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2500 | 100K| 5071 (1)| 00:01:01 |
| 1 | NESTED LOOPS | | | | | |
| 2 | NESTED LOOPS | | 2500 | 100K| 5071 (1)| 00:01:01 |
|* 3 | TABLE ACCESS FULL | T1 | 2500 | 75000 | 68 (2)| 00:00:01 |
|* 4 | INDEX RANGE SCAN | T2_IDX | 1 | | 1 (0)| 00:00:01 |
| 5 | TABLE ACCESS BY INDEX ROWID| T2 | 1 | 11 | 2 (0)| 00:00:01 |
---------------------------------------------------------------------------------------
那么我们再一次手工修改T1表的统计信息,看看结果如何: 查询计划不变
exec dbms_stats.set_table_stats(‘CQWR‘,‘T1‘,numrows=>5000000);
SELECT t1.*, t2.owner
FROM t1, t2
WHERE t1.object_name LIKE ‘%T1%‘ AND t1.object_id = t2.object_id;
---------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 250K| 9M| 500K (1)| 01:40:06 |
| 1 | NESTED LOOPS | | | | | |
| 2 | NESTED LOOPS | | 250K| 9M| 500K (1)| 01:40:06 |
|* 3 | TABLE ACCESS FULL | T1 | 250K| 7324K| 163 (59)| 00:00:02 |
|* 4 | INDEX RANGE SCAN | T2_IDX | 1 | | 1 (0)| 00:00:01 |
| 5 | TABLE ACCESS BY INDEX ROWID| T2 | 1 | 11 | 2 (0)| 00:00:01 |
---------------------------------------------------------------------------------------
---2对现有的SQL稳定其执行计划。
这里的问题是:稳定一条SQL语句的Hints从哪里来?
简单的sql,没问题,我们可以手工构造,但是复杂的SQL,手工构造相对比较复杂,
同时手工构造的Hints不一定能够保证SQL的执行计划就会稳定。
从10g开始,v$sql_plan中就包括了SQL语句OUTLINE数据,也就是稳定执行计划的Hints。
从下面可以看到:
SELECT t1.*, t2.owner
FROM t1, t2
WHERE t1.object_name LIKE ‘%T1%‘ AND t1.object_id = t2.object_id;
select * from table(dbms_xplan.display_cursor(null,null,‘outline‘));
显示的“Outline Data”即是我们稳定SQL执行计划需要的Hints
/*+
BEGIN_OUTLINE_DATA
IGNORE_OPTIM_EMBEDDED_HINTS
OPTIMIZER_FEATURES_ENABLE(‘11.2.0.3‘)
DB_VERSION(‘11.2.0.3‘)
ALL_ROWS
OUTLINE_LEAF(@"SEL$1")
FULL(@"SEL$1" "T1"@"SEL$1")
INDEX(@"SEL$1" "T2"@"SEL$1" ("T2"."OBJECT_ID"))
LEADING(@"SEL$1" "T1"@"SEL$1" "T2"@"SEL$1")
USE_NL(@"SEL$1" "T2"@"SEL$1")
NLJ_BATCHING(@"SEL$1" "T2"@"SEL$1")
END_OUTLINE_DATA
*/
对需要稳定执行计划的SQL,我们所要做的就是如前面所示,将Hints与SQL文本一起创建一个SQL Profile。
一个SQL脚本coe_xfr_sql_profile.sql
下面我们来演示一下整个过程。我们这里要修改执行计划的SQL是:
SELECT t1.*, t2.owner
FROM t1, t2
WHERE t1.object_name LIKE ‘%T1%‘ AND t1.object_id = t2.object_id;
-----------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
-----------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 250K| 9765K| | 851 (13)| 00:00:11 |
|* 1 | HASH JOIN | | 250K| 9765K| 1144K| 851 (13)| 00:00:11 |
| 2 | TABLE ACCESS FULL| T2 | 50605 | 543K| | 155 (1)| 00:00:02 |
|* 3 | TABLE ACCESS FULL| T1 | 250K| 7080K| | 150 (65)| 00:00:02 |
-----------------------------------------------------------------------------------
--记住SQL_ID 和 Plan hash value
SQL>select * from table(dbms_xplan.display_cursor(null,null,‘ADVANCED‘));
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID 1atjzcw6x51yp, child number 2
-------------------------------------
SELECT t1.*, t2.owner FROM t1, t2 WHERE t1.object_name LIKE ‘%T1%‘ AND
t1.object_id = t2.object_id
--我们希望构造的SQL如下
SELECT /*+ modify_sql index(t1) use_nl(t1 t2) */
t1.*, t2.owner
FROM t1, t2
WHERE t1.object_name LIKE ‘%T1%‘ AND t1.object_id = t2.object_id;
--------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 250K| 9765K| 500K (1)| 01:40:06 |
| 1 | TABLE ACCESS BY INDEX ROWID| T2 | 1 | 11 | 2 (0)| 00:00:01 |
| 2 | NESTED LOOPS | | 250K| 9765K| 500K (1)| 01:40:06 |
|* 3 | TABLE ACCESS FULL | T1 | 250K| 7080K| 150 (65)| 00:00:02 |
|* 4 | INDEX RANGE SCAN | T2_IDX | 1 | | 1 (0)| 00:00:01 |
--------------------------------------------------------------------------------------
--记住SQL_ID 和 Plan hash value
SQL> select * from table(dbms_xplan.display_cursor(null,null,‘ADVANCED‘));
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID 2ravw02cfdjsg, child number 0
-------------------------------------
SELECT /*+ modify_sql index(t1) use_nl(t1 t2) */ t1.*, t2.owner FROM
t1, t2 WHERE t1.object_name LIKE ‘%T1%‘ AND t1.object_id = t2.object_id
Plan hash value: 3787413387
--分别使用coe_xfr_sql_profile.sql 创建两条语句的SQL_profile
[[email protected] utl]$ cd /home/oracle/sqlt/utl
[[email protected] utl]$ ls
10053.sql coe_xfr_sql_profile_1jmk1d81apm35_3787413387.log flush.sql sel_aux.sql sqlthistpurge.sql sqltmain.sql xplore
coe_gen_sql_profile.sql coe_xfr_sql_profile_1jmk1d81apm35_3787413387.sql plan.sql sel.sql sqltimpfo.sql sqltprofile.sql
coe_load_sql_baseline.sql coe_xfr_sql_profile.log profiler.sql sqltcdirs.sql sqltimp.sql xgram
coe_load_sql_profile.sql coe_xfr_sql_profile.sql pxhcdr.sql sqlthistfile.sql sqltlite.sql xhume
[[email protected] utl]$
[[email protected] utl]$ sqlplus " /as sysdba"
SQL> conn cqwr/cqwr
Connected.
SQL>@coe_xfr_sql_profile.sql
Parameter 1:
SQL_ID (required)
Enter value for 1: 1atjzcw6x51yp
PLAN_HASH_VALUE AVG_ET_SECS
--------------- -----------
2959412835 .138
替换原有语句的SQL文件中内容
将里面h := SYS.SQLPROF_ATTR…
替换成我们想要得到的SQL语句中的那一段。
将这个文件中的force_match从FALSE改成TRUE。
q‘[BEGIN_OUTLINE_DATA]‘,
q‘[IGNORE_OPTIM_EMBEDDED_HINTS]‘,
q‘[OPTIMIZER_FEATURES_ENABLE(‘10.2.0.4‘)]‘,
q‘[ALL_ROWS]‘,
q‘[OUTLINE_LEAF(@"SEL$1")]‘,
q‘[FULL(@"SEL$1" "T1"@"SEL$1")]‘,
q‘[INDEX(@"SEL$1" "T2"@"SEL$1" ("T2"."OBJECT_ID"))]‘,
q‘[LEADING(@"SEL$1" "T1"@"SEL$1" "T2"@"SEL$1")]‘,
q‘[USE_NL(@"SEL$1" "T2"@"SEL$1")]‘,
q‘[END_OUTLINE_DATA]‘);
而后执行原来语句生成的SQL语句
SQL>@coe_xfr_sql_profile_1atjzcw6x51yp_2959412835.sql
---再次执行原始SQL语句我们发现执行计划已经改变
SELECT t1.*, t2.owner
FROM t1, t2
WHERE t1.object_name LIKE ‘%T1%‘ AND t1.object_id = t2.object_id
Plan hash value: 3787413387
--------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 250K| 9765K| 500K (1)| 01:40:06 |
| 1 | TABLE ACCESS BY INDEX ROWID| T2 | 1 | 11 | 2 (0)| 00:00:01 |
| 2 | NESTED LOOPS | | 250K| 9765K| 500K (1)| 01:40:06 |
|* 3 | TABLE ACCESS FULL | T1 | 250K| 7080K| 150 (65)| 00:00:02 |
|* 4 | INDEX RANGE SCAN | T2_IDX | 1 | | 1 (0)| 00:00:01 |
--------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - filter("T1"."OBJECT_NAME" LIKE ‘%T1%‘)
4 - access("T1"."OBJECT_ID"="T2"."OBJECT_ID")
Note
-----
- SQL profile "coe_1atjzcw6x51yp_2959412835" used for this statement
---任务2对现有的SQL稳定其执行计划。比如原有语句已经有hint了
解决办法和上面一样 生成原有语句的HINT 以及需要构造语句的HITN语句
替换其中内容就可以了
select /*+ orig_sql full(t1) full(t2) use_hash(t1 t2) */ t1.*,t2.owner
from t1,t2
where t1.object_name like ‘%T1%‘
and t1.object_id=t2.object_id;
记住 SQL_ID ft158sw5s22nu
SQL>select * from table(dbms_xplan.display_cursor(null,null,‘ADVANCED‘));
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID ft158sw5s22nu, child number 0
-------------------------------------
select /*+ orig_sql full(t1) full(t2) use_hash(t1 t2) */ t1.*,t2.owner
from t1,t2 where t1.object_name like ‘%T1%‘ and
t1.object_id=t2.object_id
Plan hash value: 2959412835
SELECT /*+ modify_sql index(t1) use_nl(t1 t2) */
t1.*, t2.owner FROM t1, t2
WHERE t1.object_name LIKE ‘%T1%‘ AND t1.object_id = t2.object_id
记住 SQL_ID 2ravw02cfdjsg
SQL>select * from table(dbms_xplan.display_cursor(null,null,‘ADVANCED‘));
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID 2ravw02cfdjsg, child number 0
-------------------------------------
SELECT /*+ modify_sql index(t1) use_nl(t1 t2) */ t1.*, t2.owner FROM
t1, t2 WHERE t1.object_name LIKE ‘%T1%‘ AND t1.object_id = t2.object_id
Plan hash value: 3787413387
@coe_xfr_sql_profile.sql
原来语句对应生成的SQL
coe_xfr_sql_profile_ft158sw5s22nu_2959412835.sql
想构造的语句生成的SQL
coe_xfr_sql_profile_2ravw02cfdjsg_3787413387.sql
对应的 hint
q‘[BEGIN_OUTLINE_DATA]‘,
q‘[IGNORE_OPTIM_EMBEDDED_HINTS]‘,
q‘[OPTIMIZER_FEATURES_ENABLE(‘10.2.0.4‘)]‘,
q‘[ALL_ROWS]‘,
q‘[OUTLINE_LEAF(@"SEL$1")]‘,
q‘[FULL(@"SEL$1" "T1"@"SEL$1")]‘,
q‘[INDEX(@"SEL$1" "T2"@"SEL$1" ("T2"."OBJECT_ID"))]‘,
q‘[LEADING(@"SEL$1" "T1"@"SEL$1" "T2"@"SEL$1")]‘,
q‘[USE_NL(@"SEL$1" "T2"@"SEL$1")]‘,
q‘[END_OUTLINE_DATA]‘);
替换原有语句的SQL文件中内容
将里面h := SYS.SQLPROF_ATTR…
替换成我们想要得到的SQL语句中的那一段。
将这个文件中的force_match从FALSE改成TRUE。
而后执行原来语句生成的SQL语句
SQL>@coe_xfr_sql_profile_1atjzcw6x51yp_2959412835.sql
select /*+ orig_sql full(t1) full(t2) use_hash(t1 t2) */ t1.*,t2.owner
from t1,t2
where t1.object_name like ‘%T1%‘
and t1.object_id=t2.object_id;
Plan hash value: 3787413387
--------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 250K| 9765K| 500K (1)| 01:40:06 |
| 1 | TABLE ACCESS BY INDEX ROWID| T2 | 1 | 11 | 2 (0)| 00:00:01 |
| 2 | NESTED LOOPS | | 250K| 9765K| 500K (1)| 01:40:06 |
|* 3 | TABLE ACCESS FULL | T1 | 250K| 7080K| 150 (65)| 00:00:02 |
|* 4 | INDEX RANGE SCAN | T2_IDX | 1 | | 1 (0)| 00:00:01 |
--------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - filter("T1"."OBJECT_NAME" LIKE ‘%T1%‘)
4 - access("T1"."OBJECT_ID"="T2"."OBJECT_ID")
Note
-----
- SQL profile "coe_ft158sw5s22nu_2959412835" used for this statement
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
coe_xfr_sql_profile.sql
SPO coe_xfr_sql_profile.log;
SET DEF ON TERM OFF ECHO ON FEED OFF VER OFF HEA ON LIN 2000 PAGES 100 LONG 8000000 LONGC 800000 TRIMS ON TI OFF TIMI OFF SERVEROUT ON SIZE 1000000 NUMF "" SQLP SQL>;
SET SERVEROUT ON SIZE UNL;
REM
REM $Header: 215187.1 coe_xfr_sql_profile.sql 11.4.4.5 2012/05/02 carlos.sierra $
REM
REM Copyright (c) 2000-2012, Oracle Corporation. All rights reserved.
REM
REM AUTHOR
REM [email protected]
REM
REM SCRIPT
REM coe_xfr_sql_profile.sql
REM
REM DESCRIPTION
REM This script generates another that contains the commands to
REM create a manual custom SQL Profile out of a known plan from
REM memory or AWR. The manual custom profile can be implemented
REM into the same SOURCE system where the plan was retrieved,
REM or into another similar TARGET system that has same schema
REM objects referenced by the SQL that generated the known plan.
REM
REM PRE-REQUISITES
REM 1. Oracle Tuning Pack license.
REM
REM PARAMETERS
REM 1. SQL_ID (required)
REM 2. Plan Hash Value for which a manual custom SQL Profile is
REM needed (required). A list of known plans is presented.
REM You may choose from list provided or enter a valid phv
REM from a version of the SQL modified with Hints.
REM
REM EXECUTION
REM 1. Connect into SQL*Plus as user with access to data dictionary.
REM 2. Execute script coe_xfr_sql_profile.sql passing SQL_ID and
REM plan hash value (parameters can be passed inline or until
REM requested).
REM
REM EXAMPLE
REM # sqlplus system
REM SQL> START coe_xfr_sql_profile.sql [SQL_ID] [PLAN_HASH_VALUE];
REM SQL> START coe_xfr_sql_profile.sql gnjy0mn4y9pbm 2055843663;
REM SQL> START coe_xfr_sql_profile.sql gnjy0mn4y9pbm;
REM SQL> START coe_xfr_sql_profile.sql;
REM
REM NOTES
REM 1. For possible errors see coe_xfr_sql_profile.log
REM 2. If SQLT is installed in SOURCE, you can use instead:
REM sqlt/utl/sqltprofile.sql
REM 3. Be aware that using DBMS_SQLTUNE requires a license for
REM Oracle Tuning Pack.
REM
SET TERM ON ECHO OFF;
PRO
PRO Parameter 1:
PRO SQL_ID (required)
PRO
DEF sql_id = ‘&1‘;
PRO
WITH
p AS (
SELECT plan_hash_value
FROM gv$sql_plan
WHERE sql_id = TRIM(‘&&sql_id.‘)
AND other_xml IS NOT NULL
UNION
SELECT plan_hash_value
FROM dba_hist_sql_plan
WHERE sql_id = TRIM(‘&&sql_id.‘)
AND other_xml IS NOT NULL ),
m AS (
SELECT plan_hash_value,
SUM(elapsed_time)/SUM(executions) avg_et_secs
FROM gv$sql
WHERE sql_id = TRIM(‘&&sql_id.‘)
AND executions > 0
GROUP BY
plan_hash_value ),
a AS (
SELECT plan_hash_value,
SUM(elapsed_time_total)/SUM(executions_total) avg_et_secs
FROM dba_hist_sqlstat
WHERE sql_id = TRIM(‘&&sql_id.‘)
AND executions_total > 0
GROUP BY
plan_hash_value )
SELECT p.plan_hash_value,
ROUND(NVL(m.avg_et_secs, a.avg_et_secs)/1e6, 3) avg_et_secs
FROM p, m, a
WHERE p.plan_hash_value = m.plan_hash_value(+)
AND p.plan_hash_value = a.plan_hash_value(+)
ORDER BY
avg_et_secs NULLS LAST;
PRO
PRO Parameter 2:
PRO PLAN_HASH_VALUE (required)
PRO
DEF plan_hash_value = ‘&2‘;
PRO
PRO Values passed to coe_xfr_sql_profile:
PRO ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PRO SQL_ID : "&&sql_id."
PRO PLAN_HASH_VALUE: "&&plan_hash_value."
PRO
SET TERM OFF ECHO ON;
WHENEVER SQLERROR EXIT SQL.SQLCODE;
-- trim parameters
COL sql_id NEW_V sql_id FOR A30;
COL plan_hash_value NEW_V plan_hash_value FOR A30;
SELECT TRIM(‘&&sql_id.‘) sql_id, TRIM(‘&&plan_hash_value.‘) plan_hash_value FROM DUAL;
VAR sql_text CLOB;
VAR sql_text2 CLOB;
VAR other_xml CLOB;
EXEC :sql_text := NULL;
EXEC :sql_text2 := NULL;
EXEC :other_xml := NULL;
-- get sql_text from memory
DECLARE
l_sql_text VARCHAR2(32767);
BEGIN -- 10g see bug 5017909
FOR i IN (SELECT DISTINCT piece, sql_text
FROM gv$sqltext_with_newlines
WHERE sql_id = TRIM(‘&&sql_id.‘)
ORDER BY 1, 2)
LOOP
IF :sql_text IS NULL THEN
DBMS_LOB.CREATETEMPORARY(:sql_text, TRUE);
DBMS_LOB.OPEN(:sql_text, DBMS_LOB.LOB_READWRITE);
END IF;
-- removes NUL characters
l_sql_text := REPLACE(i.sql_text, CHR(00), ‘ ‘);
-- adds a NUL character at the end of each line
DBMS_LOB.WRITEAPPEND(:sql_text, LENGTH(l_sql_text) + 1, l_sql_text||CHR(00));
END LOOP;
-- if found in memory then sql_text is not null
IF :sql_text IS NOT NULL THEN
DBMS_LOB.CLOSE(:sql_text);
END IF;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(‘getting sql_text from memory: ‘||SQLERRM);
:sql_text := NULL;
END;
/
SELECT :sql_text FROM DUAL;
-- get sql_text from awr
DECLARE
l_sql_text VARCHAR2(32767);
l_clob_size NUMBER;
l_offset NUMBER;
BEGIN
IF :sql_text IS NULL OR NVL(DBMS_LOB.GETLENGTH(:sql_text), 0) = 0 THEN
SELECT sql_text
INTO :sql_text2
FROM dba_hist_sqltext
WHERE sql_id = TRIM(‘&&sql_id.‘)
AND sql_text IS NOT NULL
AND ROWNUM = 1;
END IF;
-- if found in awr then sql_text2 is not null
IF :sql_text2 IS NOT NULL THEN
l_clob_size := NVL(DBMS_LOB.GETLENGTH(:sql_text2), 0);
l_offset := 1;
DBMS_LOB.CREATETEMPORARY(:sql_text, TRUE);
DBMS_LOB.OPEN(:sql_text, DBMS_LOB.LOB_READWRITE);
-- store in clob as 64 character pieces plus a NUL character at the end of each piece
WHILE l_offset < l_clob_size
LOOP
IF l_clob_size - l_offset > 64 THEN
l_sql_text := REPLACE(DBMS_LOB.SUBSTR(:sql_text2, 64, l_offset), CHR(00), ‘ ‘);
ELSE -- last piece
l_sql_text := REPLACE(DBMS_LOB.SUBSTR(:sql_text2, l_clob_size - l_offset + 1, l_offset), CHR(00), ‘ ‘);
END IF;
DBMS_LOB.WRITEAPPEND(:sql_text, LENGTH(l_sql_text) + 1, l_sql_text||CHR(00));
l_offset := l_offset + 64;
END LOOP;
DBMS_LOB.CLOSE(:sql_text);
END IF;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(‘getting sql_text from awr: ‘||SQLERRM);
:sql_text := NULL;
END;
/
SELECT :sql_text2 FROM DUAL;
SELECT :sql_text FROM DUAL;
-- validate sql_text
SET TERM ON;
BEGIN
IF :sql_text IS NULL THEN
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).‘);
END IF;
END;
/
SET TERM OFF;
-- get other_xml from memory
BEGIN
FOR i IN (SELECT other_xml
FROM gv$sql_plan
WHERE sql_id = TRIM(‘&&sql_id.‘)
AND plan_hash_value = TO_NUMBER(TRIM(‘&&plan_hash_value.‘))
AND other_xml IS NOT NULL
ORDER BY
child_number, id)
LOOP
:other_xml := i.other_xml;
EXIT; -- 1st
END LOOP;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(‘getting other_xml from memory: ‘||SQLERRM);
:other_xml := NULL;
END;
/
-- get other_xml from awr
BEGIN
IF :other_xml IS NULL OR NVL(DBMS_LOB.GETLENGTH(:other_xml), 0) = 0 THEN
FOR i IN (SELECT other_xml
FROM dba_hist_sql_plan
WHERE sql_id = TRIM(‘&&sql_id.‘)
AND plan_hash_value = TO_NUMBER(TRIM(‘&&plan_hash_value.‘))
AND other_xml IS NOT NULL
ORDER BY
id)
LOOP
:other_xml := i.other_xml;
EXIT; -- 1st
END LOOP;
END IF;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(‘getting other_xml from awr: ‘||SQLERRM);
:other_xml := NULL;
END;
/
-- get other_xml from memory from modified SQL
BEGIN
IF :other_xml IS NULL OR NVL(DBMS_LOB.GETLENGTH(:other_xml), 0) = 0 THEN
FOR i IN (SELECT other_xml
FROM gv$sql_plan
WHERE plan_hash_value = TO_NUMBER(TRIM(‘&&plan_hash_value.‘))
AND other_xml IS NOT NULL
ORDER BY
child_number, id)
LOOP
:other_xml := i.other_xml;
EXIT; -- 1st
END LOOP;
END IF;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(‘getting other_xml from memory: ‘||SQLERRM);
:other_xml := NULL;
END;
/
-- get other_xml from awr from modified SQL
BEGIN
IF :other_xml IS NULL OR NVL(DBMS_LOB.GETLENGTH(:other_xml), 0) = 0 THEN
FOR i IN (SELECT other_xml
FROM dba_hist_sql_plan
WHERE plan_hash_value = TO_NUMBER(TRIM(‘&&plan_hash_value.‘))
AND other_xml IS NOT NULL
ORDER BY
id)
LOOP
:other_xml := i.other_xml;
EXIT; -- 1st
END LOOP;
END IF;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(‘getting other_xml from awr: ‘||SQLERRM);
:other_xml := NULL;
END;
/
SELECT :other_xml FROM DUAL;
-- validate other_xml
SET TERM ON;
BEGIN
IF :other_xml IS NULL THEN
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).‘);
END IF;
END;
/
SET TERM OFF;
-- generates script that creates sql profile in target system:
SET ECHO OFF;
PRO coe_xfr_sql_profile_&&sql_id._&&plan_hash_value..sql.
SET FEED OFF LIN 666 TRIMS ON TI OFF TIMI OFF SERVEROUT ON SIZE 1000000 FOR WOR;
SET SERVEROUT ON SIZE UNL FOR WOR;
SPO OFF;
SPO coe_xfr_sql_profile_&&sql_id._&&plan_hash_value..sql;
DECLARE
l_pos NUMBER;
l_clob_size NUMBER;
l_offset NUMBER;
l_sql_text VARCHAR2(32767);
l_len NUMBER;
l_hint VARCHAR2(32767);
BEGIN
DBMS_OUTPUT.PUT_LINE(‘SPO coe_xfr_sql_profile_&&sql_id._&&plan_hash_value..log;‘);
DBMS_OUTPUT.PUT_LINE(‘SET ECHO ON TERM ON LIN 2000 TRIMS ON NUMF 99999999999999999999;‘);
DBMS_OUTPUT.PUT_LINE(‘REM‘);
DBMS_OUTPUT.PUT_LINE(‘REM $Header: 215187.1 coe_xfr_sql_profile_&&sql_id._&&plan_hash_value..sql 11.4.4.4 ‘||TO_CHAR(SYSDATE, ‘YYYY/MM/DD‘)||‘ carlos.sierra $‘);
DBMS_OUTPUT.PUT_LINE(‘REM‘);
DBMS_OUTPUT.PUT_LINE(‘REM Copyright (c) 2000-2012, Oracle Corporation. All rights reserved.‘);
DBMS_OUTPUT.PUT_LINE(‘REM‘);
DBMS_OUTPUT.PUT_LINE(‘REM AUTHOR‘);
DBMS_OUTPUT.PUT_LINE(‘REM [email protected]‘);
DBMS_OUTPUT.PUT_LINE(‘REM‘);
DBMS_OUTPUT.PUT_LINE(‘REM SCRIPT‘);
DBMS_OUTPUT.PUT_LINE(‘REM coe_xfr_sql_profile_&&sql_id._&&plan_hash_value..sql‘);
DBMS_OUTPUT.PUT_LINE(‘REM‘);
DBMS_OUTPUT.PUT_LINE(‘REM DESCRIPTION‘);
DBMS_OUTPUT.PUT_LINE(‘REM This script is generated by coe_xfr_sql_profile.sql‘);
DBMS_OUTPUT.PUT_LINE(‘REM It contains the SQL*Plus commands to create a custom‘);
DBMS_OUTPUT.PUT_LINE(‘REM SQL Profile for SQL_ID &&sql_id. based on plan hash‘);
DBMS_OUTPUT.PUT_LINE(‘REM value &&plan_hash_value..‘);
DBMS_OUTPUT.PUT_LINE(‘REM The custom SQL Profile to be created by this script‘);
DBMS_OUTPUT.PUT_LINE(‘REM will affect plans for SQL commands with signature‘);
DBMS_OUTPUT.PUT_LINE(‘REM matching the one for SQL Text below.‘);
DBMS_OUTPUT.PUT_LINE(‘REM Review SQL Text and adjust accordingly.‘);
DBMS_OUTPUT.PUT_LINE(‘REM‘);
DBMS_OUTPUT.PUT_LINE(‘REM PARAMETERS‘);
DBMS_OUTPUT.PUT_LINE(‘REM None.‘);
DBMS_OUTPUT.PUT_LINE(‘REM‘);
DBMS_OUTPUT.PUT_LINE(‘REM EXAMPLE‘);
DBMS_OUTPUT.PUT_LINE(‘REM SQL> START coe_xfr_sql_profile_&&sql_id._&&plan_hash_value..sql;‘);
DBMS_OUTPUT.PUT_LINE(‘REM‘);
DBMS_OUTPUT.PUT_LINE(‘REM NOTES‘);
DBMS_OUTPUT.PUT_LINE(‘REM 1. Should be run as SYSTEM or SYSDBA.‘);
DBMS_OUTPUT.PUT_LINE(‘REM 2. User must have CREATE ANY SQL PROFILE privilege.‘);
DBMS_OUTPUT.PUT_LINE(‘REM 3. SOURCE and TARGET systems can be the same or similar.‘);
DBMS_OUTPUT.PUT_LINE(‘REM 4. To drop this custom SQL Profile after it has been created:‘);
DBMS_OUTPUT.PUT_LINE(‘REM EXEC DBMS_SQLTUNE.DROP_SQL_PROFILE(‘‘coe_&&sql_id._&&plan_hash_value.‘‘);‘);
DBMS_OUTPUT.PUT_LINE(‘REM 5. Be aware that using DBMS_SQLTUNE requires a license‘);
DBMS_OUTPUT.PUT_LINE(‘REM for the Oracle Tuning Pack.‘);
DBMS_OUTPUT.PUT_LINE(‘REM 6. If you modified a SQL putting Hints in order to produce a desired‘);
DBMS_OUTPUT.PUT_LINE(‘REM Plan, you can remove the artifical Hints from SQL Text pieces below.‘);
DBMS_OUTPUT.PUT_LINE(‘REM By doing so you can create a custom SQL Profile for the original‘);
DBMS_OUTPUT.PUT_LINE(‘REM SQL but with the Plan captured from the modified SQL (with Hints).‘);
DBMS_OUTPUT.PUT_LINE(‘REM‘);
DBMS_OUTPUT.PUT_LINE(‘WHENEVER SQLERROR EXIT SQL.SQLCODE;‘);
DBMS_OUTPUT.PUT_LINE(‘REM‘);
DBMS_OUTPUT.PUT_LINE(‘VAR signature NUMBER;‘);
DBMS_OUTPUT.PUT_LINE(‘VAR signaturef NUMBER;‘);
DBMS_OUTPUT.PUT_LINE(‘REM‘);
DBMS_OUTPUT.PUT_LINE(‘DECLARE‘);
DBMS_OUTPUT.PUT_LINE(‘sql_txt CLOB;‘);
DBMS_OUTPUT.PUT_LINE(‘h SYS.SQLPROF_ATTR;‘);
DBMS_OUTPUT.PUT_LINE(‘PROCEDURE wa (p_line IN VARCHAR2) IS‘);
DBMS_OUTPUT.PUT_LINE(‘BEGIN‘);
DBMS_OUTPUT.PUT_LINE(‘DBMS_LOB.WRITEAPPEND(sql_txt, LENGTH(p_line), p_line);‘);
DBMS_OUTPUT.PUT_LINE(‘END wa;‘);
DBMS_OUTPUT.PUT_LINE(‘BEGIN‘);
DBMS_OUTPUT.PUT_LINE(‘DBMS_LOB.CREATETEMPORARY(sql_txt, TRUE);‘);
DBMS_OUTPUT.PUT_LINE(‘DBMS_LOB.OPEN(sql_txt, DBMS_LOB.LOB_READWRITE);‘);
DBMS_OUTPUT.PUT_LINE(‘-- SQL Text pieces below do not have to be of same length.‘);
DBMS_OUTPUT.PUT_LINE(‘-- So if you edit SQL Text (i.e. removing temporary Hints),‘);
DBMS_OUTPUT.PUT_LINE(‘-- there is no need to edit or re-align unmodified pieces.‘);
l_clob_size := NVL(DBMS_LOB.GETLENGTH(:sql_text), 0);
l_offset := 1;
WHILE l_offset < l_clob_size
LOOP
l_pos := DBMS_LOB.INSTR(:sql_text, CHR(00), l_offset);
IF l_pos > 0 THEN
l_len := l_pos - l_offset;
ELSE -- last piece
l_len := l_clob_size - l_pos + 1;
END IF;
l_sql_text := DBMS_LOB.SUBSTR(:sql_text, l_len, l_offset);
/* cannot do such 3 replacement since a line could end with a comment using "--"
l_sql_text := REPLACE(l_sql_text, CHR(10), ‘ ‘); -- replace LF with SP
l_sql_text := REPLACE(l_sql_text, CHR(13), ‘ ‘); -- replace CR with SP
l_sql_text := REPLACE(l_sql_text, CHR(09), ‘ ‘); -- replace TAB with SP
*/
l_offset := l_offset + l_len + 1;
IF l_len > 0 THEN
IF INSTR(l_sql_text, ‘‘‘[‘) + INSTR(l_sql_text, ‘]‘‘‘) = 0 THEN
l_sql_text := ‘[‘||l_sql_text||‘]‘;
ELSIF INSTR(l_sql_text, ‘‘‘{‘) + INSTR(l_sql_text, ‘}‘‘‘) = 0 THEN
l_sql_text := ‘{‘||l_sql_text||‘}‘;
ELSIF INSTR(l_sql_text, ‘‘‘<‘) + INSTR(l_sql_text, ‘>‘‘‘) = 0 THEN
l_sql_text := ‘<‘||l_sql_text||‘>‘;
ELSIF INSTR(l_sql_text, ‘‘‘(‘) + INSTR(l_sql_text, ‘)‘‘‘) = 0 THEN
l_sql_text := ‘(‘||l_sql_text||‘)‘;
ELSIF INSTR(l_sql_text, ‘‘‘"‘) + INSTR(l_sql_text, ‘"‘‘‘) = 0 THEN
l_sql_text := ‘"‘||l_sql_text||‘"‘;
ELSIF INSTR(l_sql_text, ‘‘‘|‘) + INSTR(l_sql_text, ‘|‘‘‘) = 0 THEN
l_sql_text := ‘|‘||l_sql_text||‘|‘;
ELSIF INSTR(l_sql_text, ‘‘‘~‘) + INSTR(l_sql_text, ‘~‘‘‘) = 0 THEN
l_sql_text := ‘~‘||l_sql_text||‘~‘;
ELSIF INSTR(l_sql_text, ‘‘‘^‘) + INSTR(l_sql_text, ‘^‘‘‘) = 0 THEN
l_sql_text := ‘^‘||l_sql_text||‘^‘;
ELSIF INSTR(l_sql_text, ‘‘‘@‘) + INSTR(l_sql_text, ‘@‘‘‘) = 0 THEN
l_sql_text := ‘@‘||l_sql_text||‘@‘;
ELSIF INSTR(l_sql_text, ‘‘‘#‘) + INSTR(l_sql_text, ‘#‘‘‘) = 0 THEN
l_sql_text := ‘#‘||l_sql_text||‘#‘;
ELSIF INSTR(l_sql_text, ‘‘‘%‘) + INSTR(l_sql_text, ‘%‘‘‘) = 0 THEN
l_sql_text := ‘%‘||l_sql_text||‘%‘;
ELSIF INSTR(l_sql_text, ‘‘‘$‘) + INSTR(l_sql_text, ‘$‘‘‘) = 0 THEN
l_sql_text := ‘$‘||l_sql_text||‘$‘;
ELSE
l_sql_text := CHR(96)||l_sql_text||CHR(96);
END IF;
DBMS_OUTPUT.PUT_LINE(‘wa(q‘‘‘||l_sql_text||‘‘‘);‘);
END IF;
END LOOP;
DBMS_OUTPUT.PUT_LINE(‘DBMS_LOB.CLOSE(sql_txt);‘);
DBMS_OUTPUT.PUT_LINE(‘h := SYS.SQLPROF_ATTR(‘);
DBMS_OUTPUT.PUT_LINE(‘q‘‘[BEGIN_OUTLINE_DATA]‘‘,‘);
FOR i IN (SELECT /*+ opt_param(‘parallel_execution_enabled‘, ‘false‘) */
SUBSTR(EXTRACTVALUE(VALUE(d), ‘/hint‘), 1, 4000) hint
FROM TABLE(XMLSEQUENCE(EXTRACT(XMLTYPE(:other_xml), ‘/*/outline_data/hint‘))) d)
LOOP
l_hint := i.hint;
WHILE NVL(LENGTH(l_hint), 0) > 0
LOOP
IF LENGTH(l_hint) <= 500 THEN
DBMS_OUTPUT.PUT_LINE(‘q‘‘[‘||l_hint||‘]‘‘,‘);
l_hint := NULL;
ELSE
l_pos := INSTR(SUBSTR(l_hint, 1, 500), ‘ ‘, -1);
DBMS_OUTPUT.PUT_LINE(‘q‘‘[‘||SUBSTR(l_hint, 1, l_pos)||‘]‘‘,‘);
l_hint := ‘ ‘||SUBSTR(l_hint, l_pos);
END IF;
END LOOP;
END LOOP;
DBMS_OUTPUT.PUT_LINE(‘q‘‘[END_OUTLINE_DATA]‘‘);‘);
DBMS_OUTPUT.PUT_LINE(‘:signature := DBMS_SQLTUNE.SQLTEXT_TO_SIGNATURE(sql_txt);‘);
DBMS_OUTPUT.PUT_LINE(‘:signaturef := DBMS_SQLTUNE.SQLTEXT_TO_SIGNATURE(sql_txt, TRUE);‘);
DBMS_OUTPUT.PUT_LINE(‘DBMS_SQLTUNE.IMPORT_SQL_PROFILE (‘);
DBMS_OUTPUT.PUT_LINE(‘sql_text => sql_txt,‘);
DBMS_OUTPUT.PUT_LINE(‘profile => h,‘);
DBMS_OUTPUT.PUT_LINE(‘name => ‘‘coe_&&sql_id._&&plan_hash_value.‘‘,‘);
DBMS_OUTPUT.PUT_LINE(‘description => ‘‘coe &&sql_id. &&plan_hash_value. ‘‘||:signature||‘‘ ‘‘||:signaturef||‘‘‘‘,‘);
DBMS_OUTPUT.PUT_LINE(‘category => ‘‘DEFAULT‘‘,‘);
DBMS_OUTPUT.PUT_LINE(‘validate => TRUE,‘);
DBMS_OUTPUT.PUT_LINE(‘replace => TRUE,‘);
DBMS_OUTPUT.PUT_LINE(‘force_match => FALSE /* TRUE:FORCE (match even when different literals in SQL). FALSE:EXACT (similar to CURSOR_SHARING) */ );‘);
DBMS_OUTPUT.PUT_LINE(‘DBMS_LOB.FREETEMPORARY(sql_txt);‘);
DBMS_OUTPUT.PUT_LINE(‘END;‘);
DBMS_OUTPUT.PUT_LINE(‘/‘);
DBMS_OUTPUT.PUT_LINE(‘WHENEVER SQLERROR CONTINUE‘);
DBMS_OUTPUT.PUT_LINE(‘SET ECHO OFF;‘);
DBMS_OUTPUT.PUT_LINE(‘PRINT signature‘);
DBMS_OUTPUT.PUT_LINE(‘PRINT signaturef‘);
DBMS_OUTPUT.PUT_LINE(‘PRO‘);
DBMS_OUTPUT.PUT_LINE(‘PRO ... manual custom SQL Profile has been created‘);
DBMS_OUTPUT.PUT_LINE(‘PRO‘);
DBMS_OUTPUT.PUT_LINE(‘SET TERM ON ECHO OFF LIN 80 TRIMS OFF NUMF "";‘);
DBMS_OUTPUT.PUT_LINE(‘SPO OFF;‘);
DBMS_OUTPUT.PUT_LINE(‘PRO‘);
DBMS_OUTPUT.PUT_LINE(‘PRO COE_XFR_SQL_PROFILE_&&sql_id._&&plan_hash_value. completed‘);
END;
/
SPO OFF;
SET DEF ON TERM ON ECHO OFF FEED 6 VER ON HEA ON LIN 80 PAGES 14 LONG 80 LONGC 80 TRIMS OFF TI OFF TIMI OFF SERVEROUT OFF NUMF "" SQLP SQL>;
SET SERVEROUT OFF;
PRO
PRO Execute coe_xfr_sql_profile_&&sql_id._&&plan_hash_value..sql
PRO on TARGET system in order to create a custom SQL Profile
PRO with plan &&plan_hash_value linked to adjusted sql_text.
PRO
UNDEFINE 1 2 sql_id plan_hash_value
CL COL
PRO
PRO COE_XFR_SQL_PROFILE completed.