本文主要是介绍Oracle执行计划的稳定(使用MANUAL类型的SQL PROFILE),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
使用MANUAL类型的SQL PROFILE来稳定执行计划
手动类型的SQL PROFILE总的来说就修改HINT,就是将COST较少的HINT部分复制到COST较多的里面去。就是执行计划中的Outline Data部分。
1、环境准备
SCOTT@TNS_PDB01>create table t1(n number);Table created.SCOTT@TNS_PDB01>ed
Wrote file afiedt.buf1 declare2 begin3 for i in 1 .. 10000 loop4 insert into t1 values(i);5 end loop;6 commit;7* end;8 /PL/SQL procedure successfully completed.SCOTT@TNS_PDB01>select count(*) from t1;COUNT(*)
----------10000SCOTT@TNS_PDB01>create index idx_t1 on t1(n);Index created.SCOTT@TNS_PDB01>exec dbms_stats.gather_table_stats(ownname => 'SCOTT',tabname => 'T1',estimate_percent => 100,cascade => true,no_invalidate => false);PL/SQL procedure successfully completed.
2、分别使用NO_INDEX和INDEX执行下列语句生成不同的执行计划
SCOTT@TNS_PDB01>select /*+ no_index(t1 idx_t1) */* from t1 where n = 5;N
----------5SCOTT@TNS_PDB01>select /*+ index(t1 idx_t1) */* from t1 where n = 5;N
----------5SCOTT@TNS_PDB01>select sql_text,sql_id,plan_hash_value,version_count,executions from v$sqlarea where sql_text like '%from t1 where n = 5%';SQL_TEXT SQL_ID PLAN_HASH_VALUE VERSION_COUNT EXECUTIONS
----------------------------------- ------------- --------------- ------------- ----------
select sql_text,sql_id,plan_hash_va cn3cdtrt0scbj 232555890 1 1
lue,version_count,executions from v
$sqlarea where sql_text like '%fromt1 where n = 5%'select /*+ no_index(t1 idx_t1) */* 6km0nbhw11mvq 3617692013 1 3
from t1 where n = 5select /*+ index(t1 idx_t1) */* fro 0j50kmxrzm6nh 1369807930 1 3
m t1 where n = 5SCOTT@TNS_PDB01>select * from table(dbms_xplan.display_cursor(sql_id => '6km0nbhw11mvq',cursor_child_no => 0,format => 'advanced'));PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------------------------------------------
SQL_ID 6km0nbhw11mvq, child number 0
-------------------------------------
select /*+ no_index(t1 idx_t1) */* from t1 where n = 5Plan hash value: 3617692013--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 7 (100)| |
|* 1 | TABLE ACCESS FULL| T1 | 1 | 4 | 7 (0)| 00:00:01 |
--------------------------------------------------------------------------Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------1 - SEL$1 / T1@SEL$1Outline Data
-------------/*+BEGIN_OUTLINE_DATAIGNORE_OPTIM_EMBEDDED_HINTSOPTIMIZER_FEATURES_ENABLE('19.1.0')DB_VERSION('19.1.0')ALL_ROWSOUTLINE_LEAF(@"SEL$1")FULL(@"SEL$1" "T1"@"SEL$1")END_OUTLINE_DATA*/Predicate Information (identified by operation id):
---------------------------------------------------1 - filter("N"=5)Column Projection Information (identified by operation id):
-----------------------------------------------------------1 - "N"[NUMBER,22]Hint Report (identified by operation id / Query Block Name / Object Alias):
Total hints for statement: 1
---------------------------------------------------------------------------1 - SEL$1 / T1@SEL$1- no_index(t1 idx_t1)Query Block Registry:
---------------------<q o="2" f="y"><n><![CDATA[SEL$1]]></n><f><h><t><![CDATA[T1]]></t><s><![CDATA[SEL$1]]></s></h></f></q>56 rows selected.SCOTT@TNS_PDB01>select * from table(dbms_xplan.display_cursor(sql_id => '0j50kmxrzm6nh',cursor_child_no => 0,format => 'advanced'));PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------------------------------------------
SQL_ID 0j50kmxrzm6nh, child number 0
-------------------------------------
select /*+ index(t1 idx_t1) */* from t1 where n = 5Plan hash value: 1369807930---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 1 (100)| |
|* 1 | INDEX RANGE SCAN| IDX_T1 | 1 | 4 | 1 (0)| 00:00:01 |
---------------------------------------------------------------------------Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------1 - SEL$1 / T1@SEL$1Outline Data
-------------/*+BEGIN_OUTLINE_DATAIGNORE_OPTIM_EMBEDDED_HINTSOPTIMIZER_FEATURES_ENABLE('19.1.0')DB_VERSION('19.1.0')ALL_ROWSOUTLINE_LEAF(@"SEL$1")INDEX(@"SEL$1" "T1"@"SEL$1" ("T1"."N"))END_OUTLINE_DATA*/Predicate Information (identified by operation id):
---------------------------------------------------1 - access("N"=5)Column Projection Information (identified by operation id):
-----------------------------------------------------------1 - "N"[NUMBER,22]Hint Report (identified by operation id / Query Block Name / Object Alias):
Total hints for statement: 1
---------------------------------------------------------------------------1 - SEL$1 / T1@SEL$1- index(t1 idx_t1)Query Block Registry:
---------------------<q o="2" f="y"><n><![CDATA[SEL$1]]></n><f><h><t><![CDATA[T1]]></t><s><![CDATA[SEL$1]]></s></h></f></q>56 rows selected.
上传MOS文档上的 coe_xfr_sql_profile.sql到Oracle
[oracle@kkk ~]$ ls -l coe_xfr_sql_profile.sql
-rw-r–r-- 1 oracle oinstall 12957 Jan 13 13:12 coe_xfr_sql_profile.sql
3、使用NO_INDEX的SQL_ID和PLAN_HASH_VALUE执行脚本 coe_xfr_sql_profile.sql
SCOTT@TNS_PDB01>@/home/oracle/coe_xfr_sql_profile.sqlParameter 1:
SQL_ID (required)Enter value for 1: 6km0nbhw11mvqPLAN_HASH_VALUE AVG_ET_SECS
--------------- -----------3617692013 .025Parameter 2:
PLAN_HASH_VALUE (required)Enter value for 2: 3617692013Values passed to coe_xfr_sql_profile:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SQL_ID : "6km0nbhw11mvq"
PLAN_HASH_VALUE: "3617692013"SQL>BEGIN2 IF :sql_text IS NULL THEN3 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>BEGIN2 IF :other_xml IS NULL THEN3 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_6km0nbhw11mvq_3617692013.sql
on TARGET system in order to create a custom SQL Profile
with plan 3617692013 linked to adjusted sql_text.COE_XFR_SQL_PROFILE completed.SQL>host ls -l /home/oracle/coe_xfr_sql_profile_6km0nbhw11mvq_3617692013.sql
-rw-r--r-- 1 oracle oinstall 2514 Jan 13 15:37 /home/oracle/coe_xfr_sql_profile_6km0nbhw11mvq_3617692013.sql
4、INDEX的SQL_ID和PLAN_HASH_VALUE执行脚本 coe_xfr_sql_profile.sql
SQL>@/home/oracle/coe_xfr_sql_profile.sqlParameter 1:
SQL_ID (required)Enter value for 1: 0j50kmxrzm6nhPLAN_HASH_VALUE AVG_ET_SECS
--------------- -----------1369807930 .008Parameter 2:
PLAN_HASH_VALUE (required)Enter value for 2: 1369807930Values passed to coe_xfr_sql_profile:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SQL_ID : "0j50kmxrzm6nh"
PLAN_HASH_VALUE: "1369807930"SQL>BEGIN2 IF :sql_text IS NULL THEN3 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>BEGIN2 IF :other_xml IS NULL THEN3 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_0j50kmxrzm6nh_1369807930.sql
on TARGET system in order to create a custom SQL Profile
with plan 1369807930 linked to adjusted sql_text.COE_XFR_SQL_PROFILE completed.
SQL>
SQL>
SQL>host ls -l /home/oracle/coe_xfr_sql_profile_0j50kmxrzm6nh_1369807930.sql
-rw-r--r-- 1 oracle oinstall 2523 Jan 13 15:39 /home/oracle/coe_xfr_sql_profile_0j50kmxrzm6nh_1369807930.sql
5、将 /home/oracle/coe_xfr_sql_profile_0j50kmxrzm6nh_1369807930.sql中Hint部分替换到/home/oracle/coe_xfr_sql_profile_6km0nbhw11mvq_3617692013.sql中Hint并将force_match修改为true后执行home/oracle/coe_xfr_sql_profile_6km0nbhw11mvq_3617692013.sql脚本
SQL>@/home/oracle/coe_xfr_sql_profile_6km0nbhw11mvq_3617692013.sql
SQL>REM
SQL>REM $Header: 215187.1 coe_xfr_sql_profile_6km0nbhw11mvq_3617692013.sql 11.4.3.5 2020/01/13 carlos.sierra $
SQL>REM
SQL>REM Copyright (c) 2000-2011, 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_6km0nbhw11mvq_3617692013.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 6km0nbhw11mvq based on plan hash
SQL>REM value 3617692013.
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_6km0nbhw11mvq_3617692013.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_6km0nbhw11mvq_3617692013');
SQL>REM 5. Be aware that using DBMS_SQLTUNE requires a license
SQL>REM for the Oracle Tuning Pack.
SQL>REM
SQL>WHENEVER SQLERROR EXIT SQL.SQLCODE;
SQL>REM
SQL>VAR signature NUMBER;
SQL>REM
SQL>DECLARE2 sql_txt CLOB;3 h SYS.SQLPROF_ATTR;4 BEGIN5 sql_txt := q'[6 select /*+ no_index(t1 idx_t1)7 */* from t1 where n = 58 ]';9 h := SYS.SQLPROF_ATTR(10 q'[BEGIN_OUTLINE_DATA]',11 q'[IGNORE_OPTIM_EMBEDDED_HINTS]',12 q'[OPTIMIZER_FEATURES_ENABLE('19.1.0')]',13 q'[DB_VERSION('19.1.0')]',14 q'[ALL_ROWS]',15 q'[OUTLINE_LEAF(@"SEL$1")]',16 q'[INDEX(@"SEL$1" "T1"@"SEL$1" ("T1"."N"))]',17 q'[END_OUTLINE_DATA]');18 :signature := DBMS_SQLTUNE.SQLTEXT_TO_SIGNATURE(sql_txt);19 DBMS_SQLTUNE.IMPORT_SQL_PROFILE (20 sql_text => sql_txt,21 profile => h,22 name => 'coe_6km0nbhw11mvq_3617692013',23 description => 'coe 6km0nbhw11mvq 3617692013 '||:signature||'',24 category => 'DEFAULT',25 validate => TRUE,26 replace => TRUE,27 force_match => TRUE /* TRUE:FORCE (match even when different literals in SQL). FALSE:EXACT (similar to CURSOR_SHARING) */ );28 END;29 /PL/SQL procedure successfully completed.SQL>WHENEVER SQLERROR CONTINUE
SQL>SET ECHO OFF;SIGNATURE
---------------------1624786848478586381... manual custom SQL Profile has been createdCOE_XFR_SQL_PROFILE_6km0nbhw11mvq_3617692013 completed
6、测试
SQL>select /*+ no_index(t1 idx_t1) */* from t1 where n = 5;N
----------5SQL>select * from table(dbms_xplan.display_cursor(sql_id => null,cursor_child_no => null,format => 'advanced'));PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID 6km0nbhw11mvq, child number 0
-------------------------------------
select /*+ no_index(t1 idx_t1) */* from t1 where n = 5Plan hash value: 1369807930---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 1 (100)| |
|* 1 | INDEX RANGE SCAN| IDX_T1 | 1 | 4 | 1 (0)| 00:00:01 |PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
---------------------------------------------------------------------------Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------1 - SEL$1 / T1@SEL$1Outline Data
-------------/*+PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------BEGIN_OUTLINE_DATAIGNORE_OPTIM_EMBEDDED_HINTSOPTIMIZER_FEATURES_ENABLE('19.1.0')DB_VERSION('19.1.0')ALL_ROWSOUTLINE_LEAF(@"SEL$1")INDEX(@"SEL$1" "T1"@"SEL$1" ("T1"."N"))END_OUTLINE_DATA*/Predicate Information (identified by operation id):PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
---------------------------------------------------1 - access("N"=5)Column Projection Information (identified by operation id):
-----------------------------------------------------------1 - "N"[NUMBER,22]Hint Report (identified by operation id / Query Block Name / Object Alias):
Total hints for statement: 6 (U - Unused (1))PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
---------------------------------------------------------------------------0 - STATEMENT- ALL_ROWS- DB_VERSION('19.1.0')- IGNORE_OPTIM_EMBEDDED_HINTS- OPTIMIZER_FEATURES_ENABLE('19.1.0')1 - SEL$1 / T1@SEL$1U - no_index(t1 idx_t1) / rejected by IGNORE_OPTIM_EMBEDDED_HINTS- INDEX(@"SEL$1" "T1"@"SEL$1" ("T1"."N"))PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------Note
------ SQL profile coe_6km0nbhw11mvq_3617692013 used for this statementQuery Block Registry:
---------------------<q o="2" f="y"><n><![CDATA[SEL$1]]></n><f><h><t><![CDATA[T1]]></t><s><![CDATA[SEL$1]]></s></h></f></q>PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------67 rows selected.发现在使用NO_INDEX的SQL语句中走的索引了。修改谓词条件
SQL>select /*+ no_index(t1 idx_t1) */* from t1 where n = 7;N
----------7SQL>select * from table(dbms_xplan.display_cursor(sql_id => null,cursor_child_no => null,format => 'advanced'));PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID 8up3bffqmutxx, child number 0
-------------------------------------
select /*+ no_index(t1 idx_t1) */* from t1 where n = 7Plan hash value: 1369807930---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 1 (100)| |
|* 1 | INDEX RANGE SCAN| IDX_T1 | 1 | 4 | 1 (0)| 00:00:01 |PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
---------------------------------------------------------------------------Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------1 - SEL$1 / T1@SEL$1Outline Data
-------------/*+PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------BEGIN_OUTLINE_DATAIGNORE_OPTIM_EMBEDDED_HINTSOPTIMIZER_FEATURES_ENABLE('19.1.0')DB_VERSION('19.1.0')ALL_ROWSOUTLINE_LEAF(@"SEL$1")INDEX(@"SEL$1" "T1"@"SEL$1" ("T1"."N"))END_OUTLINE_DATA*/Predicate Information (identified by operation id):PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
---------------------------------------------------1 - access("N"=7)Column Projection Information (identified by operation id):
-----------------------------------------------------------1 - "N"[NUMBER,22]Hint Report (identified by operation id / Query Block Name / Object Alias):
Total hints for statement: 6 (U - Unused (1))PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
---------------------------------------------------------------------------0 - STATEMENT- ALL_ROWS- DB_VERSION('19.1.0')- IGNORE_OPTIM_EMBEDDED_HINTS- OPTIMIZER_FEATURES_ENABLE('19.1.0')1 - SEL$1 / T1@SEL$1U - no_index(t1 idx_t1) / rejected by IGNORE_OPTIM_EMBEDDED_HINTS- INDEX(@"SEL$1" "T1"@"SEL$1" ("T1"."N"))PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------Note
------ SQL profile coe_6km0nbhw11mvq_3617692013 used for this statementQuery Block Registry:
---------------------<q o="2" f="y"><n><![CDATA[SEL$1]]></n><f><h><t><![CDATA[T1]]></t><s><![CDATA[SEL$1]]></s></h></f></q>PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------67 rows selected.修改谓词条件后也走的是索引了,(概要文件中将force_match => TRUE)
这篇关于Oracle执行计划的稳定(使用MANUAL类型的SQL PROFILE)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!