说明:本文为STS(SQL Tuning Set)使用参考手册
用途:本文仅供初学者熟悉了解SQL Tuning Set或优化参考
标签:SQL Tuning Set、STS、STS使用方法、Oracle优化、SQL调优、STS架构图
温馨提示:如果您发现本文哪里写的有问题或者有更好的写法请留言或私信我进行修改优化
高级内容:由于篇幅原因,部分高级内容和详情没有在此展现,如有需要可以留言或私信
★ 相关文章(※ STS的5种输入源)
①Cursor Cache
②AWR(Automatic Workload Repository)
③SQL Trace
④SQLPA(SQL Performance Analyzer Task)
⑤STS(SQL Tuning Set)
★ 知识点
※ 名词解释:SQL调优集(STS)是一个数据库对象,可以用作调优工具的输入。
※ STS包括以下组件:
①一组SQL语句
②关联的执行上下文,例如用户架构,应用程序模块名称和操作,绑定值列表以及游标的 SQL编译环境
③相关的基本执行统计信息,例如经过时间,CPU时间,缓冲区获取,磁盘读取,已处理的行,游标提取,执行次数,完整执行次数,优化器成本和命令类型
④每个SQL语句的关联执行计划和行源统计信息(可选)
★ 输入源D【SQL Performance Analyzer Task】 → dbms_sqltune.select_sqlpa_task
※ 温馨提示以下所有操作均在scott用户下执行(请临时给scott授予DBA角色)
--使用SCOTT用户登录并测试SPA
drop table scott.zzt_sts_t;
create table scott.zzt_sts_t as select * from scott.emp;
--收集统计信息
exec dbms_stats.gather_table_stats(ownname => 'SCOTT',tabname => 'ZZT_STS_T',method_opt=> 'for all columns',estimate_percent => '100',degree=>'2',granularity=>'all',cascade=>TRUE);
--查看STS中SQL的执行计划
set line 170
set pages 200
set serveroutput off
ALTER SESSION SET STATISTICS_LEVEL=ALL;
select count(*) from scott.zzt_sts_t;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(NULL,NULL,'ALLSTATS LAST'));
--删除SPA
exec DBMS_SQLPA.drop_ANALYSIS_TASK(task_name => 'ZZT_SPA_TASK');
--删除STS
BEGIN
DBMS_SQLTUNE.DROP_SQLSET(sqlset_name => 'ZZT_SQL_TUNING_SET_OLD',
sqlset_owner => 'SCOTT');
END;
/
--创建STS
BEGIN
DBMS_SQLTUNE.CREATE_SQLSET(
sqlset_name => 'ZZT_SQL_TUNING_SET_OLD',
sqlset_owner => 'SCOTT',
description => 'test');
END;
/
--查看STS
select * from dba_sqlset;
--加载符合条件的SQL到STS
--从18C开始STS系统包发生了变化DBMS_SQLTUNE→DBMS_SQLSET
DECLARE
zzt_cur_sqlarea DBMS_SQLTUNE.SQLSET_CURSOR; --定义游标参数和DBMS_SQLTUNE.SQLSET_CURSOR相同
--zzt_cur_sqlarea sys_refcursor; --也可以直接使用系统游标
BEGIN
OPEN zzt_cur_sqlarea FOR --将符合条件的SQL加载到STS中
SELECT VALUE(p)
FROM TABLE(DBMS_SQLTUNE.SELECT_CURSOR_CACHE(basic_filter => 'parsing_schema_name = ''SCOTT''',
attribute_list => 'all')) p;
-- load the tuning set
DBMS_SQLTUNE.LOAD_SQLSET(sqlset_name => 'ZZT_SQL_TUNING_SET_OLD',
populate_cursor => zzt_cur_sqlarea,
sqlset_owner => 'SCOTT',
load_option => 'INSERT',
update_option => 'REPLACE',
update_condition => 'new.executions >= old.executions',
update_attributes => 'ALL',
ignore_null => TRUE,
commit_rows => NULL);
END;
/
--读取最新的STS详情(尽量保证STS中只有需要的SQL,以排除干扰)
SELECT sql_id, sql_text, parsing_schema_name, elapsed_time
FROM TABLE(DBMS_SQLTUNE.SELECT_SQLSET(sqlset_name => 'ZZT_SQL_TUNING_SET_OLD',
sqlset_owner => 'SCOTT')) ;
--如果执行完上述操作后没有结果,可以重新多执行几次,直到STS抓到SQL
--SPA
--查看SPA
select * from DBA_ADVISOR_TASKS where task_name = 'ZZT_SPA_TASK';
--删除SPA
exec DBMS_SQLPA.drop_ANALYSIS_TASK(task_name => 'ZZT_SPA_TASK');
--删除数据库上的主键,并重新收集统计信息
alter table scott.zzt_sts_t drop constraint zzt_sts_t_pk;
--收集统计信息
exec dbms_stats.gather_table_stats(ownname => 'SCOTT',tabname => 'ZZT_STS_T',method_opt=> 'for all columns',estimate_percent => '100',degree=>'2',granularity=>'all',cascade=>TRUE);
--创建一个参考STS的SQLPA分析任务
var v_out char(50)
begin
:v_out:=dbms_sqlpa.create_analysis_task(
sqlset_name => 'ZZT_SQL_TUNING_SET_OLD',
basic_filter => null,
order_by => null,
top_sql => 9,
task_name => 'ZZT_SPA_TASK',
description => 'zzt test',
sqlset_owner => 'SCOTT'
);
end;
/
print v_out
--运行该SQLPA分析任务(数据库更改前)
begin
DBMS_SQLPA.EXECUTE_ANALYSIS_TASK(task_name => 'ZZT_SPA_TASK',
execution_type => 'TEST EXECUTE',
execution_name => 'test_spa_task_before',
execution_params => dbms_advisor.arglist('time_limit',3600),
execution_desc => 'zzt test');
end;
/
--查看运行情况
select * from DBA_ADVISOR_EXECUTIONS where task_name='ZZT_SPA_TASK' order by execution_end;
--读取最新的STS详情
SELECT *
FROM TABLE(DBMS_SQLTUNE.SELECT_SQLSET(sqlset_name => 'ZZT_SQL_TUNING_SET_OLD',
sqlset_owner => 'SCOTT'))
where lower(SQL_TEXT) like 'select count(*) from scott%'
order by FORCE_MATCHING_SIGNATURE;
--查看STS中SQL的执行计划
set serveroutput off
select * from table(dbms_xplan.display_sqlset(sqlset_name=>'ZZT_SQL_TUNING_SET_OLD',sql_id=>'brmcv7zkd99vj',plan_hash_value=>null,format=>'allstats last',sqlset_owner=>'SCOTT'));
--改变数据库
--在表scott.zzt_sts_t上创建一个索引
alter table scott.zzt_sts_t drop constraint zzt_sts_t_pk;
alter table scott.zzt_sts_t add constraint zzt_sts_t_pk primary key (empno);
--收集统计信息
exec dbms_stats.gather_table_stats(ownname => 'SCOTT',tabname => 'ZZT_STS_T',method_opt=> 'for all columns',estimate_percent => '100',degree=>'2',granularity=>'all',cascade=>TRUE);
--查看SQL的即席执行计划
set serveroutput off
ALTER SESSION SET STATISTICS_LEVEL=ALL;
select count(*) from scott.zzt_sts_t;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(NULL,NULL,'ALLSTATS LAST'));
--运行该SQLPA分析任务(数据库更改后)
begin
DBMS_SQLPA.EXECUTE_ANALYSIS_TASK(task_name => 'ZZT_SPA_TASK',
execution_type => 'TEST EXECUTE',
execution_name => 'test_spa_task_after',
execution_params => dbms_advisor.arglist('time_limit',3600),
execution_desc => 'zzt test');
end;
/
--查看运行情况
select * from DBA_ADVISOR_EXECUTIONS where task_name='ZZT_SPA_TASK' order by execution_end;
--读取最新的STS详情
SELECT *
FROM TABLE(DBMS_SQLTUNE.SELECT_SQLSET(sqlset_name => 'ZZT_SQL_TUNING_SET_OLD',
sqlset_owner => 'SCOTT'))
where lower(SQL_TEXT) like 'select count(*) from scott%'
order by FORCE_MATCHING_SIGNATURE;
--查看STS中SQL的执行计划
set serveroutput off
select * from table(dbms_xplan.display_sqlset(sqlset_name=>'ZZT_SQL_TUNING_SET_OLD',sql_id=>'brmcv7zkd99vj',plan_hash_value=>null,format=>'allstats last',sqlset_owner=>'SCOTT'));
--比较因数据库改变而发生的执行改变
begin
DBMS_SQLPA.EXECUTE_ANALYSIS_TASK(task_name => 'ZZT_SPA_TASK',
execution_type => 'COMPARE',
execution_name => 'test_spa_task_compare',
execution_params => dbms_advisor.arglist('comparison_metric','BUFFER_GETS')
);
end;
/
--查看运行情况
select * from DBA_ADVISOR_EXECUTIONS where task_name='ZZT_SPA_TASK' order by execution_end;
--用REPORT_ANALISIS_TASK打印分析报告(在PL/SQL中执行)
variable rep CLOB;
begin
:rep:= DBMS_SQLPA.REPORT_ANALYSIS_TASK(
task_name=>'ZZT_SPA_TASK',
type=>'HTML',
level=>'ALL',
section=>'ALL');
end;
/
--输出SPA对比结果
SET LONG 100000
set LONGCHUNKSIZE 100000
set LINESIZE 200
set head off
set feedback off
set echo off
spool c:/app/zzt_spa_sts_changes.html
PRINT :rep
spool off
set head on
--将SQLPA加载到STS中
--删除STS
BEGIN
DBMS_SQLTUNE.DROP_SQLSET(sqlset_name => 'ZZT_SQL_TUNING_SET_NEW',
sqlset_owner => 'SCOTT');
END;
/
--创建STS
BEGIN
DBMS_SQLTUNE.CREATE_SQLSET(
sqlset_name => 'ZZT_SQL_TUNING_SET_NEW',
sqlset_owner => 'SCOTT',
description => 'test');
END;
/
--查看STS
select * from dba_sqlset;
--加载符合条件的SQL到STS
--从18C开始STS系统包发生了变化DBMS_SQLTUNE→DBMS_SQLSET
DECLARE
zzt_cur_sqlarea DBMS_SQLTUNE.SQLSET_CURSOR; --定义游标参数和DBMS_SQLTUNE.SQLSET_CURSOR相同
--zzt_cur_sqlarea sys_refcursor; --也可以直接使用系统游标
BEGIN
OPEN zzt_cur_sqlarea FOR --将符合条件的SQL加载到STS中
SELECT VALUE(p)
FROM TABLE(DBMS_SQLTUNE.SELECT_SQLPA_TASK(
task_name => 'ZZT_SPA_TASK',
task_owner => 'SCOTT',
execution_name => 'test_spa_task_compare',
level_filter => 'IMPROVED'
)) p;
-- load the tuning set
DBMS_SQLTUNE.LOAD_SQLSET(sqlset_name => 'ZZT_SQL_TUNING_SET_NEW',
populate_cursor => zzt_cur_sqlarea,
sqlset_owner => 'SCOTT',
load_option => 'INSERT',
update_option => 'REPLACE',
update_condition => 'new.executions >= old.executions',
update_attributes => 'ALL',
ignore_null => TRUE,
commit_rows => NULL);
END;
/
--读取最新的STS详情
--COLUMN SQL_TEXT FORMAT a30
--COLUMN SCH FORMAT a3
--COLUMN ELAPSED FORMAT 999999999
SELECT *
FROM TABLE(DBMS_SQLTUNE.SELECT_SQLSET(sqlset_name => 'ZZT_SQL_TUNING_SET_NEW',
sqlset_owner => 'SCOTT'))
where lower(SQL_TEXT) like 'select count(*) from scott%'
order by FORCE_MATCHING_SIGNATURE;
--查看新STS中SQL的执行计划(其实和旧STS执行计划一样,并不会变为SQLPA分析后最佳的执行计划)
set serveroutput off
select * from table(dbms_xplan.display_sqlset(sqlset_name=>'ZZT_SQL_TUNING_SET_NEW',sql_id=>'brmcv7zkd99vj',plan_hash_value=>null,format=>'allstats last',sqlset_owner=>'SCOTT'));
--收集统计信息
exec dbms_stats.gather_table_stats(ownname => 'SCOTT',tabname => 'ZZT_STS_T',method_opt=> 'for all columns',estimate_percent => '100',degree=>'2',granularity=>'all',cascade=>TRUE);
--查看SQL的即席执行计划
set serveroutput off
ALTER SESSION SET STATISTICS_LEVEL=ALL;
select count(*) from scott.zzt_sts_t;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(NULL,NULL,'ALLSTATS LAST'));
--清理环境
--重置任务结果:
exec dbms_sqlpa.reset_analysis_task(task_name=>'ZZT_SPA_TASK');
--查看SPA任务运行情况
select * from DBA_ADVISOR_EXECUTIONS where task_name='ZZT_SPA_TASK' order by execution_end;
--删除SPA任务
exec DBMS_SQLPA.drop_ANALYSIS_TASK(task_name => 'ZZT_SPA_TASK');
--查看spa任务详情
select * from dba_advisor_tasks where task_name='ZZT_SPA_TASK' order by execution_end;
--清除测试表和索引
drop table scott.zzt_sts_t cascade constraints purge;
--删除STS_old
exec DBMS_SQLTUNE.DROP_SQLSET(sqlset_name => 'ZZT_SQL_TUNING_SET_OLD',sqlset_owner => 'SCOTT');
--删除STS_new
exec DBMS_SQLTUNE.DROP_SQLSET(sqlset_name => 'ZZT_SQL_TUNING_SET_NEW',sqlset_owner => 'SCOTT');
★ 参考文档
※ Oracle官方文档>>>Capturing Workloads in SQL Tuning Sets
※ lhdz_bj>>>Oracle 11g 中SQL性能优化新特性之SQL性能分析器(SQLPA)
※ 如果您觉得文章写的还不错, 别忘了在文末给作者点个赞哦 ~
over