在 简单讨论在11G,10G中如何稳定SQL执行计划(一) 中我们讨论了如何通过SPM中的sql plan baseline 来稳定11G中的执行计划
SPM是11G才有的新特性,那么在11G之前,我们是如何稳定执行计划的?
通过使用SQL PROFILE,关于SQL profile 的原理之类的,如上一篇一样,自己去百度。这里只讨论实际操作
首先,还是模拟出一个SQL对应出两个不同执行计划的情况
具体过程不再赘述,结果如下
SQL> select sql_id,child_number,plan_hash_value,sql_text from v$sql where sql_text like '%haha where%'; SQL_ID CHILD_NUMBER PLAN_HASH_VALUE------------- ------------ ---------------SQL_TEXT--------------------------------------------------------------------------------auwza0aq10mk0 0 3694936490select * from haha where a='10000'auwza0aq10mk0 1 1624320650select * from haha where a='10000'
通过查看,PLAN_HASH_VALUE为'3694936490'的,是 走了全表扫描 的执行计划,我这里假设走全表扫描是错的!
现在我们假设 走索引范围扫描的 ‘1624320650’ 执行计划,是正确的。
环境描述:
现在两个执行计划不同的子cursor,原本正常的时候为走 index range scan,现在突然改走 full table scan,导致sql非常缓慢
现在我们要做的,就是通过sql profile应急对其进行处理
如何处理,我们通过使用一个脚本‘coe_xfr_sql_profile.sql’ 这个脚本可以在MOS的文章 ‘215187.1‘找到
现在我们调用脚本
SQL> @coe_xfr_sql_profile.sqlParameter 1:SQL_ID (required)Enter value for 1: auwza0aq10mk0 ----该脚本第一个输入值,为要绑定执行计划SQL的SQL_ID,这里为auwza0aq10mk0PLAN_HASH_VALUE AVG_ET_SECS--------------- ----------- 1624320650 .002 3694936490 .028Parameter 2:PLAN_HASH_VALUE (required)Enter value for 2: 1624320650 -----选择要绑定的执行计划,这里输入1624320650Values passed to coe_xfr_sql_profile:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~SQL_ID : "auwza0aq10mk0"PLAN_HASH_VALUE: "1624320650"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_auwza0aq10mk0_1624320650.sqlon TARGET system in order to create a custom SQL Profilewith plan 1624320650 linked to adjusted sql_text.COE_XFR_SQL_PROFILE completed.
调用完这个脚本以后,会在系统的当前目录下,产生一个脚本,如这里为coe_xfr_sql_profile_auwza0aq10mk0_1624320650.sql
现在调用这个脚本,即可创建相应的SQL PROFILE
我们看执行过程(本执行过程不需要交互)
SQL>@coe_xfr_sql_profile_auwza0aq10mk0_1624320650.sqlSQL>REMSQL>REM $Header: 215187.1 coe_xfr_sql_profile_auwza0aq10mk0_1624320650.sql 11.4.4.4 2013/10/31 carlos.sierra $SQL>REMSQL>REM Copyright (c) 2000-2012, Oracle Corporation. All rights reserved.SQL>REMSQL>REM AUTHORSQL>REM [email protected]SQL>REMSQL>REM SCRIPTSQL>REM coe_xfr_sql_profile_auwza0aq10mk0_1624320650.sqlSQL>REMSQL>REM DESCRIPTIONSQL>REM This script is generated by coe_xfr_sql_profile.sqlSQL>REM It contains the SQL*Plus commands to create a customSQL>REM SQL Profile for SQL_ID auwza0aq10mk0 based on plan hashSQL>REM value 1624320650.SQL>REM The custom SQL Profile to be created by this scriptSQL>REM will affect plans for SQL commands with signatureSQL>REM matching the one for SQL Text below.SQL>REM Review SQL Text and adjust accordingly.SQL>REMSQL>REM PARAMETERSSQL>REM None.SQL>REMSQL>REM EXAMPLESQL>REM SQL> START coe_xfr_sql_profile_auwza0aq10mk0_1624320650.sql;SQL>REMSQL>REM NOTESSQL>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_auwza0aq10mk0_1624320650');SQL>REM 5. Be aware that using DBMS_SQLTUNE requires a licenseSQL>REM for the Oracle Tuning Pack.SQL>REM 6. If you modified a SQL putting Hints in order to produce a desiredSQL>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 originalSQL>REM SQL but with the Plan captured from the modified SQL (with Hints).SQL>REMSQL>WHENEVER SQLERROR EXIT SQL.SQLCODE;SQL>REMSQL>VAR signature NUMBER;SQL>VAR signaturef NUMBER;SQL>REMSQL>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 haha where a='10000']'); 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.3')]', 20 q'[DB_VERSION('11.2.0.3')]', 21 q'[FIRST_ROWS(10)]', 22 q'[OUTLINE_LEAF(@"SEL$1")]', 23 q'[INDEX_RS_ASC(@"SEL$1" "HAHA"@"SEL$1" ("HAHA"."A"))]', 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_auwza0aq10mk0_1624320650', 31 description => 'coe auwza0aq10mk0 1624320650 '||: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 CONTINUESQL>SET ECHO OFF; SIGNATURE--------------------- 3625523284774604774 SIGNATUREF--------------------- 2229953407394936365... manual custom SQL Profile has been createdCOE_XFR_SQL_PROFILE_auwza0aq10mk0_1624320650 completed
现在SQL profile已经创建好了,我们来执行对应的sql语句,并查看相应的执行计划
SQL>select * from haha where a='10000';A B------------------------------ ----------10000 10000SQL>select * from table(dbms_xplan.display_cursor());PLAN_TABLE_OUTPUT--------------------------------------------------------------------------------SQL_ID auwza0aq10mk0, child number 1-------------------------------------select * from haha where a='10000'Plan hash value: 1624320650----------------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |PLAN_TABLE_OUTPUT------------------------------------------------------------------------------------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | | | 14685 (100)| || 1 | TABLE ACCESS BY INDEX ROWID| HAHA | 1 | 11 | 14685 (1)| 00:02:57 ||* 2 | INDEX RANGE SCAN | HAHA_IDX | 1 | | 178 (0)| 00PLAN_TABLE_OUTPUT--------------------------------------------------------------------------------:00:03 |----------------------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 2 - access("A"='10000')PLAN_TABLE_OUTPUT--------------------------------------------------------------------------------Note----- - SQL profile coe_auwza0aq10mk0_1624320650 used for this statement23 rows selected.
注意看,INDEX RANGE SCAN,以及Note部分显示的,现在该SQL的执行计划已经稳定下来。
如果我们不想使用该SQL_PROFILE了该怎么办?
我们可以将SQLPROFILE删除(dbms_sqltune.drop_sql_profile)
命令本身很简单。
SQL>exec dbms_sqltune.drop_sql_profile(name=>'coe_auwza0aq10mk0_1624320650');PL/SQL procedure successfully completed.
现在我们看执行计划
SQL>select * from haha where a='10000';A B------------------------------ ----------10000 10000SQL>select * from table(dbms_xplan.display_cursor());PLAN_TABLE_OUTPUT--------------------------------------------------------------------------------SQL_ID auwza0aq10mk0, child number 1-------------------------------------select * from haha where a='10000'Plan hash value: 3694936490--------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |--------------------------------------------------------------------------| 0 | SELECT STATEMENT | | | | 32 (100)| ||* 1 | TABLE ACCESS FULL| HAHA | 1 | 11 | 32 (4)| 00:00:01 |PLAN_TABLE_OUTPUT----------------------------------------------------------------------------------------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 1 - filter("A"='10000')18 rows selected.
执行计划变回了 table access full。Note部分也消失不见。