Search notes:

Oracle: DBMS_SQLTUNE

dbms_sqltune is the traditional package to manage SQL Tuning Sets. The package allows to to create, alter, drop and transport SQL profiles.
It requires Oracle Tuning Pack.
dbms_sqlset offers the same functionality, but without the Tuning Pack.
dbms_sqltune requires the ADMINISTER SQL MANAGEMENT OBJECT privilege.

Functions and procedures

accept_all_sql_profiles
accept_sql_profile
add_sqlset_reference
alter_plan_object_xml
alter_sql_profile
alter_tuning_task_xml
build_stash_xml
cancel_tuning_task
capture_cursor_cache_sqlset
cap_sts_cbk
check_sqlset_privs
check_sql_profile_priv
check_tuning_task_status
configure_tuning_task_xml
create_sqlset
create_sql_plan_baseline
create_stgtab_sqlprof
create_stgtab_sqlset
create_tuning_task
delete_sqlset
drop_plan_object_xml
drop_sqlset
drop_sql_profile
drop_tuning_task
examine_stgtab
execute_tuning_task
export_plan_object_xml
extract_bind
extract_binds
implement_tuning_task
implement_tuning_task_xml
import_plan_object_xml
import_sql_profile
interrupt_tuning_task
list_all_directories_xml
load_sqlset
load_sql_plan_baseline_xml
pack_stgtab_sqlprof
pack_stgtab_sqlset
prepare_awr_statement
prepare_sqlset_statement
remap_stgtab_sqlprof
remap_stgtab_sqlset
remove_sqlset_reference
report_auto_tuning_task
report_sql_detail
report_sql_detail_xml
report_sql_monitor
report_sql_monitor_list
report_sql_monitor_list_xml
report_sql_monitor_xml
report_tuning_task
report_tuning_task_list_xml
report_tuning_task_xml
reset_tuning_task
resume_tuning_task
schedule_tuning_task See Identifying SQL statements: force_matching_signature and exact_matching_signature
script_tuning_task
select_cursor_cache collects SQL statements from the shared SQL area.
select_sqlpa_task
select_sqlset
select_sql_trace reads a trace file and returns the found SQL statements as an sqlset_row.
select_workload_repository
set_auto_tuning_task_parameter
set_tuning_task_parameter
sqlset_progress_stats
sqltext_to_signature
transform_sqlset_cursor
unpack_stgtab_sqlprof
unpack_stgtab_sqlset
update_sqlset

Operations sequences

SQL profile «life cycle»

dbms_sqltune.accept_sql_profile implements a (new) SQL profile.
dba_sql_profiles shows available profiles.
Existing profiles is modified with dbms_sqltune.alter_sql_profile.
Profiles are removed with dbms_sqltune.drop_sql_profile.

Transporting SQL profiles

dbms_sqltune.create_stgtab_sqlprof creates the staging table.
dbms_sqltune.pack_stgtab_sqlset copies an SQL profile's data into the staging table.
dbms_sqltune.unpack_stgtab_sqlset uses the data in a staging table to create SQL profiles.
Typically, after inserting data into the staging table, the data is then copied to another server using with the Data Pump or a database link.

Example (slight variation of MOS Note 271796.1

create table tq84_sql_profile_test (
   id      number  (   7) primary key,
   payload varchar2(1000)
);

begin
   insert into tq84_sql_profile_test
   select
      level,
      lpad('x', 1000, 'x')
   from
      dual connect by level <= 1e5;

   dbms_stats.gather_table_stats(user, 'tq84_sql_profile_test');
end;
/

set serveroutput off

select /*+ no_index(tq84_sql_profile_test) */
   length(payload)
from
   tq84_sql_profile_test
where
   id = 12345;

select * from dbms_xplan.display_cursor(format=>'allstats last');


set serveroutput on
declare
   task_name varchar2(128);
begin
   task_name := dbms_sqltune.create_tuning_task (
      sql_text   => q'[select /*+ no_index(tq84_sql_profile_test) */
   length(payload)
from
   tq84_sql_profile_test
where
   id = 12345]',
      user_name   => user,                         -- The user name under which the SQL statement is analyzed
      scope       =>'COMPREHENSIVE',               -- Also perform SQL profiling analysis
      time_limit  => 60,                           -- Seconds
      task_name   =>'tq84_sql_profile_test_task',
      description =>'test'
   );

   dbms_output.put_line(task_name);

end;
/

--
-- View th task names that are associated with a given owner:
--
select *
from
   dba_advisor_log
where
   owner = user;


begin
   dbms_sqltune.execute_tuning_task( task_name => 'tq84_sql_profile_test_task' );
end;
/

select * from dba_sql_profiles where signature = dbms_sqltune.sqltext_to_signature(q'[select /*+ no_index(tq84_sql_profile_test) */
   length(payload)
from
   tq84_sql_profile_test
where
   id = 12345]');

--
-- View recommendations:
--
set long 10000
set longchunksize 10000
set lines 100
set heading off
select dbms_sqltune.report_tuning_task('tq84_sql_profile_test_task') from dual;

/*

1- SQL Profile Finding (see explain plans section below)
--------------------------------------------------------
  A potentially better execution plan was found for this statement.

  Recommendation (estimated benefit: 99.97%)
  ------------------------------------------
  - Consider accepting the recommended SQL profile.
    BEGIN    
     dbms_sqltune.accept_sql_profile(
      task_name => 'tq84_sql_profile_test_task',
      task_owner => 'RENE',
      replace => TRUE);
    END;    
    /    

*/

begin
   dbms_sqltune.accept_sql_profile(
      task_name  => 'tq84_sql_profile_test_task',
      task_owner =>  user,
      replace    =>  true
   );
end;
/

select * from dba_sql_profiles where signature = dbms_sqltune.sqltext_to_signature(q'[select /*+ no_index(tq84_sql_profile_test) */
   length(payload)
from
   tq84_sql_profile_test
where
   id = 12345]');

explain plan for
select /*+ no_index(tq84_sql_profile_test) */
   length(payload)
from
   tq84_sql_profile_test
where
   id = 12345;

select * from dbms_xplan.display();
/*

-----------------------------------------------------------------------------------------------------
| Id  | Operation                   | Name                  | Rows  | Bytes | Cost (%CPU)| Time     |
-----------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |                       |     1 |  1006 |     2   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| TQ84_SQL_PROFILE_TEST |     1 |  1006 |     2   (0)| 00:00:01 |
|*  2 |   INDEX UNIQUE SCAN         | SYS_C0012147          |     1 |       |     1   (0)| 00:00:01 |
-----------------------------------------------------------------------------------------------------
 
Predicate Information (identified by operation id):
---------------------------------------------------
 
   2 - access("ID"=12345)
 
Hint Report (identified by operation id / Query Block Name / Object Alias):
Total hints for statement: 1 (U - Unused (1))
---------------------------------------------------------------------------
 
   1 -  SEL$1 / "TQ84_SQL_PROFILE_TEST"@"SEL$1"
         U -  no_index(tq84_sql_profile_test) / rejected by IGNORE_OPTIM_EMBEDDED_HINTS
 
Note
-----
   - SQL profile "SYS_SQLPROF_01944bbe1e770000" used for this statement


*/

drop   table tq84_sql_profile_test;

begin
   dbms_sqltune.drop_tuning_task('tq84_sql_profile_test_task');
end;
/

See also

Oracle DBMS PL/SQL Packages

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1758194140, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/databases/Oracle/installed/packages/dbms/sqltune/index(319): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78