您好,登錄后才能下訂單哦!
參考:
http://www.bubuko.com/infodetail-216529.html
實驗如下:
SQL> create table dh_stat as select rownum id ,object_name name ,object_type type from dba_objects;
Table created.
SQL> create index ind_1 on dh_stat(id) compute statistics;
Index created.
SQL> alter system flush shared_pool;
System altered.
SQL> exec DBMS_STATS.GATHER_TABLE_STATS(OWNNAME=>'sys',TABNAME=>'dh_stat',ESTIMATE_PERCENT=>30,METHOD_OPT=>'FOR ALL COLUMNS SIZE 1',NO_INVALIDATE=>FALSE,CASCADE=>TRUE,DEGREE => 1);
PL/SQL procedure successfully completed.
SQL> col name format a15
SQL> col name format a30
SQL> col sql_text for a55
SQL> select /* outlinetest1 */ * from dh_stat where id=771;
ID NAME TYPE
---------- ------------------------------ -------------------
771 SYS_IL0000000772C00002$$ INDEX
SQL> set lines 200
SQL> select sql_text,sql_id,hash_value,child_number from v$sql a where sql_text like '%outlinetest1%' and sql_text not like '%v$sql%';
SQL_TEXT SQL_ID HASH_VALUE CHILD_NUMBER
------------------------------------------------------- ------------- ---------- ------------
select /* outlinetest1 */ * from dh_stat where id=771 9a69w18a8uuhk 344812050 0
SQL> select * from table(dbms_xplan.display_cursor('9a69w18a8uuhk',null));
PLAN_TABLE_OUTPUT
-----------------------------------------------------------------------------
SQL_ID 9a69w18a8uuhk, child number 0
-------------------------------------
select /* outlinetest1 */ * from dh_stat where id=771
Plan hash value: 2780970545
---------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 2 (100)| |
| 1 | TABLE ACCESS BY INDEX ROWID| DH_STAT | 1 | 39 | 2 (0)| 00:00:01 |
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------------
|* 2 | INDEX RANGE SCAN | IND_1 | 1 | | 1 (0)| 00:00:01 |
---------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("ID"=771)
19 rows selected.
---根據sql已經有的執行計劃生成outline:
SQL> exec DBMS_OUTLN.create_outline(hash_value=>344812050,child_number => 0,category=>'TEST');
PL/SQL procedure successfully completed.
--查詢outline情況:
SQL> select name,category,used,sql_text from dba_outlines;
NAME CATEGORY USED SQL_TEXT
------------------------------ ------------------------------ ------ -------------------------------------------------------
SYS_OUTLINE_17090216454529101 TEST UNUSED select /* outlinetest1 */ * from dh_stat where id=771
此處outline的USED狀態沒有改變,因為我們沒有激活TEST類別的outline,再次申明,outline必須通過use_stored_outlines參數激活后,優化器才會使用outline
---下面這一步激活TEST類別的OUTLINE,也可以在系統級激活OUTLINE
SQL> alter session set use_stored_outlines=TEST;
驗證省略。。。。。。。。。。。
語法:
SQL text format:
DBMS_SQLTUNE.CREATE_TUNING_TASK( sql_text IN CLOB, bind_list IN sql_binds := NULL, user_name IN VARCHAR2 := NULL, scope IN VARCHAR2 := SCOPE_COMPREHENSIVE, time_limit IN NUMBER := TIME_LIMIT_DEFAULT, task_name IN VARCHAR2 := NULL, description IN VARCHAR2 := NULL) RETURN VARCHAR2;
SQL ID format:
DBMS_SQLTUNE.CREATE_TUNING_TASK( sql_id IN VARCHAR2, plan_hash_value IN NUMBER := NULL, scope IN VARCHAR2 := SCOPE_COMPREHENSIVE, time_limit IN NUMBER := TIME_LIMIT_DEFAULT, task_name IN VARCHAR2 := NULL, description IN VARCHAR2 := NULL) RETURN VARCHAR2;
Workload Repository format:
DBMS_SQLTUNE.CREATE_TUNING_TASK( begin_snap IN NUMBER, end_snap IN NUMBER, sql_id IN VARCHAR2, plan_hash_value IN NUMBER := NULL, scope IN VARCHAR2 := SCOPE_COMPREHENSIVE, time_limit IN NUMBER := TIME_LIMIT_DEFAULT, task_name IN VARCHAR2 := NULL, description IN VARCHAR2 := NULL) RETURN VARCHAR2; 實驗1:sql_id format 1. sys用戶創建oracle優化任務(v_sqlid,task_name根據實際情況改變) set long 10000000 longchunksize 1000000 linesize 150 pagesize 0 serveroutput on size 1000000 verify off declare my_task_name varchar2(30); v_sqlid varchar2(50); begin v_sqlid:='11wrxmug9y4a7'; my_task_name := dbms_sqltune.create_tuning_task (sql_id=> v_sqlid, scope => 'comprehensive', time_limit=>160, task_name=>'task_00000', description => 'tuning task'); dbms_sqltune.execute_tuning_task('task_00000'); end; / 注:dbms_sqltune.execute_tuning_task('task_00000'),是執行優化任務 2. 打印優化任務,里面有一些具體的改進措施 select dbms_sqltune.report_tuning_task('task_00000') from dual; 3.根據優化建議,綁定profile,理論上可提高語句執行效率 execute dbms_sqltune.accept_sql_profile(task_name => 'task_00000',task_owner => 'sys', replace => true); 實驗2:sql_text format SQL> alter session set statistics_level=all; Session altered. SQL> set serveroutput off SQL> select * from scott.emp where ename='SCOTT' and DEPTNO=20; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- ------------------- ---------- ---------- ---------- 7788 SCOTT ANALYST 7566 1987-04-19 00:00:00 3000 20 SQL> SELECT * FROM table(dbms_xplan.display_cursor(NULL,NULL,'runstats_last')); PLAN_TABLE_OUTPUT -------------------------------------------------------------------------------- SQL_ID 4dsqbp572auuu, child number 0 ------------------------------------- select * from scott.emp where ename='SCOTT' and DEPTNO=20 Plan hash value: 3956160932 ------------------------------------------------------------------------------------ | Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | ------------------------------------------------------------------------------------ | 0 | SELECT STATEMENT | | 1 | | 1 |00:00:00.01 | 7 | |* 1 | TABLE ACCESS FULL| EMP | 1 | 1 | 1 |00:00:00.01 | 7 | PLAN_TABLE_OUTPUT --------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter(("ENAME"='SCOTT' AND "DEPTNO"=20)) 18 rows selected. SQL> DECLARE my_task_name VARCHAR2(30); my_sqltext CLOB; BEGIN my_sqltext := 'select * from scott.emp where ename= :name and DEPTNO= :deptno'; my_task_name := DBMS_SQLTUNE.CREATE_TUNING_TASK( sql_text => my_sqltext, bind_list => sql_binds(anydata.convertvarchar2(10),anydata.convertnumber(2)), user_name => 'SYS', scope => 'COMPREHENSIVE', time_limit => 60, task_name => 'test_sql_tuning', description => 'Task to tune a query on emp'); END; / PL/SQL procedure successfully completed. 參數說明: bind_list:多個綁定變量以','逗號分隔。參數值一定要根據綁定變量對應的列的類型書寫.如:emp.ename類型是VARCHAR2(10),那么就要寫成 bind_list =>sql_binds(anydata.convertvarchar2(10)), time_limit:執行的最長時間,默認是60。 scope:LIMITED,用大概1秒時間去優化SQL語句,但是并不進行SQL Profiling分析;COMPREHENSIVE,進行全面分析,包含SQL Profiling分析;比LIMITED用時更長。 --查看任務名 SQL> select task_name from dba_advisor_log where task_name='test_sql_tuning'; TASK_NAME ------------------------------ test_sql_tuning --執行sql tuning任務 SQL> exec DBMS_SQLTUNE.EXECUTE_TUNING_TASK( task_name => 'test_sql_tuning' ); PL/SQL procedure successfully completed. --查看sql tunning任務狀態 SQL> select task_name,status from dba_advisor_log where task_name='test_sql_tuning'; TASK_NAME STATUS ------------------------------ ----------- test_sql_tuning COMPLETED ----展示sql tunning結果 set long 10000 set longchunksize 1000 set linesize 100 select dbms_sqltune.report_tuning_task('test_sql_tuning') from dual; ...........省略 --根據建議accept_sql_profile execute dbms_sqltune.accept_sql_profile(.....) --完成后刪除sql tunning任務 exec dbms_sqltune.drop_tuning_task('test_sql_tuning'); --查看SQL Tuning Advisor的進展(task執行很久) set lines 200 col opname for a20 col ADVISOR_NAME for a20 select sid,serial#,username,opname,advisor_name,target_desc,start_time sofar,totalwork from v$advisor_progress where username = 'SYS';
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。