## 2020-06-04 測試 SQL PROFILE 。
## 步驟
## 1 創建SQL Tuning Task
## 2 執行SQL Tuning Task
## 3 查看SQL Tuning進度
## 4 查看SQL Tuning結果
## 5 接受SQL Tuning給的SQL PROFILE建議
--創建測試表t1
conn hr/hr
create table t1 (n number);
declare
begin
for i in 1..10000
loop
insert into t1 values(i);
commit;
end loop;
end;
select count(*) from t1;
##HR@testogg>create table t1 (n number);
##
##Table created.
##
##HR@testogg>declare
##b 2 egin
##f 3 or i in 1..10000
##l 4 oop
##i 5 nsert into t1 values(i);
##c 6 ommit;
##e 7 nd loop;
##e 8 nd;
## 9 /
##
##PL/SQL procedure successfully completed.
##
##HR@testogg>select count(*) from t1;
##
## COUNT(*)
##----------
## 10000
--在t1列N上創建索引idx_t1_n
create index idx_t1_n on t1(n);
##HR@testogg>create index idx_t1_n on t1(n);
##
##Index created.
##
##HR@testogg>
--收集t1的統計信息
exec dbms_stats.gather_table_stats('HR','T1',method_opt=>'for all columns size 1',cascade=>true);
##HR@testogg>exec dbms_stats.gather_table_stats('HR','T1',method_opt=>'for all columns size 1',cascade=>true);
##
##PL/SQL procedure successfully completed.
##
##HR@testogg>
-- 強制不使用索引,目的是模擬錯誤的執行計劃
select /*+ no_index(t1 idx_t1_n) */ * from t1 where n=1;
select * from table (dbms_xplan.display_cursor(null,null,'advanced'));
##HR@testogg>select /*+ no_index(t1 idx_t1_n) */ * from t1 where n=1;
##
## N
##----------
## 1
##
##HR@testogg>select * from table (dbms_xplan.display_cursor(null,null,'advanced'));
##
##PLAN_TABLE_OUTPUT
##--------------------------------------------------------------------------------
##SQL_ID d1nuwfmgy6593, child number 0
##-------------------------------------
##select /*+ no_index(t1 idx_t1_n) */ * from t1 where n=1
##
##Plan hash value: 3617692013
##
##--------------------------------------------------------------------------
##| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
##--------------------------------------------------------------------------
##| 0 | SELECT STATEMENT | | | | 7 (100)| |
##|* 1 | TABLE ACCESS FULL| T1 | 1 | 4 | 7 (0)| 00:00:01 |
##
##PLAN_TABLE_OUTPUT
##--------------------------------------------------------------------------------
##--------------------------------------------------------------------------
##
##Query Block Name / Object Alias (identified by operation id):
##-------------------------------------------------------------
##
## 1 - SEL$1 / T1@SEL$1
##
##Outline Data
##-------------
##
## /*+
##
##PLAN_TABLE_OUTPUT
##--------------------------------------------------------------------------------
## BEGIN_OUTLINE_DATA
## IGNORE_OPTIM_EMBEDDED_HINTS
## OPTIMIZER_FEATURES_ENABLE('11.2.0.4')
## DB_VERSION('11.2.0.4')
## ALL_ROWS
## OUTLINE_LEAF(@"SEL$1")
## FULL(@"SEL$1" "T1"@"SEL$1")
## END_OUTLINE_DATA
## */
##
##Predicate Information (identified by operation id):
##
##PLAN_TABLE_OUTPUT
##--------------------------------------------------------------------------------
##---------------------------------------------------
##
## 1 - filter("N"=1)
##
##Column Projection Information (identified by operation id):
##-----------------------------------------------------------
##
## 1 - "N"[NUMBER,22]
##
##
##42 rows selected.
##
##HR@testogg>
--使用sql tuning 來對sql 調優,生成sql profile。創建任務。
declare
my_task_name varchar2(30);
my_sqltext clob;
begin
my_sqltext:='select /*+ no_index(t1 idx_t1_n) */ * from t1 where n=1';
my_task_name:=dbms_sqltune.create_tuning_task(
sql_text => my_sqltext,
user_name=>'HR',
scope=>'COMPREHENSIVE',
time_limit=>60,
task_name=>'my_sql_tuning_task20200604',
description=>'task to tune a query on table t1'
);
end;
##HR@testogg>declare
## 2 my_task_name varchar2(30);
##my 3 _sqltext clob;
##b 4 egin
##m 5 y_sqltext:='select /*+ no_index(t1 idx_t1_n) */ * from t1 where n=1';
## 6 my_task_name:=dbms_sqltune.create_tuning_task(
## 7 sql_text => my_sqltext,
##u 8 ser_name=>'HR',
##sco 9 pe=>'COMPREHENSIVE',
## 10 time_limit=>60,
##ta 11 sk_name=>'my_sql_tuning_task20200604',
##d 12 escription=>'task to tune a query on table t1'
##); 13
##en 14 d;
## 15 /
##
##PL/SQL procedure successfully completed.
##
##HR@testogg>
-- 進行SQL Tuning
exec dbms_sqltune.execute_tuning_task(task_name=>'my_sql_tuning_task20200604');
##HR@testogg>exec dbms_sqltune.execute_tuning_task(task_name=>'my_sql_tuning_task20200604');
##
##PL/SQL procedure successfully completed.
-- 查看
SELECT task_name,status FROM USER_ADVISOR_TASKS where task_name like '%20200604%'
##HR@testogg>SELECT task_name,status FROM USER_ADVISOR_TASKS where task_name like '%20200604%';
##
##TASK_NAME STATUS
##------------------------------ -----------
##my_sql_tuning_task20200604 INITIAL
HR@testogg>
##HR@testogg>SELECT task_name,status FROM USER_ADVISOR_TASKS where task_name like '%20200604%';
##
##TASK_NAME STATUS
##------------------------------ -----------
##my_sql_tuning_task20200604 COMPLETED
##
##HR@testogg>
-- 查看Tuning的結果
set LONG 9999
set longchunksize 1000
set linesize 800
select dbms_sqltune.report_tuning_task('my_sql_tuning_task20200604') from dual;
##HR@testogg>select dbms_sqltune.report_tuning_task('my_sql_tuning_task20200604') from dual;
##
##DBMS_SQLTUNE.REPORT_TUNING_TASK('MY_SQL_TUNING_TASK20200604')
##------------------------------------------------------------------------------------------------------------------------
##GENERAL INFORMATION SECTION
##-------------------------------------------------------------------------------
##Tuning Task Name : my_sql_tuning_task20200604
##Tuning Task Owner : HR
##Workload Type : Single SQL Statement
##Scope : COMPREHENSIVE
##Time Limit(seconds): 60
##Completion Status : COMPLETED
##Started at : 06/04/2020 07:56:50
##Completed at : 06/04/2020 07:56:51
##
##
##DBMS_SQLTUNE.REPORT_TUNING_TASK('MY_SQL_TUNING_TASK20200604')
##------------------------------------------------------------------------------------------------------------------------
##-------------------------------------------------------------------------------
##Schema Name: HR
##SQL ID : 2cpdzf2ukjdfs
##SQL Text : select /*+ no_index(t1 idx_t1_n) */ * from t1 where n=1
##
##-------------------------------------------------------------------------------
##FINDINGS SECTION (1 finding)
##-------------------------------------------------------------------------------
##
##1- SQL Profile Finding (see explain plans section below)
##--------------------------------------------------------
##
##DBMS_SQLTUNE.REPORT_TUNING_TASK('MY_SQL_TUNING_TASK20200604')
##------------------------------------------------------------------------------------------------------------------------
## A potentially better execution plan was found for this statement.
##
## Recommendation (estimated benefit: 90.9%)
## -----------------------------------------
## - Consider accepting the recommended SQL profile.
## execute dbms_sqltune.accept_sql_profile(task_name =>
## 'my_sql_tuning_task20200604', task_owner => 'HR', replace =>
## TRUE);
##
## Validation results
## ------------------
##
##DBMS_SQLTUNE.REPORT_TUNING_TASK('MY_SQL_TUNING_TASK20200604')
##------------------------------------------------------------------------------------------------------------------------
## The SQL profile was tested by executing both its plan and the original plan
## and measuring their respective execution statistics. A plan may have been
## only partially executed if the other could be run to completion in less time.
##
## Original Plan With SQL Profile % Improved
## ------------- ---------------- ----------
## Completion Status: COMPLETE COMPLETE
## Elapsed Time (s): .000217 .000023 89.4 %
## CPU Time (s): .000216 .000023 89.35 %
## User I/O Time (s): 0 0
## Buffer Gets: 22 2 90.9 %
##
##DBMS_SQLTUNE.REPORT_TUNING_TASK('MY_SQL_TUNING_TASK20200604')
##------------------------------------------------------------------------------------------------------------------------
## Physical Read Requests: 0 0
## Physical Write Requests: 0 0
## Physical Read Bytes: 0 0
## Physical Write Bytes: 0 0
## Rows Processed: 1 1
## Fetches: 1 1
## Executions: 1 1
##
## Notes
## -----
## 1. Statistics for the original plan were averaged over 10 executions.
##
##DBMS_SQLTUNE.REPORT_TUNING_TASK('MY_SQL_TUNING_TASK20200604')
##------------------------------------------------------------------------------------------------------------------------
## 2. Statistics for the SQL profile plan were averaged over 10 executions.
##
##-------------------------------------------------------------------------------
##EXPLAIN PLANS SECTION
##-------------------------------------------------------------------------------
##
##1- Original With Adjusted Cost
##------------------------------
##Plan hash value: 3617692013
##
##--------------------------------------------------------------------------
##
##DBMS_SQLTUNE.REPORT_TUNING_TASK('MY_SQL_TUNING_TASK20200604')
##------------------------------------------------------------------------------------------------------------------------
##| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
##--------------------------------------------------------------------------
##| 0 | SELECT STATEMENT | | 1 | 4 | 7 (0)| 00:00:01 |
##|* 1 | TABLE ACCESS FULL| T1 | 1 | 4 | 7 (0)| 00:00:01 |
##--------------------------------------------------------------------------
##
##Predicate Information (identified by operation id):
##---------------------------------------------------
##
## 1 - filter("N"=1)
##
##
##DBMS_SQLTUNE.REPORT_TUNING_TASK('MY_SQL_TUNING_TASK20200604')
##------------------------------------------------------------------------------------------------------------------------
##2- Using SQL Profile
##--------------------
##Plan hash value: 3270407578
##
##-----------------------------------------------------------------------------
##| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
##-----------------------------------------------------------------------------
##| 0 | SELECT STATEMENT | | 1 | 4 | 1 (0)| 00:00:01 |
##|* 1 | INDEX RANGE SCAN| IDX_T1_N | 1 | 4 | 1 (0)| 00:00:01 |
##-----------------------------------------------------------------------------
##
##
##DBMS_SQLTUNE.REPORT_TUNING_TASK('MY_SQL_TUNING_TASK20200604')
##------------------------------------------------------------------------------------------------------------------------
##Predicate Information (identified by operation id):
##---------------------------------------------------
##
## 1 - access("N"=1)
##
##-------------------------------------------------------------------------------
##
##
##HR@testogg>
-- 接受SQL Profile
exec dbms_sqltune.accept_sql_profile(task_name=>'my_sql_tuning_task20200604',task_owner=>'HR',replace=>true);
##HR@testogg>exec dbms_sqltune.accept_sql_profile(task_name=>'my_sql_tuning_task20200604',task_owner=>'HR',replace=>true);
##
##PL/SQL procedure successfully completed.
##
##HR@testogg>
-- 查看效果,可以看到,使用到了索引,並且使用到了profile SQL profile SYS_SQLPROF_01727ca165b20000 used for this statement
select /*+ no_index(t1 idx_t1_n) */ * from t1 where n=1;
select * from table (dbms_xplan.display_cursor(null,null,'advanced'));
##HR@testogg>select /*+ no_index(t1 idx_t1_n) */ * from t1 where n=1;
##
## N
##----------
## 1
##
##HR@testogg>select * from table (dbms_xplan.display_cursor(null,null,'advanced'));
##
##PLAN_TABLE_OUTPUT
##------------------------------------------------------------------------------------------------------------------------
##SQL_ID d1nuwfmgy6593, child number 0
##-------------------------------------
##select /*+ no_index(t1 idx_t1_n) */ * from t1 where n=1
##
##Plan hash value: 3270407578
##
##-----------------------------------------------------------------------------
##| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
##-----------------------------------------------------------------------------
##| 0 | SELECT STATEMENT | | | | 1 (100)| |
##|* 1 | INDEX RANGE SCAN| IDX_T1_N | 1 | 4 | 1 (0)| 00:00:01 |
##
##PLAN_TABLE_OUTPUT
##------------------------------------------------------------------------------------------------------------------------
##-----------------------------------------------------------------------------
##
##Query Block Name / Object Alias (identified by operation id):
##-------------------------------------------------------------
##
## 1 - SEL$1 / T1@SEL$1
##
##Outline Data
##-------------
##
## /*+
##
##PLAN_TABLE_OUTPUT
##------------------------------------------------------------------------------------------------------------------------
## BEGIN_OUTLINE_DATA
## IGNORE_OPTIM_EMBEDDED_HINTS
## OPTIMIZER_FEATURES_ENABLE('11.2.0.4')
## DB_VERSION('11.2.0.4')
## ALL_ROWS
## OUTLINE_LEAF(@"SEL$1")
## INDEX(@"SEL$1" "T1"@"SEL$1" ("T1"."N"))
## END_OUTLINE_DATA
## */
##
##Predicate Information (identified by operation id):
##
##PLAN_TABLE_OUTPUT
##------------------------------------------------------------------------------------------------------------------------
##---------------------------------------------------
##
## 1 - access("N"=1)
##
##Column Projection Information (identified by operation id):
##-----------------------------------------------------------
##
## 1 - "N"[NUMBER,22]
##
##Note
##-----
##
##PLAN_TABLE_OUTPUT
##------------------------------------------------------------------------------------------------------------------------
## - SQL profile SYS_SQLPROF_01727ca165b20000 used for this statement
##
##
##46 rows selected.
##
##HR@testogg>
-- 當where條件變化時,查看效果 ,可以看到。沒有使用索引。掃描方式使用了全表掃描 。
select /*+ no_index(t1 idx_t1_n) */ * from t1 where n=2;
select * from table (dbms_xplan.display_cursor(null,null,'advanced'));
##HR@testogg>select /*+ no_index(t1 idx_t1_n) */ * from t1 where n=2;
##
## N
##----------
## 2
##
##HR@testogg>select * from table (dbms_xplan.display_cursor(null,null,'advanced'));
##
##PLAN_TABLE_OUTPUT
##------------------------------------------------------------------------------------------------------------------------
##SQL_ID 5ya4ypmbjcqrj, child number 0
##-------------------------------------
##select /*+ no_index(t1 idx_t1_n) */ * from t1 where n=2
##
##Plan hash value: 3617692013
##
##--------------------------------------------------------------------------
##| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
##--------------------------------------------------------------------------
##| 0 | SELECT STATEMENT | | | | 7 (100)| |
##|* 1 | TABLE ACCESS FULL| T1 | 1 | 4 | 7 (0)| 00:00:01 |
##
##PLAN_TABLE_OUTPUT
##------------------------------------------------------------------------------------------------------------------------
##--------------------------------------------------------------------------
##
##Query Block Name / Object Alias (identified by operation id):
##-------------------------------------------------------------
##
## 1 - SEL$1 / T1@SEL$1
##
##Outline Data
##-------------
##
## /*+
##
##PLAN_TABLE_OUTPUT
##------------------------------------------------------------------------------------------------------------------------
## BEGIN_OUTLINE_DATA
## IGNORE_OPTIM_EMBEDDED_HINTS
## OPTIMIZER_FEATURES_ENABLE('11.2.0.4')
## DB_VERSION('11.2.0.4')
## ALL_ROWS
## OUTLINE_LEAF(@"SEL$1")
## FULL(@"SEL$1" "T1"@"SEL$1")
## END_OUTLINE_DATA
## */
##
##Predicate Information (identified by operation id):
##
##PLAN_TABLE_OUTPUT
##------------------------------------------------------------------------------------------------------------------------
##---------------------------------------------------
##
## 1 - filter("N"=2)
##
##Column Projection Information (identified by operation id):
##-----------------------------------------------------------
##
## 1 - "N"[NUMBER,22]
##
##
##42 rows selected.
##
##HR@testogg>
-- 修改force_match爲true,這個參數是針對綁定變量的。設置爲true類似於cursor_sharing中的force。設置爲false類似於cursor_sharing中的exact 。
exec dbms_sqltune.accept_sql_profile(task_name=>'my_sql_tuning_task20200604',task_owner=>'HR',replace=>true,force_match=>true);
select /*+ no_index(t1 idx_t1_n) */ * from t1 where n=2;
select * from table (dbms_xplan.display_cursor(null,null,'advanced'));
##HR@testogg>exec dbms_sqltune.accept_sql_profile(task_name=>'my_sql_tuning_task20200604',task_owner=>'HR',replace=>true,force_match=>true);
##
##PL/SQL procedure successfully completed.
##
##HR@testogg>
##
##HR@testogg>select /*+ no_index(t1 idx_t1_n) */ * from t1 where n=2;
##
## N
##----------
## 2
##
##HR@testogg>select * from table (dbms_xplan.display_cursor(null,null,'advanced'));
##
##PLAN_TABLE_OUTPUT
##------------------------------------------------------------------------------------------------------------------------
##SQL_ID 5ya4ypmbjcqrj, child number 0
##-------------------------------------
##select /*+ no_index(t1 idx_t1_n) */ * from t1 where n=2
##
##Plan hash value: 3270407578
##
##-----------------------------------------------------------------------------
##| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
##-----------------------------------------------------------------------------
##| 0 | SELECT STATEMENT | | | | 1 (100)| |
##|* 1 | INDEX RANGE SCAN| IDX_T1_N | 1 | 4 | 1 (0)| 00:00:01 |
##
##PLAN_TABLE_OUTPUT
##------------------------------------------------------------------------------------------------------------------------
##-----------------------------------------------------------------------------
##
##Query Block Name / Object Alias (identified by operation id):
##-------------------------------------------------------------
##
## 1 - SEL$1 / T1@SEL$1
##
##Outline Data
##-------------
##
## /*+
##
##PLAN_TABLE_OUTPUT
##------------------------------------------------------------------------------------------------------------------------
## BEGIN_OUTLINE_DATA
## IGNORE_OPTIM_EMBEDDED_HINTS
## OPTIMIZER_FEATURES_ENABLE('11.2.0.4')
## DB_VERSION('11.2.0.4')
## ALL_ROWS
## OUTLINE_LEAF(@"SEL$1")
## INDEX(@"SEL$1" "T1"@"SEL$1" ("T1"."N"))
## END_OUTLINE_DATA
## */
##
##Predicate Information (identified by operation id):
##
##PLAN_TABLE_OUTPUT
##------------------------------------------------------------------------------------------------------------------------
##---------------------------------------------------
##
## 1 - access("N"=2)
##
##Column Projection Information (identified by operation id):
##-----------------------------------------------------------
##
## 1 - "N"[NUMBER,22]
##
##Note
##-----
##
##PLAN_TABLE_OUTPUT
##------------------------------------------------------------------------------------------------------------------------
## - SQL profile SYS_SQLPROF_01727ca668f00001 used for this statement
##
##
##46 rows selected.
##
##HR@testogg>
END