--创建任务
DECLARE
my_task_name VARCHAR2(30);
my_sqltext CLOB;
BEGIN
my_sqltext := 'SELECT ID_CODE,ID_NAME
FROM VIDS.TRACE_OFFLINE_hist T
WHERE 1 =1
AND LOGOUT_AT >= to_date(''2013-08-14 00:00:00'',''yyyy-mm-dd hh24:mi:ss'')
AND LOGOUT_AT <= to_date(''2013-08-25 23:59:00'',''yyyy-mm-dd hh24:mi:ss '')
AND T.ID_TYPE = 2
AND T.RID_YEAR <= 2003
AND T.RID_YEAR >= 1978
AND T.RID_SEX = 1
AND ((1=1
AND (T.AREA_CODE IN(''310105'')
OR T.SERVICE_CODE IN(''31010421020167'',''31010421140033''))))
AND NOT EXISTS
(SELECT 1
FROM VIDS.TRACE_OFFLINE_hist S7
WHERE 1 =1
AND LOGOUT_AT >= to_date(''2013-08-24 00:00:00'',''yyyy-mm-dd hh24:mi:ss'')
AND LOGOUT_AT <= to_date(''2013-08-30 23:59:00'',''yyyy-mm-dd hh24:mi:ss '')
AND S7.ID_TYPE = 2
AND S7.RID_YEAR <= 2003
AND S7.RID_YEAR >= 1978
AND S7.RID_SEX = 1
AND ((1=1
AND (S7.AREA_CODE IN(''310105'')
OR S7.SERVICE_CODE IN(''31010421020167'',''31010421140033''))))
AND T.ID_CODE = S7.ID_CODE
)';
my_task_name := DBMS_SQLTUNE.CREATE_TUNING_TASK(
sql_text => my_sqltext,
user_name => 'VIDS', --注意是大写,不然会报错,用户无效
scope => 'COMPREHENSIVE',
time_limit => 60,
task_name => 'tuning_sql_test',
description => 'Task to tune a query on a specified table');
DBMS_SQLTUNE.EXECUTE_TUNING_TASK( task_name => 'tuning_sql_test');
END;
/
--执行任务
exec dbms_sqltune.execute_tuning_task('tuning_sql_test');
--删除任务
exec dbms_sqltune.drop_tuning_task('tuning_sql_test');
--检查优化任务的状态
SELECT task_name,status FROM USER_ADVISOR_TASKS WHERE task_name ='tuning_sql_test';
--查看优化结果
SET LONG 999999
set serveroutput on size 999999
SET LINESIZE 100
SELECT DBMS_SQLTUNE.REPORT_TUNING_TASK( 'tuning_sql_test') from DUAL;
该方法与db2advis有相同作用。