oracle 为了优化SQL性能,最初引入 outline 之后是 sql profile 然后是 sql plan baseline
可以理解 SQL PLAN BASELINE 就是 outline / sql profile 的升级版
那么我们就来测试一下建立 sql plan baseline
1.检查参数
SQL> show parameter OPTIMIZER_USE_SQL_PLAN_BASELINE
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
optimizer_use_sql_plan_baselines boolean TRUE
这个参数是控制 oracle 是否使用基线 默认为true
2.通过自动捕获创建一个 sql plan baseline
SQL> show parameter optimizer_capture_sql_plan_baselines
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
optimizer_capture_sql_plan_baselines boolean FALSE
SQL> alter system set optimizer_capture_sql_plan_baselines = true;
系统已更改。
SQL> create table system.wh as select * from dba_objects;
表已创建。
SQL> create index system.ll on system.wh(object_id);
索引已创建。
SQL> select count(*) from system.wh where object_id=2;
COUNT(*)
----------
1
SQL > select sql_handle, sql_text, enabled, accepted from dba_sql_plan_baselines where creator = 'SYS' and sql_text like '%select count(*) from system.wh where object_id=2%' and created > systimestamp - to_dsinterval('0 00:15:00');
no rows
SQL> select count(*) from system.wh where object_id=2;
COUNT(*)
----------
1
SQL> select sql_handle, sql_text, enabled, accepted from dba_sql_plan_baselines where creator = 'SYS' and sql_text like '%select count(*) from system.wh where object_id=2%' and created > systimestamp - to_dsinterval('0 00:15:00');
SQL_HANDLE SQL_TEXT ENA ACC
------------------------------ -------------------------------------------------------------------------------- --- ---
SQL_421367f859113b61 select count(*) from system.wh where object_id=2 YES YES
好了,我们回头看一下,第一次没有生成 sql plan baseline,但是信息应该是保存在了 sql management base 与 statement log 中
第二次,发现 sql managemtn base 与 statement log 中已经存在了 签名,这时就添加了一条 sql plan baseline
3.手动创建一个 sql plan baseline
SQL> alter system set optimizer_capture_sql_plan_baselines = false;
系统已更改。
SQL> create table system.wh as select * from dba_objects;
表已创建。
SQL> create index system.ll on system.wh(object_id);
索引已创建。
SQL> select /* find me */ count(*) from system.wh where object_id=1;
COUNT(*)
----------
0
SQL> select sql_id,sql_text from v$sql where sql_text like '%find me%';
SQL_ID SQL_TEXT
7rnmxtvcmjdsm select sql_id from v$sql where sql_text like '%find me%'
cuwx15qtyxgt2 select /* find me */ count(*) from system.wh where object_id=1
1r1ygnhzhtxsh select sql_id,sql_text from v$sql where sql_text like '%find me%'
可以看到 cuwx15qtyxgt2 是需要定位的 sql_id
DECLARE
ret PLS_INTEGER;
BEGIN
ret := dbms_spm.load_plans_from_cursor_cache(sql_id=> 'cuwx15qtyxgt2',plan_hash_value => NULL);
END;
/
创建sql plan baseline
SQL> select sql_handle, sql_text, enabled, accepted from dba_sql_plan_baselines where creator = 'SYS' and sql_text like '%/* find me */%' and created > systimestamp - to_dsinterval('0 00:15:00');
SQL_HANDLE SQL_TEXT ENA ACC
------------------------------ -------------------------------------------------------------------------------- --- ---
SQL_4ea485cd897f01ba select /* find me */ count(*) from system.wh where object_id=1 YES YES
我们也看到, sql plan base line 已经创建成功了
4.删除 sql plan base line
SET SERVEROUTPUT ON
DECLARE
v_text PLS_INTEGER;
BEGIN
v_text := DBMS_SPM.drop_sql_plan_baseline(sql_handle => 'SQL_421367f859113b61',plan_name => NULL);
DBMS_OUTPUT.put_line(v_text);
END;
/
关于基线的进化等高科技技能,这次测试暂且不做测试,今天你们学会了么
可以理解 SQL PLAN BASELINE 就是 outline / sql profile 的升级版
那么我们就来测试一下建立 sql plan baseline
1.检查参数
SQL> show parameter OPTIMIZER_USE_SQL_PLAN_BASELINE
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
optimizer_use_sql_plan_baselines boolean TRUE
这个参数是控制 oracle 是否使用基线 默认为true
2.通过自动捕获创建一个 sql plan baseline
SQL> show parameter optimizer_capture_sql_plan_baselines
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
optimizer_capture_sql_plan_baselines boolean FALSE
SQL> alter system set optimizer_capture_sql_plan_baselines = true;
系统已更改。
SQL> create table system.wh as select * from dba_objects;
表已创建。
SQL> create index system.ll on system.wh(object_id);
索引已创建。
SQL> select count(*) from system.wh where object_id=2;
COUNT(*)
----------
1
SQL > select sql_handle, sql_text, enabled, accepted from dba_sql_plan_baselines where creator = 'SYS' and sql_text like '%select count(*) from system.wh where object_id=2%' and created > systimestamp - to_dsinterval('0 00:15:00');
no rows
SQL> select count(*) from system.wh where object_id=2;
COUNT(*)
----------
1
SQL> select sql_handle, sql_text, enabled, accepted from dba_sql_plan_baselines where creator = 'SYS' and sql_text like '%select count(*) from system.wh where object_id=2%' and created > systimestamp - to_dsinterval('0 00:15:00');
SQL_HANDLE SQL_TEXT ENA ACC
------------------------------ -------------------------------------------------------------------------------- --- ---
SQL_421367f859113b61 select count(*) from system.wh where object_id=2 YES YES
好了,我们回头看一下,第一次没有生成 sql plan baseline,但是信息应该是保存在了 sql management base 与 statement log 中
第二次,发现 sql managemtn base 与 statement log 中已经存在了 签名,这时就添加了一条 sql plan baseline
3.手动创建一个 sql plan baseline
SQL> alter system set optimizer_capture_sql_plan_baselines = false;
系统已更改。
SQL> create table system.wh as select * from dba_objects;
表已创建。
SQL> create index system.ll on system.wh(object_id);
索引已创建。
SQL> select /* find me */ count(*) from system.wh where object_id=1;
COUNT(*)
----------
0
SQL> select sql_id,sql_text from v$sql where sql_text like '%find me%';
SQL_ID SQL_TEXT
7rnmxtvcmjdsm select sql_id from v$sql where sql_text like '%find me%'
cuwx15qtyxgt2 select /* find me */ count(*) from system.wh where object_id=1
1r1ygnhzhtxsh select sql_id,sql_text from v$sql where sql_text like '%find me%'
可以看到 cuwx15qtyxgt2 是需要定位的 sql_id
DECLARE
ret PLS_INTEGER;
BEGIN
ret := dbms_spm.load_plans_from_cursor_cache(sql_id=> 'cuwx15qtyxgt2',plan_hash_value => NULL);
END;
/
创建sql plan baseline
SQL> select sql_handle, sql_text, enabled, accepted from dba_sql_plan_baselines where creator = 'SYS' and sql_text like '%/* find me */%' and created > systimestamp - to_dsinterval('0 00:15:00');
SQL_HANDLE SQL_TEXT ENA ACC
------------------------------ -------------------------------------------------------------------------------- --- ---
SQL_4ea485cd897f01ba select /* find me */ count(*) from system.wh where object_id=1 YES YES
我们也看到, sql plan base line 已经创建成功了
4.删除 sql plan base line
SET SERVEROUTPUT ON
DECLARE
v_text PLS_INTEGER;
BEGIN
v_text := DBMS_SPM.drop_sql_plan_baseline(sql_handle => 'SQL_421367f859113b61',plan_name => NULL);
DBMS_OUTPUT.put_line(v_text);
END;
/
关于基线的进化等高科技技能,这次测试暂且不做测试,今天你们学会了么
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7569309/viewspace-2135540/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/7569309/viewspace-2135540/