使用sql profile固定執行計劃
使用sql profile固定執行計劃實驗
10g之前有outlines,10g之後sql profile作為新特性之一出現。
如果針對非繫結變數的sql,outlines則力不從心。
下面是實驗過程
--1.準備階段
SQL> select * from v$version;
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
PL/SQL Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production
TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production
SQL> create table test_raugher as select * from dba_objects;
表已建立。
SQL> create index ind_objectid on test_raugher(object_id);
索引已建立。
SQL> select object_id from test_raugher where rownum<2;
OBJECT_ID
----------
20
SQL> exec dbms_stats.gather_table_stats(user,'TEST_RAUGHER',cascade=>true);
PL/SQL 過程已成功完成。
--原sql執行計劃
SQL> set autot trace explain
SQL> select * from test_raugher where object_id=20;
執行計劃
----------------------------------------------------------
Plan hash value: 800879874
--------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 95 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| TEST_RAUGHER | 1 | 95 | 2 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | IND_OBJECTID | 1 | | 1 (0)| 00:00:01 |
--------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("OBJECT_ID"=20)
SQL>
--新sql執行計劃
SQL> select /*+ full(test_raugher) */ * from test_raugher where object_id=20;
執行計劃
----------------------------------------------------------
Plan hash value: 3725671026
----------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 95 | 166 (2)| 00:00:02 |
|* 1 | TABLE ACCESS FULL| TEST_RAUGHER | 1 | 95 | 166 (2)| 00:00:02 |
----------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("OBJECT_ID"=20)
--2.獲取新sql的sql_id
SQL> col sql_id for a20
SQL> col sql_text for a100
SQL> select sql_id,sql_text from v$sql where sql_text like '%full(test_raugher)%';
SQL_ID SQL_TEXT
-------------------- ----------------------------------------------------------------------------------------------------
5nkhk378705z3 select sql_id,sql_text from v$sql where sql_text like '%full(test_raugher)%'
g23hbdmcsdahc select /*+ full(test_raugher) */ * from test_raugher where object_id=20
dqp79vx5pmw0k EXPLAIN PLAN SET STATEMENT_ID='PLUS4294967295' FOR select /*+ full(test_raugher) */ * from test_raug
her where object_id=20
--3.獲取新sql的outline
SQL> set pagesize 1000
SQL> select * from table(dbms_xplan.display_cursor('g23hbdmcsdahc',null,'outline'));
PLAN_TABLE_OUTPUT
-----------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------
SQL_ID g23hbdmcsdahc, child number 0
-------------------------------------
select /*+ full(test_raugher) */ * from test_raugher where object_id=20
Plan hash value: 3725671026
----------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 166 (100)| |
|* 1 | TABLE ACCESS FULL| TEST_RAUGHER | 1 | 95 | 166 (2)| 00:00:02 |
----------------------------------------------------------------------------------
Outline Data
-------------
/*+
BEGIN_OUTLINE_DATA
IGNORE_OPTIM_EMBEDDED_HINTS
OPTIMIZER_FEATURES_ENABLE('10.2.0.1')
ALL_ROWS
OUTLINE_LEAF(@"SEL$1")
FULL(@"SEL$1" "TEST_RAUGHER"@"SEL$1")
END_OUTLINE_DATA
*/
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("OBJECT_ID"=20)
已選擇31行。
--4.建立sql profile(SQLPROFILE_001)
SQL> declare
2 v_hints sys.sqlprof_attr;
3 begin
4 v_hints:=sys.sqlprof_attr(
5 'BEGIN_OUTLINE_DATA',
6 'IGNORE_OPTIM_EMBEDDED_HINTS',
7 'OPTIMIZER_FEATURES_ENABLE(''10.2.0.1'')',
8 'ALL_ROWS',
9 'OUTLINE_LEAF(@"SEL$1")',
10 'FULL(@"SEL$1" "TEST_RAUGHER"@"SEL$1")',
11 'END_OUTLINE_DATA');
12 dbms_sqltune.import_sql_profile(
13 'select * from test_raugher where object_id=20',
14 v_hints,'SQLPROFILE_001',
15 force_match=>true,replace=>false);
16 end;
17 /
PL/SQL 過程已成功完成。
--5.檢視是否使用sql profile
SQL> set autot trace explain
SQL> select * from test_raugher where object_id=20;
執行計劃
----------------------------------------------------------
Plan hash value: 3725671026
----------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 95 | 166 (2)| 00:00:02 |
|* 1 | TABLE ACCESS FULL| TEST_RAUGHER | 1 | 95 | 166 (2)| 00:00:02 |
----------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("OBJECT_ID"=20)
Note
-----
- SQL profile "SQLPROFILE_001" used for this statement
SQL> select * from test_raugher where object_id=200;
執行計劃
----------------------------------------------------------
Plan hash value: 3725671026
----------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 95 | 166 (2)| 00:00:02 |
|* 1 | TABLE ACCESS FULL| TEST_RAUGHER | 1 | 95 | 166 (2)| 00:00:02 |
----------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("OBJECT_ID"=200)
Note
-----
- SQL profile "SQLPROFILE_001" used for this statement
建立sql profile
DBMS_SQLTUNE.IMPORT_SQL_PROFILE(
sql_text => 'FULL QUERY TEXT',
profile => sqlprof_attr('HINT SPECIFICATION WITH FULL OBJECT ALIASES'),
name => 'PROFILE NAME',
force_match => TRUE/FALSE,
replace=> TRUE/FALSE);
sql_text用於指定sql的全文字,可查詢V$SQLAREA.SQL_FULLTEXT或DBA_HIST_SQLTEXT.SQL_TEXT獲得。
刪除sql proflie
BEGIN
DBMS_SQLTUNE.DROP_SQL_PROFILE(name => 'PROFILE NAME');
END;
/
sql profile相關檢視
SELECT name, created,category,sql_Text from dba_sql_profiles ORDER BY created DESC;
SELECT sql_attr.attr_val outline_hints
FROM dba_sql_profiles sql_profiles, sys.SQLPROF$ATTR sql_attr
WHERE sql_profiles.signature = sql_attr.signature
AND sql_profiles.name = 'SQLPROFILE_001'
10g之前有outlines,10g之後sql profile作為新特性之一出現。
如果針對非繫結變數的sql,outlines則力不從心。
下面是實驗過程
--1.準備階段
SQL> select * from v$version;
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
PL/SQL Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production
TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production
SQL> create table test_raugher as select * from dba_objects;
表已建立。
SQL> create index ind_objectid on test_raugher(object_id);
索引已建立。
SQL> select object_id from test_raugher where rownum<2;
OBJECT_ID
----------
20
SQL> exec dbms_stats.gather_table_stats(user,'TEST_RAUGHER',cascade=>true);
PL/SQL 過程已成功完成。
--原sql執行計劃
SQL> set autot trace explain
SQL> select * from test_raugher where object_id=20;
執行計劃
----------------------------------------------------------
Plan hash value: 800879874
--------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 95 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| TEST_RAUGHER | 1 | 95 | 2 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | IND_OBJECTID | 1 | | 1 (0)| 00:00:01 |
--------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("OBJECT_ID"=20)
SQL>
--新sql執行計劃
SQL> select /*+ full(test_raugher) */ * from test_raugher where object_id=20;
執行計劃
----------------------------------------------------------
Plan hash value: 3725671026
----------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 95 | 166 (2)| 00:00:02 |
|* 1 | TABLE ACCESS FULL| TEST_RAUGHER | 1 | 95 | 166 (2)| 00:00:02 |
----------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("OBJECT_ID"=20)
--2.獲取新sql的sql_id
SQL> col sql_id for a20
SQL> col sql_text for a100
SQL> select sql_id,sql_text from v$sql where sql_text like '%full(test_raugher)%';
SQL_ID SQL_TEXT
-------------------- ----------------------------------------------------------------------------------------------------
5nkhk378705z3 select sql_id,sql_text from v$sql where sql_text like '%full(test_raugher)%'
g23hbdmcsdahc select /*+ full(test_raugher) */ * from test_raugher where object_id=20
dqp79vx5pmw0k EXPLAIN PLAN SET STATEMENT_ID='PLUS4294967295' FOR select /*+ full(test_raugher) */ * from test_raug
her where object_id=20
--3.獲取新sql的outline
SQL> set pagesize 1000
SQL> select * from table(dbms_xplan.display_cursor('g23hbdmcsdahc',null,'outline'));
PLAN_TABLE_OUTPUT
-----------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------
SQL_ID g23hbdmcsdahc, child number 0
-------------------------------------
select /*+ full(test_raugher) */ * from test_raugher where object_id=20
Plan hash value: 3725671026
----------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 166 (100)| |
|* 1 | TABLE ACCESS FULL| TEST_RAUGHER | 1 | 95 | 166 (2)| 00:00:02 |
----------------------------------------------------------------------------------
Outline Data
-------------
/*+
BEGIN_OUTLINE_DATA
IGNORE_OPTIM_EMBEDDED_HINTS
OPTIMIZER_FEATURES_ENABLE('10.2.0.1')
ALL_ROWS
OUTLINE_LEAF(@"SEL$1")
FULL(@"SEL$1" "TEST_RAUGHER"@"SEL$1")
END_OUTLINE_DATA
*/
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("OBJECT_ID"=20)
已選擇31行。
--4.建立sql profile(SQLPROFILE_001)
SQL> declare
2 v_hints sys.sqlprof_attr;
3 begin
4 v_hints:=sys.sqlprof_attr(
5 'BEGIN_OUTLINE_DATA',
6 'IGNORE_OPTIM_EMBEDDED_HINTS',
7 'OPTIMIZER_FEATURES_ENABLE(''10.2.0.1'')',
8 'ALL_ROWS',
9 'OUTLINE_LEAF(@"SEL$1")',
10 'FULL(@"SEL$1" "TEST_RAUGHER"@"SEL$1")',
11 'END_OUTLINE_DATA');
12 dbms_sqltune.import_sql_profile(
13 'select * from test_raugher where object_id=20',
14 v_hints,'SQLPROFILE_001',
15 force_match=>true,replace=>false);
16 end;
17 /
PL/SQL 過程已成功完成。
--5.檢視是否使用sql profile
SQL> set autot trace explain
SQL> select * from test_raugher where object_id=20;
執行計劃
----------------------------------------------------------
Plan hash value: 3725671026
----------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 95 | 166 (2)| 00:00:02 |
|* 1 | TABLE ACCESS FULL| TEST_RAUGHER | 1 | 95 | 166 (2)| 00:00:02 |
----------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("OBJECT_ID"=20)
Note
-----
- SQL profile "SQLPROFILE_001" used for this statement
SQL> select * from test_raugher where object_id=200;
執行計劃
----------------------------------------------------------
Plan hash value: 3725671026
----------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 95 | 166 (2)| 00:00:02 |
|* 1 | TABLE ACCESS FULL| TEST_RAUGHER | 1 | 95 | 166 (2)| 00:00:02 |
----------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("OBJECT_ID"=200)
Note
-----
- SQL profile "SQLPROFILE_001" used for this statement
建立sql profile
DBMS_SQLTUNE.IMPORT_SQL_PROFILE(
sql_text => 'FULL QUERY TEXT',
profile => sqlprof_attr('HINT SPECIFICATION WITH FULL OBJECT ALIASES'),
name => 'PROFILE NAME',
force_match => TRUE/FALSE,
replace=> TRUE/FALSE);
sql_text用於指定sql的全文字,可查詢V$SQLAREA.SQL_FULLTEXT或DBA_HIST_SQLTEXT.SQL_TEXT獲得。
刪除sql proflie
BEGIN
DBMS_SQLTUNE.DROP_SQL_PROFILE(name => 'PROFILE NAME');
END;
/
sql profile相關檢視
SELECT name, created,category,sql_Text from dba_sql_profiles ORDER BY created DESC;
SELECT sql_attr.attr_val outline_hints
FROM dba_sql_profiles sql_profiles, sys.SQLPROF$ATTR sql_attr
WHERE sql_profiles.signature = sql_attr.signature
AND sql_profiles.name = 'SQLPROFILE_001'
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/25462274/viewspace-2102542/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- SQL PROFILE修改固定執行計劃SQL
- 使用coe_xfr_sql_profile固定執行計劃SQL
- 用sql profile來固定執行計劃SQL
- Oracle SQL Profile固定執行計劃的方法OracleSQL
- 使用SQL Profile及SQL Tuning Advisor固定執行計劃SQL
- .Oracle固定執行計劃之SQL PROFILE概要檔案OracleSQL
- 使用SQL PROFILE 給出合理的執行計劃SQL
- 控制執行計劃之-SQL Profile(一)SQL
- zt_sql baseline_sql profile_sql outline修改固定執行計劃SQL
- Oracle優化案例-coe_xfr_sql_profile固定執行計劃與刪除profile(二十五)Oracle優化SQL
- baseline固定SQL執行計劃SQL
- Oracle手動固定SQL執行計劃OracleSQL
- SQL BASELINE修改固定執行計劃SQL
- 使用OUTLINE固定執行計劃
- oracle 固定執行計劃Oracle
- Oracle優化案例-view merge與coe_load_sql_profile固定執行計劃(十五)Oracle優化ViewSQL
- 使用SPM和STA進行固定執行計劃
- 利用coe_xfr_sql_profile 改變執行計劃SQL
- oracle固定執行計劃--sqlprofileOracleSQL
- oracle sqlprofile 固定執行計劃,並遷移執行計劃OracleSQL
- Oracle SQL_Profile手動生成及繫結sql執行計劃OracleSQL
- oracle使用outline固定執行計劃事例Oracle
- 【SPM】Oracle如何固定執行計劃Oracle
- sql 執行計劃SQL
- 使用PL/SQL檢視執行計劃SQL
- 影響執行計劃之oracle sql baseline與sql profile之互動OracleSQL
- Oracle利用coe_load_sql_profile指令碼繫結執行計劃OracleSQL指令碼
- Oracle緊急固定執行計劃之手段Oracle
- 用outline修改固定執行計劃
- Oracle sql執行計劃OracleSQL
- SQL Server執行計劃SQLServer
- SQL的執行計劃SQL
- SQL執行計劃分析SQL
- 【sql調優之執行計劃】獲取執行計劃SQL
- 獲取SQL執行計劃SQL
- SQL 執行計劃案例1SQL
- 剖析SQL Server執行計劃SQLServer
- 檢視sql執行計劃SQL