一. 涉及到的函数(5点)
Summary of DBMS_SPM Subprograms ……………………………………………………………………………. 135-9
ALTER_SQL_PLAN_BASELINE Function ………………………………………………………………….. 135-10
DROP_SQL_PLAN_BASELINE Function ……………………………………………………………………. 135-14
EVOLVE_SQL_PLAN_BASELINE Function ……………………………………………………………….. 135-15
LOAD_PLANS_FROM_CURSOR_CACHE Functions ………………………………………………… 135-17
LOAD_PLANS_FROM_SQLSET Function ………………………………………………………………….. 135-19
1.
LOAD_PLANS_FROM_CURSOR_CACHE
DBMS_SPM.LOAD_PLANS_FROM_CURSOR_CACHE (
sql_id IN VARCHAR2,
plan_hash_value IN NUMBER := NULL,
sql_text IN CLOB,
fixed IN VARCHAR2 := ‘NO’,
enabled IN VARCHAR2 := ‘YES’)
RETURN PLS_INTEGER;
2.
DBMS_SPM.LOAD_PLANS_FROM_CURSOR_CACHE (
sql_id IN VARCHAR2,
plan_hash_value IN NUMBER := NULL,
sql_handle IN VARCHAR2,
fixed IN VARCHAR2 := ‘NO’,
enabled IN VARCHAR2 := ‘YES’)
RETURN PLS_INTEGER;
3.
DBMS_SPM.LOAD_PLANS_FROM_CURSOR_CACHE (
sql_id IN VARCHAR2,
plan_hash_value IN NUMBER := NULL,
fixed IN VARCHAR2 := ‘NO’,
enabled IN VARCHAR2 := ‘YES’)
RETURN PLS_INTEGER;
二. 涉及到的两个参数
optimizer_capture_sql_plan_baselines boolean FALSE –true,自动捕获;
optimizer_use_sql_plan_baselines boolean TRUE –baselin可否使用的开关;
三. 举例说明
3.1
SQL> create table t1 as select * from dba_objects;
Table created.
SQL> create index idx_t1 on t1(object_id);
Index created.
SQL> var a number;
SQL> exec :a :=88;
PL/SQL procedure successfully completed.
SQL> set autot traceonly exp
SQL> select object_name from t1 where object_id=:a;
Execution Plan
———————————————————-
Plan hash value: 50753647
————————————————————————————–
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
————————————————————————————–
| 0 | SELECT STATEMENT | | 694 | 54826 | 6 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| T1 | 694 | 54826 | 6 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | IDX_T1 | 278 | | 1 (0)| 00:00:01 |
————————————————————————————–
Predicate Information (identified by operation id):
—————————————————
2 – access(“OBJECT_ID”=TO_NUMBER(:A))
Note
—–
– dynamic sampling used for this statement (level=2)
3.2 查询SQL_ID, PLAN_HASH_VALUE
select sql_id,plan_hash_value,sql_text from v$sqlarea
where upper(sql_text) like upper(‘select object_name from t1 where object_id%’)
SQL_ID=g69va7fzttz9t PLAN_HASH_VALUE=50753647
3.3第一个函数使用:使用场景(共享池中已有可用的执行计划)
DBMS_SPM.LOAD_PLANS_FROM_CURSOR_CACHE (
sql_id IN VARCHAR2,
plan_hash_value IN NUMBER := NULL,
fixed IN VARCHAR2 := ‘NO’,
enabled IN VARCHAR2 := ‘YES’)
RETURN PLS_INTEGER;
3.3.1 操作:
DECLARE
k1 pls_integer;
begin
k1 := DBMS_SPM.LOAD_PLANS_FROM_CURSOR_CACHE (
sql_id=>’g69va7fzttz9t’,
plan_hash_value=>50753647,
fixed=>’NO’,
enabled=>’YES’);
end;
/
3.3.2 结果:
SQL> select object_name from t1 where object_id=:a;
Execution Plan
———————————————————-
Plan hash value: 50753647
————————————————————————————–
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
————————————————————————————–
| 0 | SELECT STATEMENT | | 694 | 54826 | 6 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| T1 | 694 | 54826 | 6 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | IDX_T1 | 278 | | 1 (0)| 00:00:01 |
————————————————————————————–
Predicate Information (identified by operation id):
—————————————————
2 – access(“OBJECT_ID”=TO_NUMBER(:A))
Note
—–
– dynamic sampling used for this statement (level=2)
– SQL plan baseline “SQL_PLAN_4cf2fwffq8ckv844cb98a” used for this statement –已经使用上baseline了;
————————————————————
3.4 第二个函数使用,使用场景:内存中已有执行计划但需要修正,且此SQL未建立BASELINE
DBMS_SPM.LOAD_PLANS_FROM_CURSOR_CACHE (
sql_id IN VARCHAR2,
plan_hash_value IN NUMBER := NULL,
sql_text IN CLOB,
fixed IN VARCHAR2 := ‘NO’,
enabled IN VARCHAR2 := ‘YES’)
RETURN PLS_INTEGER;
3.4.1 举例
select object_name from t1 where object_id=:b;
SQL> select object_name from t1 where object_id=:b;
Execution Plan
———————————————————-
Plan hash value: 50753647
————————————————————————————–
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
————————————————————————————–
| 0 | SELECT STATEMENT | | 694 | 54826 | 6 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| T1 | 694 | 54826 | 6 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | IDX_T1 | 278 | | 1 (0)| 00:00:01 |
————————————————————————————–
Predicate Information (identified by operation id):
—————————————————
2 – access(“OBJECT_ID”=TO_NUMBER(:B))
Note
—–
– dynamic sampling used for this statement (level=2)
3.4.2 构造一条修正后的SQL,并记录下其SQL_ID,PLAN_HASH_VALUE
select /*+ full(t1) */ object_name from t1 where object_id=:b;
44za4tu1rnnga 3617692013
3.4.3 操作:
declare
k1 pls_integer;
begin
k1:=DBMS_SPM.LOAD_PLANS_FROM_CURSOR_CACHE (
sql_id=>’44za4tu1rnnga’,
plan_hash_value=>3617692013,
sql_text=>’select object_name from t1 where object_id=:b’,
fixed=>’NO’,
enabled=>’YES’);
end;
/
3.4.4 结果:
SQL> select object_name from t1 where object_id=:b;
Execution Plan
———————————————————-
Plan hash value: 3617692013
————————————————————————–
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
————————————————————————–
| 0 | SELECT STATEMENT | | 866 | 68414 | 244 (1)| 00:00:05 |
|* 1 | TABLE ACCESS FULL| T1 | 866 | 68414 | 244 (1)| 00:00:05 |
————————————————————————–
Predicate Information (identified by operation id):
—————————————————
1 – filter(“OBJECT_ID”=TO_NUMBER(:B))
Note
—–
– SQL plan baseline “SQL_PLAN_0zt3455kptp8zdbd90e8e” used for this statement
3.5. 第3个函数使用:使用场景:SQL已有BASELINE但需修正;
DBMS_SPM.LOAD_PLANS_FROM_CURSOR_CACHE (
sql_id IN VARCHAR2,
plan_hash_value IN NUMBER := NULL,
sql_handle IN VARCHAR2,
fixed IN VARCHAR2 := ‘NO’,
enabled IN VARCHAR2 := ‘YES’)
RETURN PLS_INTEGER;
3.5.1 操作前可以先看下些SQL对应的BASELINE
select * from table(dbms_xplan.DISPLAY_SQL_PLAN_BASELINE(SQL_HANDLE=>’SQL_0fe46429655cd51f’,plan_name=>’SQL_PLAN_0zt3455kptp8zdbd90e8e’));
1* select * from table(dbms_xplan.DISPLAY_SQL_PLAN_BASELINE(SQL_HANDLE=>’SQL_0fe46429655cd51f’,plan_name=>’SQL_PLAN_0zt3455kptp8zdbd90e8e’))
SQL> /
PLAN_TABLE_OUTPUT
————————————————————————————————————————————————————————————
——————————————————————————–
SQL handle: SQL_0fe46429655cd51f
SQL text: select object_name from t1 where object_id=:b
——————————————————————————–
——————————————————————————–
Plan name: SQL_PLAN_0zt3455kptp8zdbd90e8e Plan id: 3688435342
Enabled: YES Fixed: NO Accepted: YES Origin: MANUAL-LOAD
——————————————————————————–
Plan hash value: 3617692013
————————————————————————–
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
————————————————————————–
| 0 | SELECT STATEMENT | | 866 | 68414 | 244 (1)| 00:00:05 |
|* 1 | TABLE ACCESS FULL| T1 | 866 | 68414 | 244 (1)| 00:00:05 |
————————————————————————–
Predicate Information (identified by operation id):
—————————————————
1 – filter(“OBJECT_ID”=TO_NUMBER(:B))
24 rows selected.
3.5.2 修正的SQL,并获取SQL_ID,PLAN_HASH_VALUE
select /*+ index(t1) */object_name from t1 where object_id=:b
4krd3njkgkx3a 50753647
3.5.3 操作:
declare
k1 pls_integer;
begin
k1:=DBMS_SPM.LOAD_PLANS_FROM_CURSOR_CACHE (
sql_id=>’4krd3njkgkx3a’,
plan_hash_value=>50753647,
sql_handle=>’SQL_0fe46429655cd51f’,–原来不正确BASELINE
fixed=>’NO’,
enabled=>’YES’);
end;
/
3.5.4 验证下执行计划
select * from table(dbms_xplan.DISPLAY_SQL_PLAN_BASELINE(SQL_HANDLE=>’SQL_0fe46429655cd51f’,plan_name=>’SQL_PLAN_0zt3455kptp8z844cb98a’));
3.5.5 删除多余的baseline
declare
k1 pls_integer;
begin
k1:=DBMS_SPM.DROP_SQL_PLAN_BASELINE (
sql_handle=>’SQL_0fe46429655cd51f’,
plan_name=>’SQL_PLAN_0zt3455kptp8zdbd90e8e’);
end;
/
3.5.6 结果:
SQL> select object_name from t1 where object_id=:b;
Execution Plan
———————————————————-
Plan hash value: 50753647
————————————————————————————–
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
————————————————————————————–
| 0 | SELECT STATEMENT | | 694 | 54826 | 6 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| T1 | 694 | 54826 | 6 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | IDX_T1 | 278 | | 1 (0)| 00:00:01 |
————————————————————————————–
Predicate Information (identified by operation id):
—————————————————
2 – access(“OBJECT_ID”=TO_NUMBER(:B))
Note
—–
– dynamic sampling used for this statement (level=2)
– SQL plan baseline “SQL_PLAN_0zt3455kptp8z844cb98a” used for this statement –使用上新的修正的BASELINE
四 . 修改属性
DBMS_SPM.ALTER_SQL_PLAN_BASELINE (
sql_handle IN VARCHAR2 := NULL,
plan_name IN VARCHAR2 := NULL,
attribute_name IN VARCHAR2,
attribute_value IN VARCHAR2)
RETURN PLS_INTEGER;
4.1 包含的属性
dba_sql_plan_baselines.enabled,dba_sql_plan_baselines.fixed其中这个是不能改的dba_sql_plan_baselines.accepted(可以使用演进)
4.2 操作(fixed属性修改)
declare
k1 pls_integer;
begin
k1:=DBMS_SPM.ALTER_SQL_PLAN_BASELINE (
sql_handle=>’SQL_0fe46429655cd51f’,
plan_name=>’SQL_PLAN_0zt3455kptp8z844cb98a’,
attribute_name=>’FIXED’,
attribute_value=>’YES’);
end;
/
五. EVOLVE_SQL_PLAN_BASELINE函数(baseline演进)
5.1 场景:参数optimizer_capture_sql_plan_baselines=true
SQL> show parameter baseline
NAME TYPE VALUE
———————————— ———– ——————————
optimizer_capture_sql_plan_baselines boolean TRUE
5.2 同一条SQL生成了2种执行计划被自动捕获(sql_id相等,sql_handle也是相等,plan_name不同)
SQL_HANDLE PLAN_NAME ENABLED ACCEPTED FIXED
SQL_443bd90af74d056b SQL_PLAN_48fyt1bvnu1bbb3da4735 YES NO NO
SQL_443bd90af74d056b SQL_PLAN_48fyt1bvnu1bb74df6e96 YES YES NO
5.3 演化操作:
现在目的是通过演进函数,将accepted=>no 转化为yes,这样库系统就可以使用此baseline了;
declare
k1 clob;
begin
k1:=DBMS_SPM.EVOLVE_SQL_PLAN_BASELINE (
sql_handle=>’SQL_443bd90af74d056b’,
plan_name =>’SQL_PLAN_48fyt1bvnu1bbb3da4735′,
verify=>’NO’,
commit=> ‘YES’);
end;
/
5.4 结果:accepted=>NO,转为accepted=>yes
SQL_HANDLE PLAN_NAME ENABLED ACCEPTED FIXED
SQL_443bd90af74d056b SQL_PLAN_48fyt1bvnu1bbb3da4735 YES YES NO
SQL_443bd90af74d056b SQL_PLAN_48fyt1bvnu1bb74df6e96 YES YES NO
5.5 说明:
(1)Verify:
+ YES (只有性能更好的计划才会被演化)
+ NO (演化所有的计划)
(2)Commit:
+ YES (直接演化)
+ NO (只生成报告)
这里可以通过不同的排列组合,达到不同的效果:
(1)自动接收所有性能更好的执行计划(Verify->YES, Commit->YES)
(2)自动接收所有新的执行计划 (Verify->NO,Commit->YES)
(3)比较性能,生成报告,人工确认是否演化(Verify->NO, Commit->NO)
6. :1,:2,:3…变量情况
6.1 通过pl/sql 构造
declare
l_dept pls_integer := 20;
l_nam number;
begin
execute immediate ‘select /*+ full(t1) */ count(1) from wxh_tbd where object_id=:1’
into l_nam
using l_dept ;
end;
/
7. AWR提取BASELINE
LOAD_PLANS_FROM_SQLSET
DBMS_SPM.LOAD_PLANS_FROM_SQLSET (
sqlset_name IN VARCHAR2,
sqlset_owner IN VARCHAR2 := NULL,
basic_filter IN VARCHAR2 := NULL,
fixed IN VARCHAR2 := ‘NO’,
enabled IN VARCHAR2 := ‘YES’
commit_rows IN NUMBER := 1000)
RETURN PLS_INTEGER;
7.1先通过dbms_xplan.display_awr(sql_id=>”,format=>’advanced’)查看AWR历史记录情况;
SQL> select * from table(dbms_xplan.display_awr(sql_id=>’60hrphhd8tk2b’,format=>’allstats advanced’));
PLAN_TABLE_OUTPUT
————————————————————————————————————————————————————————————
SQL_ID 60hrphhd8tk2b
——————–
select count(1) from t2 where object_id=:a
Plan hash value: 3088483145
—————————————————————————–
| Id | Operation | Name | E-Rows |E-Bytes| Cost (%CPU)| E-Time |
—————————————————————————–
| 0 | SELECT STATEMENT | | | | 1 (100)| |
| 1 | SORT AGGREGATE | | 1 | 3 | | |
| 2 | INDEX RANGE SCAN| IDX_01 | 13 | 39 | 1 (0)| 00:00:01 |
—————————————————————————–
Query Block Name / Object Alias (identified by operation id):
————————————————————-
1 – SEL$1
2 – SEL$1 / T2@SEL$1
Outline Data
————-
/*+
BEGIN_OUTLINE_DATA
IGNORE_OPTIM_EMBEDDED_HINTS
OPTIMIZER_FEATURES_ENABLE(‘11.2.0.2’)
DB_VERSION(‘11.2.0.2’)
ALL_ROWS
OUTLINE_LEAF(@”SEL$1″)
INDEX(@”SEL$1″ “T2″@”SEL$1” (“T2″.”OBJECT_ID”))
END_OUTLINE_DATA
*/
Peeked Binds (identified by position):
————————————–
1 – :A (NUMBER): 1000000
Note
—–
– Warning: basic plan statistics not available. These are only collected when:
* hint ‘gather_plan_statistics’ is used for the statement or
* parameter ‘statistics_level’ is set to ‘ALL’, at session or system level
SQL_ID 60hrphhd8tk2b
——————–
select count(1) from t2 where object_id=:a
Plan hash value: 3971548523
———————————————————————————
| Id | Operation | Name | E-Rows |E-Bytes| Cost (%CPU)| E-Time |
———————————————————————————
| 0 | SELECT STATEMENT | | | | 34 (100)| |
| 1 | SORT AGGREGATE | | 1 | 3 | | |
| 2 | INDEX FAST FULL SCAN| IDX_01 | 72586 | 212K| 34 (0)| 00:00:01 |
———————————————————————————
Query Block Name / Object Alias (identified by operation id):
————————————————————-
1 – SEL$1
2 – SEL$1 / T2@SEL$1
Outline Data
————-
/*+
BEGIN_OUTLINE_DATA
IGNORE_OPTIM_EMBEDDED_HINTS
OPTIMIZER_FEATURES_ENABLE(‘11.2.0.2’)
DB_VERSION(‘11.2.0.2’)
ALL_ROWS
OUTLINE_LEAF(@”SEL$1″)
INDEX_FFS(@”SEL$1″ “T2″@”SEL$1” (“T2″.”OBJECT_ID”))
END_OUTLINE_DATA
*/
Peeked Binds (identified by position):
————————————–
1 – :A (NUMBER): 88
Note
—–
– Warning: basic plan statistics not available. These are only collected when:
* hint ‘gather_plan_statistics’ is used for the statement or
* parameter ‘statistics_level’ is set to ‘ALL’, at session or system level
7.2 先大致查询SQL_ID所被捕获的时间点
select sql_id,plan_hash_value,timestamp from dba_hist_sql_plan where sql_id=’chmc32x4rt821′;
ID SQL_ID PLAN_HASH_VALUE TIMESTAMP
5 60hrphhd8tk2b 3088483145 2013/10/27 18:16:38
6 60hrphhd8tk2b 3088483145 2013/10/27 18:16:38
3 60hrphhd8tk2b 3088483145 2013/10/27 18:16:38
1 60hrphhd8tk2b 3971548523 2013/10/27 18:20:44
2 60hrphhd8tk2b 3971548523 2013/10/27 18:20:44
4 60hrphhd8tk2b 3971548523 2013/10/27 18:20:44
7.3 再查询这个被捕获时间所在的SANP_ID的范围
SQL> SELECT SNAP_ID, BEGIN_INTERVAL_TIME, END_INTERVAL_TIME FROM dba_hist_snapshot ORDER BY END_INTERVAL_TIME DESC;
SNAP_ID BEGIN_INTERVAL_TIME END_INTERVAL_TIME
———- ————————————————————————— —————————————————————————
631 27-OCT-13 07.10.31.655 PM 27-OCT-13 07.20.32.135 PM
630 27-OCT-13 07.00.30.311 PM 27-OCT-13 07.10.31.655 PM
629 27-OCT-13 06.50.29.862 PM 27-OCT-13 07.00.30.311 PM
628 27-OCT-13 06.40.29.213 PM 27-OCT-13 06.50.29.862 PM
627 27-OCT-13 06.33.37.207 PM 27-OCT-13 06.40.29.213 PM
626 27-OCT-13 06.30.28.709 PM 27-OCT-13 06.33.37.207 PM
625 27-OCT-13 06.20.28.263 PM 27-OCT-13 06.30.28.709 PM
624 27-OCT-13 06.10.27.754 PM 27-OCT-13 06.20.28.263 PM
623 27-OCT-13 06.00.27.194 PM 27-OCT-13 06.10.27.754 PM
7.4 生成STS(SQL TUNING SET)
DECLARE
cur sys_refcursor;
BEGIN
OPEN cur FOR
SELECT VALUE(P)
FROM TABLE(
dbms_sqltune.select_workload_repository
(begin_snap=>624, –老执行计划起始的snap id
end_snap=>626, –老执行计划结束的snap id
basic_filter=>’sql_id = ”60hrphhd8tk2b”’, –老执行计划起始的sql id
attribute_list=>’ALL’)
) p;
DBMS_SQLTUNE.LOAD_SQLSET( sqlset_name=> ‘mysts01’, populate_cursor=>cur);
CLOSE cur;
END;
/
7.5 STS导入到BASELINE
declare
k1 pls_integer;
begin
k1:=DBMS_SPM.LOAD_PLANS_FROM_SQLSET (
sqlset_name=>’mysts01′,
sqlset_owner=>’AIKI’,
basic_filter=>’plan_hash_value = ”3971548523”’ );
end;
/
7.6 结果:在dba_sql_plan_baselines中可以看到新导入的BASELINE;
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.fwqtg.net
相关推荐: Qt quick基础3(基础动画,包含旋转动画、串行并行动画及其嵌套)
Qt quick基础3(基础动画)Qt quick基础3(基础动画) 目录 Qt quick基础3(基础动画) 前言 前期准备工作 Animation on property 元素加载后自动运行动画 Behavior on property 当元素值改变后运行动画 Standalone Ani…