Oracle / SQL 调优顾问
Posted
技术标签:
【中文标题】Oracle / SQL 调优顾问【英文标题】:Oracle / SQL Tuning Advisor 【发布时间】:2021-04-01 08:17:50 【问题描述】:STA 可以处理在数据库中甚至没有运行一次的查询吗?我的意思是,如果它不在缓存中,SQL 调优顾问可以为我们得到一些结果吗?
【问题讨论】:
【参考方案1】:SQL Tuning Advisor 可以处理以前从未运行过的查询。我不确定您使用的是什么接口,但DBMS_SQLTUNE package 可以很容易地传递 SQL 文本。
例如,让我们创建一个没有索引的简单表:
--Create a simple table with no indexes.
create table table_missing_index(a number);
insert into table_missing_index
select level from dual connect by level <= 100000;
begin
dbms_stats.gather_table_stats(user, 'TABLE_MISSING_INDEX');
end;
/
然后传入一个显然可以从索引中受益的查询:
--Create, execute, and display the tuning task.
declare
v_task varchar2(64);
begin
v_task := dbms_sqltune.create_tuning_task
(
sql_text => 'select a from table_missing_index where a = 1'
);
dbms_sqltune.execute_tuning_task(task_name => v_task);
dbms_output.put_line('Task name: '||v_task);
end;
/
从输出中获取任务名称,并将其插入此查询以查看结果:
select dbms_sqltune.report_tuning_task('TASK_362') from dual;
输出应该是这样的:
GENERAL INFORMATION SECTION
-------------------------------------------------------------------------------
Tuning Task Name : TASK_362
Tuning Task Owner : JHELLER
Workload Type : Single SQL Statement
Scope : COMPREHENSIVE
Time Limit(seconds): 1800
Completion Status : COMPLETED
Started at : 04/04/2021 14:26:15
Completed at : 04/04/2021 14:26:15
-------------------------------------------------------------------------------
Schema Name : JHELLER
Container Name: ORCLPDB
SQL ID : 0g6v1x1c7kcjt
SQL Text : select a from table_missing_index where a = 1
-------------------------------------------------------------------------------
FINDINGS SECTION (1 finding)
-------------------------------------------------------------------------------
1- Index Finding (see explain plans section below)
--------------------------------------------------
The execution plan of this statement can be improved by creating one or more
indices.
Recommendation (estimated benefit: 98.56%)
------------------------------------------
- Consider running the Access Advisor to improve the physical schema design
or creating the recommended index.
create index JHELLER.IDX$$_016A0001 on JHELLER.TABLE_MISSING_INDEX("A");
... [removed rest of the large report] ...
【讨论】:
以上是关于Oracle / SQL 调优顾问的主要内容,如果未能解决你的问题,请参考以下文章