[20120807]11G SPM的學習5.txt--第3方優化
[20120807]11G SPM的學習5.txt--第3方優化
繼續上面的學習:
1.假設程式中的程式碼寫成如下:
select /*+ full(t) */ * from t where id=:1;
10g下可以使用sql profiles來改變與穩定計劃,而使用11G SPM如何實現呢?
建立測試例子:
--我這裡使用繫結變數是:1,不能像sqlplus那樣定義變數,必須寫一個匿名過程。
--可以發現執行計劃採用索引範圍掃描。
繼續上面的學習:
SQL> select * from v$version ;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
CORE 11.2.0.1.0 Production
TNS for Linux: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
create table t as select rownum id , cast(dbms_random.string('a',6 ) as varchar2(10)) name from dual connect by level <=1e5;
--分析表
BEGIN
SYS.DBMS_STATS.GATHER_TABLE_STATS (
OwnName => user
,TabName => 'T'
,Estimate_Percent => NULL
,Method_Opt => 'FOR ALL COLUMNS SIZE 1 '
,Degree => 4
,Cascade => TRUE
,No_Invalidate => TRUE);
END;
/
create index i_t_id on t(id);
1.假設程式中的程式碼寫成如下:
select /*+ full(t) */ * from t where id=:1;
10g下可以使用sql profiles來改變與穩定計劃,而使用11G SPM如何實現呢?
建立測試例子:
declare
begin
execute immediate 'select /*+ full(t) */ * from t where id=:1' using 100;
end;
/
--我這裡使用繫結變數是:1,不能像sqlplus那樣定義變數,必須寫一個匿名過程。
SQL> select * from table(dbms_xplan.display_cursor('1nhrs4ytd2fvq',NULL,'ALLSTATS LAST PEEKED_BINDS cost'));
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------
SQL_ID 1nhrs4ytd2fvq, child number 0
-------------------------------------
select /*+ full(t) */ * from t where id=:1
Plan hash value: 1601196873
--------------------------------------------------------
| Id | Operation | Name | E-Rows | Cost (%CPU)|
--------------------------------------------------------
| 0 | SELECT STATEMENT | | | 69 (100)|
|* 1 | TABLE ACCESS FULL| T | 1 | 69 (2)|
--------------------------------------------------------
Peeked Binds (identified by position):
--------------------------------------
1 - (NUMBER): 100
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("ID"=:1)
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--可以發現這樣的語句由於使用了提示,作為第3方無法修改sql程式碼?如何實現呢?
var v_basenum number;
exec :v_basenum:=dbms_spm.load_plans_from_cursor_cache(sql_id => '1nhrs4ytd2fvq',plan_hash_value =>1601196873 );
SQL> column signature format 9999999999999999999999
SQL> column FORCE_MATCHING_SIGNATURE like SIGNATURE
SQL> column EXACT_MATCHING_SIGNATURE like SIGNATURE
SQL> select sql_handle, plan_name, enabled, accepted,fixed,autopurge,origin,signature from dba_sql_plan_baselines ;
SQL_HANDLE PLAN_NAME ENA ACC FIX AUT ORIGIN SIGNATURE
------------------------------ ------------------------------ --- --- --- --- -------------- -----------------------
SYS_SQL_2a9db9f54b3cfa0c SQL_PLAN_2p7dtyp5mtyhc94ecae5c YES YES NO YES MANUAL-LOAD 3070814984116959756
SQL> column sql_text format a60
SQL> SELECT sql_id, sql_text, sql_plan_baseline, exact_matching_signature, force_matching_signature FROM v$sql WHERE sql_id = '1nhrs4ytd2fvq';
SQL_ID SQL_TEXT SQL_PLAN_BASELINE EXACT_MATCHING_SIGNATURE FORCE_MATCHING_SIGNATURE
------------- ------------------------------------------------------------ ------------------------------ ------------------------ ------------------------
1nhrs4ytd2fvq select /*+ full(t) */ * from t where id=:1 3070814984116959756 3070814984116959756
SQL> variable x number;
SQL> exec :x := 100;
PL/SQL procedure successfully completed.
SQL> select * from t where id=:x;
ID NAME
---------- ----------
100 lAGCjs
SQL> @dpc
PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------------
SQL_ID 89fczrmrx25j6, child number 0
-------------------------------------
select * from t where id=:x
Plan hash value: 4153437776
--------------------------------------------------------------------
| Id | Operation | Name | E-Rows | Cost (%CPU)|
--------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | 2 (100)|
| 1 | TABLE ACCESS BY INDEX ROWID| T | 1 | 2 (0)|
|* 2 | INDEX RANGE SCAN | I_T_ID | 1 | 1 (0)|
--------------------------------------------------------------------
Peeked Binds (identified by position):
--------------------------------------
1 - (NUMBER): 100
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("ID"=:X)
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> SELECT sql_id, sql_text, sql_plan_baseline, exact_matching_signature, force_matching_signature FROM v$sql WHERE sql_id in ('1nhrs4ytd2fvq','89fczrmrx25j6');
SQL_ID SQL_TEXT SQL_PLAN_BASELINE EXACT_MATCHING_SIGNATURE FORCE_MATCHING_SIGNATURE
------------- ------------------------------------------------------------ ------------------------------ ------------------------ ------------------------
89fczrmrx25j6 select * from t where id=:x 11842951964357158308 11842951964357158308
1nhrs4ytd2fvq select /*+ full(t) */ * from t where id=:1 3070814984116959756 3070814984116959756
exec :v_num:=dbms_spm.load_plans_from_cursor_cache(sql_id => '89fczrmrx25j6',plan_hash_value => 4153437776 ,sql_handle => 'SYS_SQL_2a9db9f54b3cfa0c');
SQL> exec :v_basenum:=dbms_spm.load_plans_from_cursor_cache(sql_id => '89fczrmrx25j6',plan_hash_value => 4153437776 ,sql_handle => 'SYS_SQL_2a9db9f54b3cfa0c');
PL/SQL procedure successfully completed.
SQL> select sql_handle, plan_name, enabled, accepted,fixed,autopurge,origin,signature from dba_sql_plan_baselines ;
SQL_HANDLE PLAN_NAME ENA ACC FIX AUT ORIGIN SIGNATURE
------------------------------ ------------------------------ --- --- --- --- -------------- -----------------------
SYS_SQL_2a9db9f54b3cfa0c SQL_PLAN_2p7dtyp5mtyhc0893a4b2 YES YES NO YES MANUAL-LOAD 3070814984116959756
SYS_SQL_2a9db9f54b3cfa0c SQL_PLAN_2p7dtyp5mtyhc94ecae5c YES YES NO YES MANUAL-LOAD 3070814984116959756
SQL> alter system flush shared_pool;
declare
begin
execute immediate 'select /*+ full(t) */ * from t where id=:1' using 100;
end;
/
SQL> select * from table(dbms_xplan.display_cursor('1nhrs4ytd2fvq',NULL,'ALLSTATS LAST PEEKED_BINDS cost'));
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------
SQL_ID 1nhrs4ytd2fvq, child number 0
-------------------------------------
select /*+ full(t) */ * from t where id=:1
Plan hash value: 1601196873
--------------------------------------------------------
| Id | Operation | Name | E-Rows | Cost (%CPU)|
--------------------------------------------------------
| 0 | SELECT STATEMENT | | | 69 (100)|
|* 1 | TABLE ACCESS FULL| T | 1 | 69 (2)|
--------------------------------------------------------
Peeked Binds (identified by position):
--------------------------------------
1 - (NUMBER): 100
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("ID"=:1)
Note
-----
- SQL plan baseline SQL_PLAN_2p7dtyp5mtyhc94ecae5c used for this statement--執行計劃並沒有選擇索引!
SQL> exec :v_basenum := dbms_spm.alter_sql_plan_baseline(sql_handle=>'SYS_SQL_2a9db9f54b3cfa0c',plan_name=>'SQL_PLAN_2p7dtyp5mtyhc94ecae5c',attribute_name=>'enabled',attribute_value=>'NO');
PL/SQL procedure successfully completed.
SQL> select sql_handle, plan_name, enabled, accepted,fixed,autopurge,origin,signature from dba_sql_plan_baselines ;
SQL_HANDLE PLAN_NAME ENA ACC FIX AUT ORIGIN SIGNATURE
------------------------------ ------------------------------ --- --- --- --- -------------- -----------------------
SYS_SQL_2a9db9f54b3cfa0c SQL_PLAN_2p7dtyp5mtyhc0893a4b2 YES YES NO YES MANUAL-LOAD 3070814984116959756
SYS_SQL_2a9db9f54b3cfa0c SQL_PLAN_2p7dtyp5mtyhc94ecae5c NO YES NO YES MANUAL-LOAD 3070814984116959756
alter system flush shared_pool;
declare
begin
execute immediate 'select /*+ full(t) */ * from t where id=:1' using 99;
end;
/
SQL> select * from table(dbms_xplan.display_cursor('1nhrs4ytd2fvq',NULL,'ALLSTATS LAST PEEKED_BINDS cost'));
PLAN_TABLE_OUTPUT
-------------------------------------------------------------------------------------------------------------
SQL_ID 1nhrs4ytd2fvq, child number 0
-------------------------------------
select /*+ full(t) */ * from t where id=:1
Plan hash value: 4153437776
--------------------------------------------------------------------
| Id | Operation | Name | E-Rows | Cost (%CPU)|
--------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | 2 (100)|
| 1 | TABLE ACCESS BY INDEX ROWID| T | 1 | 2 (0)|
|* 2 | INDEX RANGE SCAN | I_T_ID | 1 | 1 (0)|
--------------------------------------------------------------------
Peeked Binds (identified by position):
--------------------------------------
1 - (NUMBER): 100
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("ID"=:1)
Note
-----
- SQL plan baseline SQL_PLAN_2p7dtyp5mtyhc0893a4b2 used for this statement
--可以發現執行計劃採用索引範圍掃描。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-740217/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- [20120830]11G SPM的學習6.txt--第3方優化.txt優化
- [20120805]11G SPM的學習2.txt
- [20120806]11G SPM的學習4.txt
- [20120806]11G SPM的學習3.txt
- 效能優化的過程學習優化
- 深度學習中的優化方法(二)深度學習優化
- 深度學習中的優化方法(一)深度學習優化
- 我的MYSQL學習心得(16) : 優化MySql優化
- oracle 學習筆記---效能優化學習(1)Oracle筆記優化
- MySQL優化學習手札(三)MySql優化
- 斜率優化學習筆記優化筆記
- mysql left join 優化學習MySql優化
- mysql優化學習筆記MySql優化筆記
- 【AIX 學習】效能優化--vmstatAI優化
- 【AIX 學習】效能優化--sarAI優化
- 【AIX 學習】效能優化--topasAI優化
- oracle statpack優化學習(1)Oracle優化
- oracle statpack優化學習(2)Oracle優化
- 《java學習三》jvm效能優化-------調優JavaJVM優化
- Java學習之程式碼優化Java優化
- Mysql 優化(學習筆記二十)MySql優化筆記
- oracle 學習總結(效能優化)Oracle優化
- 【AIX 學習】效能優化--iostatAI優化iOS
- 使用spm繫結執行計劃來線上優化資料庫優化資料庫
- [譯]深度學習模型的簡單優化技巧深度學習模型優化
- 聯邦學習中的優化演算法聯邦學習優化演算法
- Oracle效能優化視訊學習筆記-效能優化概念(一)Oracle優化筆記
- Oracle效能優化視訊學習筆記-效能優化概念(二)Oracle優化筆記
- 深度學習 - 常用優化演算法深度學習優化演算法
- 深度學習運算元優化-FFT深度學習優化FFT
- 學習 Webpack5 之路(優化篇)Web優化
- 網路優化需要學習什麼優化
- MySQL優化學習筆記之索引MySql優化筆記索引
- MySQL優化學習筆記之explainMySql優化筆記AI
- Android卡頓優化學習筆記Android優化筆記
- HTTPS 效能優化學習筆記HTTP優化筆記
- ORACLE學習筆記--效能優化FAQ。Oracle筆記優化
- 【AIX 學習】效能優化--iostat (續)AI優化iOS