SQL> create user liushiming identified by liushiming;
User created.
SQL> grant connect,resource to liushiming;
Grant succeeded.
SQL> grant advisor to liushiming;
Grant succeeded.
SQL> conn liushiming/liushiming;
Connected.
SQL> create table bigtable (id number(10),name varchar2(100));
Table created.
SQL> begin
for i in 1..50000 loop
insert into bigtable values(i,'test'||i);
end loop;
end;
/
PL/SQL procedure successfully completed.
SQL> create table smalltable (id number(10),name varchar2(100));
Table created.
SQL> begin
for i in 1..1000 loop
insert into smalltable values(i,'test'||i);
end loop;
end;
/
PL/SQL procedure successfully completed.
SQL> commit;
Commit complete.
SQL> set autot on
SQL> select a.id,a.name,b.id,b.name from bigtable a,smalltable b where a.id=b.id and a.id=1000;
ID
----------
NAME
--------------------------------------------------------------------------------
ID
----------
NAME
--------------------------------------------------------------------------------
1000
test1000
1000
test1000
Execution Plan
----------------------------------------------------------
Plan hash value: 1703851322
--------------------------------------------------------------------------------
-
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time
|
--------------------------------------------------------------------------------
-
| 0 | SELECT STATEMENT | | 3 | 390 | 60 (4)| 00:00:01
|
|* 1 | HASH JOIN | | 3 | 390 | 60 (4)| 00:00:01
|
|* 2 | TABLE ACCESS FULL| SMALLTABLE | 1 | 65 | 3 (0)| 00:00:01
|
|* 3 | TABLE ACCESS FULL| BIGTABLE | 3 | 195 | 57 (4)| 00:00:01
|
--------------------------------------------------------------------------------
-
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("A"."ID"="B"."ID")
2 - filter("B"."ID"=1000)
3 - filter("A"."ID"=1000)
Note
-----
- dynamic sampling used for this statement
通过DBMS_SQLTUNE包的CREATE_TUNING_TASK来创建一个优化任务,然后通过DBMS_SQLTUNE.EXECUTE_TUNING_TASK来执行调优任务,生成调优建议
SQL> DECLARE
my_task_name VARCHAR2(30);
my_sqltext CLOB;
BEGIN
my_sqltext := 'select a.id,a.name,b.id,b.name from bigtable a,smalltable b where a.id=b.id and a.id=1000';
my_task_name := DBMS_SQLTUNE.CREATE_TUNING_TASK(
sql_text => my_sqltext,
user_name => 'LIUSHIMING',
scope => 'COMPREHENSIVE',
time_limit => 60,
task_name => 'test_sql_tuning_task2',
description => 'Task to tune a query');
DBMS_SQLTUNE.EXECUTE_TUNING_TASK(task_name => 'test_sql_tuning_task2');
END;
/
PL/SQL procedure successfully completed.
执行的过程中,也可以通过user_advisor_tasks或者dba_advisor_tasks来查看调优任务执行的状况
SQL> conn / as sysdba
Connected.
SQL> select task_name,ADVISOR_NAME,STATUS from user_advisor_tasks;
SQL> conn liushiming/liushiming;
SQL> set long 10000
SQL> select dbms_sqltune.report_tuning_task('test_sql_tuning_task2') from dual;
DBMS_SQLTUNE.REPORT_TUNING_TASK('TEST_SQL_TUNING_TASK2')
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
GENERAL INFORMATION SECTION
-------------------------------------------------------------------------------
Tuning Task Name : test_sql_tuning_task2
Tuning Task Owner : LIUSHIMING
Scope : COMPREHENSIVE
Time Limit(seconds) : 60
Completion Status : COMPLETED
Started at : 04/01/2014 15:05:26
Completed at : 04/01/2014 15:05:26
Number of Statistic Findings : 2
Number of SQL Profile Findings : 1
DBMS_SQLTUNE.REPORT_TUNING_TASK('TEST_SQL_TUNING_TASK2')
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Number of Index Findings : 1
-------------------------------------------------------------------------------
Schema Name: LIUSHIMING
SQL ID : 4ak4vqzrr1c1d
SQL Text : select a.id,a.name,b.id,b.name from bigtable a,smalltable b
where a.id=b.id and a.id=1000
-------------------------------------------------------------------------------
FINDINGS SECTION (4 findings)
-------------------------------------------------------------------------------
DBMS_SQLTUNE.REPORT_TUNING_TASK('TEST_SQL_TUNING_TASK2')
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1- Statistics Finding
---------------------
Table "LIUSHIMING"."SMALLTABLE" was not analyzed.
Recommendation
--------------
- Consider collecting optimizer statistics for this table.
execute dbms_stats.gather_table_stats(ownname => 'LIUSHIMING', tabname =>
'SMALLTABLE', estimate_percent => DBMS_STATS.AUTO_SAMPLE_SIZE,
method_opt => 'FOR ALL COLUMNS SIZE AUTO');
DBMS_SQLTUNE.REPORT_TUNING_TASK('TEST_SQL_TUNING_TASK2')
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Rationale
---------
The optimizer requires up-to-date statistics for the table in order to
select a good execution plan.
2- Statistics Finding
---------------------
Table "LIUSHIMING"."BIGTABLE" was not analyzed.
Recommendation
DBMS_SQLTUNE.REPORT_TUNING_TASK('TEST_SQL_TUNING_TASK2')
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------
- Consider collecting optimizer statistics for this table.
execute dbms_stats.gather_table_stats(ownname => 'LIUSHIMING', tabname =>
'BIGTABLE', estimate_percent => DBMS_STATS.AUTO_SAMPLE_SIZE,
method_opt => 'FOR ALL COLUMNS SIZE AUTO');
Rationale
---------
The optimizer requires up-to-date statistics for the table in order to
select a good execution plan.
DBMS_SQLTUNE.REPORT_TUNING_TASK('TEST_SQL_TUNING_TASK2')
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
3- SQL Profile Finding (see explain plans section below)
--------------------------------------------------------
A potentially better execution plan was found for this statement.
Recommendation (estimated benefit<=10%)
---------------------------------------
- Consider accepting the recommended SQL profile.
execute dbms_sqltune.accept_sql_profile(task_name =>
'test_sql_tuning_task2', replace => TRUE);
4- Index Finding (see explain plans section below)
DBMS_SQLTUNE.REPORT_TUNING_TASK('TEST_SQL_TUNING_TASK2')
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------
The execution plan of this statement can be improved by creating one or more
indices.
Recommendation (estimated benefit: 93.34%)
------------------------------------------
- Consider running the Access Advisor to improve the physical schema design
or creating the recommended index.
create index LIUSHIMING.IDX$$_00A80001 on LIUSHIMING.BIGTABLE("ID");
- Consider running the Access Advisor to improve the physical schema design
DBMS_SQLTUNE.REPORT_TUNING_TASK('TEST_SQL_TUNING_TASK2')
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
or creating the recommended index.
create index LIUSHIMING.IDX$$_00A80002 on LIUSHIMING.SMALLTABLE("ID");
Rationale
---------
Creating the recommended indices significantly improves the execution plan
of this statement. However, it might be preferable to run "Access Advisor"
using a representative SQL workload as opposed to a single statement. This
will allow to get comprehensive index recommendations which takes into
account index maintenance overhead and additional space consumption.
DBMS_SQLTUNE.REPORT_TUNING_TASK('TEST_SQL_TUNING_TASK2')
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------
EXPLAIN PLANS SECTION
-------------------------------------------------------------------------------
1- Original With Adjusted Cost
------------------------------
Plan hash value: 1703851322
---------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------
DBMS_SQLTUNE.REPORT_TUNING_TASK('TEST_SQL_TUNING_TASK2')
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 23 | 60 (4)| 00:00:01 |
|* 1 | HASH JOIN | | 1 | 23 | 60 (4)| 00:00:01 |
|* 2 | TABLE ACCESS FULL| SMALLTABLE | 1 | 10 | 3 (0)| 00:00:01 |
|* 3 | TABLE ACCESS FULL| BIGTABLE | 1 | 13 | 57 (4)| 00:00:01 |
---------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("A"."ID"="B"."ID")
2 - filter("B"."ID"=1000)
DBMS_SQLTUNE.REPORT_TUNING_TASK('TEST_SQL_TUNING_TASK2')
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
3 - filter("A"."ID"=1000)
2- Using SQL Profile
--------------------
Plan hash value: 3277188744
---------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 23 | 60 (4)| 00:00:01 |
|* 1 | HASH JOIN | | 1 | 23 | 60 (4)| 00:00:01 |
DBMS_SQLTUNE.REPORT_TUNING_TASK('TEST_SQL_TUNING_TASK2')
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|* 2 | TABLE ACCESS FULL| BIGTABLE | 1 | 13 | 57 (4)| 00:00:01 |
|* 3 | TABLE ACCESS FULL| SMALLTABLE | 1 | 10 | 3 (0)| 00:00:01 |
---------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("A"."ID"="B"."ID")
2 - filter("A"."ID"=1000)
3 - filter("B"."ID"=1000)
DBMS_SQLTUNE.REPORT_TUNING_TASK('TEST_SQL_TUNING_TASK2')
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
3- Using New Indices
--------------------
Plan hash value: 1729327841
------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 23 | 4 (0)| 00:00:01 |
| 1 | MERGE JOIN CARTESIAN | | 1 | 23 | 4 (0)| 00:00:01 |
| 2 | TABLE ACCESS BY INDEX ROWID | BIGTABLE | 1 | 13 | 2 (0)| 00:00:01 |
|* 3 | INDEX RANGE SCAN | IDX$$_00A80001 | 1 | | 1 (0)| 00:00:01 |
DBMS_SQLTUNE.REPORT_TUNING_TASK('TEST_SQL_TUNING_TASK2')
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 4 | BUFFER SORT | | 1 | 10 | 2 (0)| 00:00:01 |
| 5 | TABLE ACCESS BY INDEX ROWID| SMALLTABLE | 1 | 10 | 2 (0)| 00:00:01 |
|* 6 | INDEX RANGE SCAN | IDX$$_00A80002 | 1 | | 1 (0)| 00:00:01 |
------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - access("A"."ID"=1000)
6 - access("B"."ID"=1000)
DBMS_SQLTUNE.REPORT_TUNING_TASK('TEST_SQL_TUNING_TASK2')
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/31448824/viewspace-2139376/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/31448824/viewspace-2139376/