sql最佳化:使用儲存提綱穩定sql執行計劃
sql最佳化:使用儲存提綱穩定sql執行計劃
0、建立測試表
create table t as select * from dba_objects;
create index id_inx on t (object_id)
1、建立儲存提綱
create or replace outline test_outline for category test on select * from t where object_id=6284; ---建立
select * from user_outlines; --檢視
select dbms_lob.substr(hint) from user_outline_hints where name='TEST_OUTLINE'; --檢視提示
Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 Connected as system SQL> create or replace outline test_outline for category test on select * from t where object_id=6284; Outline created SQL> set linesize 400 SQL> desc user_outlines; Name Type Nullable Default Comments ---------- ------------ -------- ------- -------------------------------------------------------------------------- NAME VARCHAR2(30) Y Name of the outline CATEGORY VARCHAR2(30) Y Category to which the outline belongs USED VARCHAR2(6) Y Flag indicating whether the outline has ever been used TIMESTAMP DATE Y Timestamp at which the outline was created VERSION VARCHAR2(64) Y Oracle Version that created the outline SQL_TEXT LONG Y SQL text of the query SIGNATURE RAW(16) Y Signature uniquely identifying the outline SQL text COMPATIBLE VARCHAR2(12) Y Flag indicating whether the outline hints were compatible across migration ENABLED VARCHAR2(8) Y Flag indicating whether the outline is enabled FORMAT VARCHAR2(6) Y Flag indicating what hint format is used MIGRATED VARCHAR2(12) Y Flag indicating whether the outline has been migrated to SQL plan baseline SQL> select name,sql_text from user_outlines where name='TEST_OUTLINE'; NAME SQL_TEXT ------------------------------ -------------------------------------------------------------------------------- TEST_OUTLINE select * from t where object_id=6284 SQL> SQL> select dbms_lob.substr(hint) from user_outline_hints where name='TEST_OUTLINE'; DBMS_LOB.SUBSTR(HINT) -------------------------------------------------------------------------------- INDEX_RS_ASC(@"SEL$1" "T"@"SEL$1" ("T"."OBJECT_ID")) OUTLINE_LEAF(@"SEL$1") ALL_ROWS DB_VERSION('11.2.0.1') OPTIMIZER_FEATURES_ENABLE('11.2.0.1') IGNORE_OPTIM_EMBEDDED_HINTS 6 rows selected SQL> |
2、更改儲存提綱名稱
alter outline TEST_OUTLINE rename to test1;
3、啟用儲存提綱
啟用儲存提綱必須要有兩個條件,
1)是儲存提綱必須是enable狀態,
如:
alter outlineTEST_OUTLINE enable;
2)是透過初始化引數use_stored_outlines在系統級或者會話級設定為true或者類別名
如:
alter system set use_stored_outlines=TEST_OUTLINE; ---test為類別名
alter system set use_stored_outlines=true;
4、編輯儲存提綱
修改一個儲存提綱只能透過修改私有儲存提綱完成。
1)建立一個私有儲存提綱
explain plan for select * from t where object_id=6284;
select * from table(dbms_xplan.display(null,null,'ALL'));
SQL> explain plan for select * from t where object_id=6284; Explained SQL> select * from table(dbms_xplan.display(null,null,'ALL')); PLAN_TABLE_OUTPUT -------------------------------------------------------------------------------- Plan hash value: 801830182 -------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time -------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 207 | 2 (0)| 00:0 | 1 | TABLE ACCESS BY INDEX ROWID| T | 1 | 207 | 2 (0)| 00:0 |* 2 | INDEX RANGE SCAN | ID_INX | 1 | | 1 (0)| 00:0 -------------------------------------------------------------------------------- Query Block Name / Object Alias (identified by operation id): ------------------------------------------------------------- 1 - SEL$1 / T@SEL$1 2 - SEL$1 / T@SEL$1 Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("OBJECT_ID"=6284) PLAN_TABLE_OUTPUT -------------------------------------------------------------------------------- Column Projection Information (identified by operation id): ----------------------------------------------------------- 1 - "T"."OWNER"[VARCHAR2,30], "T"."OBJECT_NAME"[VARCHAR2,128], "T"."SUBOBJECT_NAME"[VARCHAR2,30], "OBJECT_ID"[NUMBER,22], "T"."DATA_OBJECT_ID"[NUMBER,22], "T"."OBJECT_TYPE"[VARCHAR2,19], "T"."CREATED"[DATE,7], "T"."LAST_DDL_TIME"[DATE,7], "T"."TIMESTAMP"[VARCHAR2,19], "T"."STATUS"[VARCHAR2,7], "T"."TEMPORARY"[VARCHAR2,1], "T"."GENERATED"[VARCHAR2,1], "T"."SECONDARY"[VARCHAR2,1], "T"."NAMESPACE"[NUMBER,22], "T"."EDITION_NAME"[VARCHAR2,30] 2 - "T".ROWID[ROWID,10], "OBJECT_ID"[NUMBER,22] Note ----- - dynamic sampling used for this statement (level=2) 37 rows selected SQL> |
建立儲存提綱
create or replace private outline p_outline_editing
on select * from t where object_id=6284;
SQL> SQL> create or replace private outline p_outline_editing on select * from t where object_id=6284; Outline created SQL> |
檢視私有儲存提綱
select hint#,hint_text from ol$hints;
SQL> SQL> SQL> select hint#,hint_text from ol$hints; HINT# HINT_TEXT ---------- -------------------------------------------------------------------------------- 1 INDEX_RS_ASC(@"SEL$1" "T"@"SEL$1" ("T"."OBJECT_ID")) 2 OUTLINE_LEAF(@"SEL$1") 3 ALL_ROWS 4 DB_VERSION('11.2.0.1') 5 OPTIMIZER_FEATURES_ENABLE('11.2.0.1') 6 IGNORE_OPTIM_EMBEDDED_HINTS 6 rows selected SQL> |
2)編輯私有儲存提綱
update ol$hints set hint_text='FULL(@SEL$1 T)' where hint#=1; --修改為全表掃描
commit;
select * from ol$hints;
SQL>
SQL> update ol$hints set hint_text='FULL(@SEL$1 T)' where hint#=1;
1 row updated
SQL> commit;
Commit complete
SQL> select hint_text from ol$hints;
HINT_TEXT
--------------------------------------------------------------------------------
FULL(@SEL$1 T)
OUTLINE_LEAF(@"SEL$1")
ALL_ROWS
DB_VERSION('11.2.0.1')
OPTIMIZER_FEATURES_ENABLE('11.2.0.1')
IGNORE_OPTIM_EMBEDDED_HINTS
6 rows selected
SQL>
3) 同步私有儲存提綱
execute dbms_outln_edit.refresh_private_outline('P_OUTLINE_EDITING');
|
4)測試私有提綱
alter session set use_private_outlines=TRUE;
explain plan for select * from t where object_id=6284;
select * from table(dbms_xplan.display(null,null,'ALL'));
SQL> alter session set use_private_outlines=TRUE 2 ; Session altered SQL> SQL> explain plan for select * from t where object_id=6284; Explained SQL> select * from table(dbms_xplan.display(null,null,'ALL')); PLAN_TABLE_OUTPUT -------------------------------------------------------------------------------- Plan hash value: 1601196873 -------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 837 | 169K| 281 (1)| 00:00:04 | |* 1 | TABLE ACCESS FULL| T | 837 | 169K| 281 (1)| 00:00:04 | -------------------------------------------------------------------------- Query Block Name / Object Alias (identified by operation id): ------------------------------------------------------------- 1 - SEL$1 / T@SEL$1 Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter("OBJECT_ID"=6284) Column Projection Information (identified by operation id): PLAN_TABLE_OUTPUT -------------------------------------------------------------------------------- ----------------------------------------------------------- 1 - "T"."OWNER"[VARCHAR2,30], "T"."OBJECT_NAME"[VARCHAR2,128], "T"."SUBOBJECT_NAME"[VARCHAR2,30], "OBJECT_ID"[NUMBER,22], "T"."DATA_OBJECT_ID"[NUMBER,22], "T"."OBJECT_TYPE"[VARCHAR2,19], "T"."CREATED"[DATE,7], "T"."LAST_DDL_TIME"[DATE,7], "T"."TIMESTAMP"[VARCHAR2,19], "T"."STATUS"[VARCHAR2,7], "T"."TEMPORARY"[VARCHAR2,1], "T"."GENERATED"[VARCHAR2,1], "T"."SECONDARY"[VARCHAR2,1], "T"."NAMESPACE"[NUMBER,22], "T"."EDITION_NAME"[VARCHAR2,30] Note ----- - outline "P_OUTLINE_EDITING" used for this statement 34 rows selected |
5)釋出私有提綱
create public outline P_OUTLINE_EDITING_T from private P_OUTLINE_EDITING;
SQL> create public outline P_OUTLINE_EDITING_T from private P_OUTLINE_EDITING; Outline created SQL> SQL> |
5、刪除儲存提綱
刪除一個儲存提綱
drop outline P_OUTLINE_EDITING_T ;
刪除一個列別儲存提綱
dbms_outln>drop_by_cat(cat=>'P_OUTLINE_EDITING_T ');
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/27571661/viewspace-1787135/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 儲存過程中檢視sql執行計劃的方法儲存過程SQL
- SQL最佳化 —— 讀懂執行計劃SQL
- 通過鎖定表的統計資訊來穩定sql的執行計劃SQL
- sql 執行計劃SQL
- 使用sql profile固定執行計劃SQL
- [MSSQL]sql 儲存過程定時執行方法SQL儲存過程
- SQL最佳化案例-從執行計劃定位SQL問題(三)SQL
- Oracle sql執行計劃OracleSQL
- SQL Server執行計劃SQLServer
- SQL的執行計劃SQL
- SQL執行計劃分析SQL
- 使用PL/SQL檢視執行計劃SQL
- spark sql語句效能最佳化及執行計劃SparkSQL
- 透過分析SQL語句的執行計劃最佳化SQL(總結)SQL
- 獲取SQL執行計劃SQL
- SQL 執行計劃案例1SQL
- 剖析SQL Server執行計劃SQLServer
- 檢視sql執行計劃SQL
- [轉]透過分析SQL語句的執行計劃最佳化SQL(總結)SQL
- 【sql調優之執行計劃】獲取執行計劃SQL
- 決定一個SQL執行效率的是執行計劃, 而不是SQL的寫法SQL
- 微課sql最佳化(9)、如何獲取執行計劃SQL
- 微課sql最佳化(11) 、如何檢視執行計劃SQL
- 透過空間佔用和執行計劃瞭解SQL Server的行儲存索引SQLServer索引
- 執行計劃不穩定的原因分析
- oracle動態sql執行table表中儲存的sqlOracleSQL
- 使用coe_xfr_sql_profile固定執行計劃SQL
- 使用SQL PROFILE 給出合理的執行計劃SQL
- 使用leading(,)優化sql執行計劃優化SQL
- [20120104]穩定一條sql語句的執行計劃.txtSQL
- 使用SQL Profile及SQL Tuning Advisor固定執行計劃SQL
- sql執行計劃是否改變SQL
- sql的執行計劃 詳解SQL
- sql執行計劃基本命令SQL
- SQL PROFILE修改固定執行計劃SQL
- 【TUNE_ORACLE】定製化執行計劃SQL參考OracleSQL
- oracle儲存提綱(stored outline)Oracle
- 穩定PG SQL的執行速度SQL