outline执行计划稳定使用
2014-10-27 10:01
351 查看
--1、创建表
create table a as select * from all_objects
update a set a.object_id=21 where object_id >=100;
commit;
create index a_inx on a(object_id);
begin
dbms_stats.gather_table_stats('TEST','a');
end;
--2、
--iffs
variable i number
variable k number
execute :i:=20
execute :k:=21
select /*+gather_plan_statistics*/ count(*) from a where object_id between :i and :k;
select * from table(dbms_xplan.display_cursor('085j460v6su03',null,'ALLSTATS LAST'));
--实际iffs应该irs
execute :i:=22
execute :k:=25
select /*+gather_plan_statistics*/ count(*) from a where object_id between :i and :k;
select * from table(dbms_xplan.display_cursor('085j460v6su03',null,'ALLSTATS LAST'));
--验证实际带入变量值irs
select /*+gather_plan_statistics*/ count(*) from a where object_id between 22 and 25;
--------------------------------------------------------------------------------------
/*这里可以看到后续只要使用的绑定变量后续执行计划就都是iffs了除非该执行计划失效才会重新生成*/
--------------------------------------------------------------------------------------
--重新生成执行计划方法
/*
1、重新收集统计信息等待执行计划过期(默认"_optimizer_invalidation_period"过期时间5h);
2、执行计划不在share pool(手动flush或age out等)
3、10.2.0.4后提供了个dbms_shared_pool.purge
4、对该表A做一个ddl操作,如添加一个注释comments */
alter system flush shared_pool;
irs
execute :i:=22
execute :k:=25
select /*+gather_plan_statistics*/ count(*) from a where object_id between :i and :k;
select * from table(dbms_xplan.display_cursor('085j460v6su03',null,'ALLSTATS LAST'));
irs
execute :i:=20
execute :k:=21
select /*+gather_plan_statistics*/ count(*) from a where object_id between :i and :k;
select * from table(dbms_xplan.display_cursor('085j460v6su03',null,'ALLSTATS LAST'));
----以上可以看到大量数据还是走到irs这就是我们要的效果
----outline固定执行计划--------
--outline相关操作
--select * from dba_outlines 查看索引outline
--drop outline oltname 删除outline
--alter system set use_stored_outlines=ture; 禁用outline默认
--找到我们需要的绑定的执行计划
select sql_text, sql_id, hash_value, child_number
from v$sql
where sql_text like '%gather_plan_statistics%'
and sql_text not like '%like%';
hash_value=913074179
child_number=0
--直接create outline需要用sql这里直接调用过程使用hash-value
--
begin
dbms_outln.create_outline('913074179','0');
end;
---
--查询已经生成
select * from dba_outlines
--让执outline生效默认是false
alter system set use_stored_outlines=ture;
---测试效果
--现在的执行计划是irs如果我们清除执行计划在使用21范围查询看结果
--如果结果是iffs那么就没有绑定成功
alter system flush shared_pool;
execute :i:=20
execute :k:=21
select /*+gather_plan_statistics*/ count(*) from a where object_id between :i and :k;
select * from table(dbms_xplan.display_cursor('085j460v6su03',null,'ALLSTATS LAST'));
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID 085j460v6su03, child number 1
-------------------------------------
select /*+gather_plan_statistics*/ count(*) from a where object_id between
:i and :k
Plan hash value: 2093133565
--------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buf
--------------------------------------------------------------------------------
| 1 | SORT AGGREGATE | | 1 | 1 | 1 |00:00:00.10 |
|* 2 | FILTER | | 1 | | 49649 |00:00:00.01 |
|* 3 | INDEX RANGE SCAN| A_INX | 1 | 49627 | 49649 |00:00:00.01 |
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter(:I<=:K)
3 - access("OBJECT_ID">=:I AND "OBJECT_ID"<=:K)
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Note
-----
- outline "SYS_OUTLINE_14102216460808404" used for this statement
46 rows selected
------以上已经可以清楚看到outline已经生效执行计划里面也有提示
Note
-----
- outline "SYS_OUTLINE_14102216460808404" used for this statement
---------------------------------------------------------------------------------
--重启实例后该outline回到false失效需要手动启动或者做一个启动触发器每次启动自动执行
--同时启动outline和sqlprofile后会选择outline执行计划
--alter system set use_stored_outlines=ture;
SQL>
SQL> execute :i:=20
Warning: connection was lost and re-established
PL/SQL procedure successfully completed
i
---------
20
SQL> execute :k:=21
PL/SQL procedure successfully completed
k
---------
21
SQL> select /*+gather_plan_statistics*/ count(*) from a where object_id between :i and :k;
COUNT(*)
----------
49649
i
---------
20
k
---------
21
SQL> select * from table(dbms_xplan.display_cursor('085j460v6su03',null,'ALLSTATS LAST'));
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID 085j460v6su03, child number 0
-------------------------------------
select /*+gather_plan_statistics*/ count(*) from a where object_id between :i a
Plan hash value: 831184990
--------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time |
--------------------------------------------------------------------------------
| 1 | SORT AGGREGATE | | 1 | 1 | 1 |00:00:00.03 |
|* 2 | FILTER | | 1 | | 49649 |00:00:00.02 |
|* 3 | INDEX FAST FULL SCAN| A_INX | 1 | 49627 | 49649 |00:00:00.02 |
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter(:I<=:K)
3 - filter(("OBJECT_ID"<=:K AND "OBJECT_ID">=:I))
--1、创建表
create table a as select * from all_objects
update a set a.object_id=21 where object_id >=100;
commit;
create index a_inx on a(object_id);
begin
dbms_stats.gather_table_stats('TEST','a');
end;
--2、
--iffs
variable i number
variable k number
execute :i:=20
execute :k:=21
select /*+gather_plan_statistics*/ count(*) from a where object_id between :i and :k;
select * from table(dbms_xplan.display_cursor('085j460v6su03',null,'ALLSTATS LAST'));
--实际iffs应该irs
execute :i:=22
execute :k:=25
select /*+gather_plan_statistics*/ count(*) from a where object_id between :i and :k;
select * from table(dbms_xplan.display_cursor('085j460v6su03',null,'ALLSTATS LAST'));
--验证实际带入变量值irs
select /*+gather_plan_statistics*/ count(*) from a where object_id between 22 and 25;
--------------------------------------------------------------------------------------
/*这里可以看到后续只要使用的绑定变量后续执行计划就都是iffs了除非该执行计划失效才会重新生成*/
--------------------------------------------------------------------------------------
--重新生成执行计划方法
/*
1、重新收集统计信息等待执行计划过期(默认"_optimizer_invalidation_period"过期时间5h);
2、执行计划不在share pool(手动flush或age out等)
3、10.2.0.4后提供了个dbms_shared_pool.purge
4、对该表A做一个ddl操作,如添加一个注释comments */
alter system flush shared_pool;
irs
execute :i:=22
execute :k:=25
select /*+gather_plan_statistics*/ count(*) from a where object_id between :i and :k;
select * from table(dbms_xplan.display_cursor('085j460v6su03',null,'ALLSTATS LAST'));
irs
execute :i:=20
execute :k:=21
select /*+gather_plan_statistics*/ count(*) from a where object_id between :i and :k;
select * from table(dbms_xplan.display_cursor('085j460v6su03',null,'ALLSTATS LAST'));
----以上可以看到大量数据还是走到irs这就是我们要的效果
----outline固定执行计划--------
--outline相关操作
--select * from dba_outlines 查看索引outline
--drop outline oltname 删除outline
--alter system set use_stored_outlines=ture; 禁用outline默认
--找到我们需要的绑定的执行计划
select sql_text, sql_id, hash_value, child_number
from v$sql
where sql_text like '%gather_plan_statistics%'
and sql_text not like '%like%';
hash_value=913074179
child_number=0
--直接create outline需要用sql这里直接调用过程使用hash-value
--
begin
dbms_outln.create_outline('913074179','0');
end;
---
--查询已经生成
select * from dba_outlines
--让执outline生效默认是false
alter system set use_stored_outlines=ture;
---测试效果
--现在的执行计划是irs如果我们清除执行计划在使用21范围查询看结果
--如果结果是iffs那么就没有绑定成功
alter system flush shared_pool;
execute :i:=20
execute :k:=21
select /*+gather_plan_statistics*/ count(*) from a where object_id between :i and :k;
select * from table(dbms_xplan.display_cursor('085j460v6su03',null,'ALLSTATS LAST'));
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID 085j460v6su03, child number 1
-------------------------------------
select /*+gather_plan_statistics*/ count(*) from a where object_id between
:i and :k
Plan hash value: 2093133565
--------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buf
--------------------------------------------------------------------------------
| 1 | SORT AGGREGATE | | 1 | 1 | 1 |00:00:00.10 |
|* 2 | FILTER | | 1 | | 49649 |00:00:00.01 |
|* 3 | INDEX RANGE SCAN| A_INX | 1 | 49627 | 49649 |00:00:00.01 |
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter(:I<=:K)
3 - access("OBJECT_ID">=:I AND "OBJECT_ID"<=:K)
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Note
-----
- outline "SYS_OUTLINE_14102216460808404" used for this statement
46 rows selected
------以上已经可以清楚看到outline已经生效执行计划里面也有提示
Note
-----
- outline "SYS_OUTLINE_14102216460808404" used for this statement
---------------------------------------------------------------------------------
--重启实例后该outline回到false失效需要手动启动或者做一个启动触发器每次启动自动执行
--同时启动outline和sqlprofile后会选择outline执行计划
--alter system set use_stored_outlines=ture;
SQL>
SQL> execute :i:=20
Warning: connection was lost and re-established
PL/SQL procedure successfully completed
i
---------
20
SQL> execute :k:=21
PL/SQL procedure successfully completed
k
---------
21
SQL> select /*+gather_plan_statistics*/ count(*) from a where object_id between :i and :k;
COUNT(*)
----------
49649
i
---------
20
k
---------
21
SQL> select * from table(dbms_xplan.display_cursor('085j460v6su03',null,'ALLSTATS LAST'));
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID 085j460v6su03, child number 0
-------------------------------------
select /*+gather_plan_statistics*/ count(*) from a where object_id between :i a
Plan hash value: 831184990
--------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time |
--------------------------------------------------------------------------------
| 1 | SORT AGGREGATE | | 1 | 1 | 1 |00:00:00.03 |
|* 2 | FILTER | | 1 | | 49649 |00:00:00.02 |
|* 3 | INDEX FAST FULL SCAN| A_INX | 1 | 49627 | 49649 |00:00:00.02 |
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter(:I<=:K)
3 - filter(("OBJECT_ID"<=:K AND "OBJECT_ID">=:I))
相关文章推荐
- 使用stored_outline优化、稳定执行计划
- 使用stored_outline优化、稳定执行计划
- 使用OUTLINE调整执行计划
- 使用outline稳固sql执行计划
- stored_outline优化稳定执行计划
- 使用outline固定执行计划
- 《基于ORACLE SQL优化》读书笔记-使用SPM稳定执行计划
- ORACLE使用STORED OUTLINE固化执行计划--CURSOR_SHARING
- ORACLE使用STORED OUTLINE固化执行计划--私有和公有
- 使用outline固定sql执行计划
- SQL优化--使用 EXISTS 代替 IN 和 inner join来选择正确的执行计划
- 执行计划的使用(EXPLAIN)
- 使用mysql profiles 来查看sql 语句执行计划
- 为什么Oracle有时会用索引来查找数据?--强制Oracle使用最优的“执行计划”
- 学习散记8之--执行计划的缓存和重新使用
- 应用程序对Sqlserver进行SQL查询使用与不使用参数对执行计划的影响
- 使用 EXPLAIN PLAN 获取SQL语句执行计划
- oracle--如何干预执行计划(使用hints提示)
- Linux下使用crontab来执行定时任务计划
- 如何使用SQLPLUS分析SQL语句(查询执行计划跟踪)