eristoddle
3/26/2014 - 6:45 PM

Oracle tune queries

Oracle tune queries

DECLARE
  l_sql              VARCHAR2(2000) := '';
  l_sql_tune_task_id VARCHAR2(400) ;
  recommendations    VARCHAR2(2000) ;
  RetVal CLOB;
  MAXBUFSIZE NUMBER := 32767;
BEGIN
  dbms_output.enable(1000000) ;
  l_sql_tune_task_id := dbms_sqltune.create_tuning_task( 
    sql_text => l_sql, 
    time_limit => 120, 
    scope => DBMS_SQLTUNE.scope_comprehensive, 
    task_name => 'sql_id_tuning_task_SD', 
    description => 'Tuning task for statement sqlid'
  ) ;
  BEGIN
    DBMS_SQLTUNE.reset_tuning_task(task_name => 'sql_id_tuning_task_SD') ;
    DBMS_SQLTUNE.execute_tuning_task(task_name => 'sql_id_tuning_task_SD') ;
    RetVal := dbms_sqltune.report_tuning_task(l_sql_tune_task_id) ;
    dbms_output.put_line('<PRE>') ;
    dbms_output.put_line(dbms_lob.substr(RetVal, MAXBUFSIZE - 1, 1)) ;
    dbms_output.put_line('</PRE>') ;
    dbms_sqltune.DROP_TUNING_TASK('sql_id_tuning_task_SD') ;
  EXCEPTION
  WHEN OTHERS THEN
    dbms_output.put_line('Unable to produce report.') ;
    dbms_output.put_line(SQLERRM) ;
    dbms_sqltune.DROP_TUNING_TASK('sql_id_tuning_task_SD') ;
  END;
END;