-- 为了避免出现:ORA-13767:结束快照 ID 必须大于开始快照 ID。-- 如果 snap_ids 相同,则将 begin snap id 减一
BEGIN -- 如果它不在 V$SQL 中,我们将不得不查询工作负载存储库 select count(*) into cnt from V$SQLSTATS where sql_id = '&&sqlid';
IF (cnt > 0) THEN :task_name := dbms_sqltune.create_tuning_task(sql_id => '&&sqlid',scope => DBMS_SQLTUNE.scope_comprehensive,time_limit => 7200); ELSE select min(snap_id) into bsnap from dba_hist_sqlstat where sql_id = '&&sqlid';
select max(snap_id) into esnap
from dba_hist_sqlstat
where sql_id = '&&sqlid';
-- in order to avoid an: ORA-13767: End snapshot ID must be greater than begin snapshot ID.
-- if the snap_ids are the same, then lower the begin snap id by one
if (:bsnap = :esnap) then
:bsnap := :bsnap - 1;
end if;
:task_name := dbms_sqltune.create_tuning_task(begin_snap => :bsnap,
end_snap => :esnap,
time_limit => 7200,
sql_id => '&&sqlid',
scope => DBMS_SQLTUNE.scope_comprehensive);
万一;
dbms_sqltune.execute_tuning_task(:task_name);