PLSQL_性能优化工具系列06_SQL Profile

2013-10-10 Created By BaoXinjian

一、摘要


SQL Tuning Advisor简介

Oracle 10g的查询优化器具有自动SQL调整功能。

1. SQL Tuning Advisor有两个模式:普通模式和调整(tuning)模式。

普通模式类似Oracle以往版本的优化器,SQL语句经过编译,生成一个运行计划,而且所需时间很短。

调整模式可以为SQL语句生成推荐的操作及调整,该功能可以为将来运行该SQL语句产生更好的运行计划。

2. SQL Tuning Advisor可以分析以下几种SQL来源:

(1). 通过ADDM发现的高负载的SQL

(2). 仍然处于缓存中的SQL

(3). 来自AWR中的SQL

(4). 用户定义的SQL系列

3. SQL Tuning Advisor会进行下列四种类型的分析:

(1). 统计数据分析

ATO(自动调整优化器)会检查统计数据是否存在,是否过时

(2). SQL Profiling(模仿)

SQL模仿采用其它方法检验其在统计数据分析阶段进行的分析,并可能采用各种优化提示,来尝试多种执行方案

(3). 访问路径分析

这个分析可能会建议增加一个或多个索引来提高性能

(4). SQL结构分析

这种分析可能会找出SQL种的潜在的编码错误或者可能会产生不良计划的SQL结构,例如可能会建议用NOT IN代替NOT EXISTS。

4. SQL调整和DBMS_SQLTUNE包。

DBMS_SQLTUNE包包含了下面3种类型

(1). SQL调整任务管理

(2). SQL配置文件管理

(3). STS(SQL调整集)管理

5.Waht is a SQL Profile

The SQL profile does not contain information about individual execution plans. Rather, the optimizer has the following sources of information when choosing plans:

(1). The environment, which contains the database configuration, bind variable values, optimizer statistics, data set, and so on

(2). The supplemental statistics in the SQL profile 

6.How can the scope of the SQL Profile be controlled

SQL Profile通过Category 属性进行控制,理论上,如果不指定category, 所有的SQLTunning后的Profile都采存放在Default Category下

select category,name from dba_sql_profiles;

7. To what statements can a SQL Profile be applied - SQL Profile在哪些情况下可以被使用

UELECT statements

(2). UPDATE statements

(3). INSERT statements (only with a SELECT clause)

(4). DELETE statements

(5). CREATE TABLE statements (only with the AS SELECT clause)

(6). MERGE statements (the update or insert operations) 

 

二、语法


1. Accepting a SQL Profile

DECLARE my_sqlprofile_name VARCHAR2(30); BEGIN my_sqlprofile_name := DBMS_SQLTUNE.ACCEPT_SQL_PROFILE ( task_name => 'my_sql_tuning_task', name => 'my_sql_profile'); END;

a. my_sql_tuning_task is the name of the SQL tuning task.

b. You can view information about a SQL Profile in the DBA_SQL_PROFILES view.

2. Altering a SQL Profile

BEGIN DBMS_SQLTUNE.ALTER_SQL_PROFILE( name => 'my_sql_profile', attribute_name => 'STATUS', value => 'DISABLED'); END; 

a. my_sql_profile is the name of the SQL Profile that you want to alter.

b. The status attribute is changed to disabled which means the SQL Profile will not be used during SQL compilation.

3. Droping a SQL Profile

begin DBMS_SQLTUNE.DROP_SQL_PROFILE(name => 'my_sql_profile'); end;

4. How do I produce a report of every tuning set

SELECT 'SELECT d.id , d.owner , d.description , d.created , d.last_modified , d.statement_count, ss.* FROM TABLE(DBMS_SQLTUNE.select_sqlset ('''||name||''')) ss, dba_sqlset d WHERE d.name='''||name||''';'

  FROM dba_sqlset d ORDER BY d.last_modified DESC

 

三、案例


案例:创建测试表,测试数据,创建表中索引,统计信息导入,通过no_index hint query执行

Step1. 创建测试表test

SQL> create table test (n number ); Table created.

Step2. 创建表中测试数据

SQL> declare

     begin

       for i in 1 .. 10000 loop insert into test values(i); commit; end loop; end; / PL/SQL procedure successfully completed.

Step3. 创建索引test_idx

SQL> create index test_idx on test(n); Index created.

Step4. 收集统计信息

SQL> exec dbms_stats.gather_table_stats('','TEST'); PL/SQL procedure successfully completed.

Step5. 通过no_index查询数据,发现数据时不走索引,走全表扫扫描

set autotrace on

select /*+ no_index(test test_idx) */ * from test where n=1; Plan hash value: 217508114

--------------------------------------------------------------------------

| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |

--------------------------------------------------------------------------

|   0 | SELECT STATEMENT  |      |     1 |     4 |     4   (0)| 00:00:01 |

|*  1 |  TABLE ACCESS FULL| TEST |     1 |     4 |     4   (0)| 00:00:01 |

-------------------------------------------------------------------------- Predicate Information (identified by operation id): ---------------------------------------------------

   1 - filter("N"=1)

Step6. 通过DBMS_SQLTUNE进行分析初始化

declare my_task_name VARCHAR2(30); my_sqltext CLOB; begin my_sqltext := 'select /*+ no_index(test test_idx) */ * from test where n=1'; my_task_name := DBMS_SQLTUNE.CREATE_TUNING_TASK( sql_text => my_sqltext, user_name => 'SCOTT', scope => 'COMPREHENSIVE', time_limit => 60, task_name => 'my_sql_tuning_task_2', description => 'Task to tune a query on a specified table'); end; / PL/SQL procedure successfully completed.

Step7. 通过DBMS_SQLTUNE进行分析执行

begin DBMS_SQLTUNE.EXECUTE_TUNING_TASK( task_name => 'my_sql_tuning_task_2'); end; / PL/SQL procedure successfully completed.

Step8. 通过DBMS_SQLTUNE进行分析,产生性能报告

a. report tuning task

set long 10000

set longchunksize 1000

set linesize 100

set heading off

SELECT DBMS_SQLTUNE.REPORT_TUNING_TASK( 'my_sql_tuning_task_2') from DUAL; set heading on

b. general inoformation section, 比较原始的解析计划和推荐的解析计划

GENERAL INFORMATION SECTION -------------------------------------------------------------------------------

Tuning Task Name : my_sql_tuning_task_2 Tuning Task Owner : SYS Workload Type : Single SQL Statement Scope : COMPREHENSIVE Time Limit(seconds): 60 Completion Status : COMPLETED Started at : 09/24/2012 12:36:44 Completed at : 09/24/2012 12:36:49

-------------------------------------------------------------------------------

Schema Name: SCOTT SQL ID : d4wgpc5g0s0vu SQL Text   : select /*+ no_index(test test_idx) */ * from test where n=1

-------------------------------------------------------------------------------

FINDINGS SECTION (1 finding) -------------------------------------------------------------------------------

1- SQL Profile Finding (see explain plans section below) --------------------------------------------------------

1- SQL Profile Finding (see explain plans section below) --------------------------------------------------------

  A potentially better execution plan was found for this statement. Recommendation (estimated benefit: 90.95%) ------------------------------------------

  - Consider accepting the recommended SQL profile. execute dbms_sqltune.accept_sql_profile(task_name =>

            'my_sql_tuning_task_2', task_owner => 'SYS', replace => TRUE); Validation results ------------------

  The SQL profile was tested by executing both its plan and the original plan

  and measuring their respective execution statistics. A plan may have been only partially executed if the other could be run to completion in less time. Original Plan  With SQL Profile  % Improved ------------- ---------------- ----------

 Completion Status: COMPLETE COMPLETE Elapsed Time (s): .001004           .000331      67.03 % CPU Time (s): .001                 0        100 %

  User I/O Time (s):                  0                 0 Buffer Gets: 22                 2       90.9 % Physical Read Requests:             0                 0 Physical Write Requests: 0                 0 Physical Read Bytes:                0                 0 Physical Write Bytes: 0                 0 Rows Processed: 1                 1 Fetches: 1                 1 Executions: 1                 1 Notes -----

  1. Statistics for the original plan were averaged over 10 executions. 2. Statistics for the SQL profile plan were averaged over 10 executions. -------------------------------------------------------------------------------

EXPLAIN PLANS SECTION -------------------------------------------------------------------------------



1- Original With Adjusted Cost ------------------------------

Plan hash value: 217508114



--------------------------------------------------------------------------

| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |

--------------------------------------------------------------------------

|   0 | SELECT STATEMENT  |      |     1 |     4 |     4   (0)| 00:00:01 |

|*  1 |  TABLE ACCESS FULL| TEST |     1 |     4 |     4   (0)| 00:00:01 |

--------------------------------------------------------------------------

 Predicate Information (identified by operation id): ---------------------------------------------------



   1 - filter("N"=1) 2- Using SQL Profile --------------------

Plan hash value: 1416057887



-----------------------------------------------------------------------------

| Id  | Operation        | Name     | Rows  | Bytes | Cost (%CPU)| Time     |

-----------------------------------------------------------------------------

|   0 | SELECT STATEMENT |          |     1 |     4 |     1   (0)| 00:00:01 |

|*  1 |  INDEX RANGE SCAN| TEST_IDX |     1 |     4 |     1   (0)| 00:00:01 |

-----------------------------------------------------------------------------

 Predicate Information (identified by operation id): ---------------------------------------------------



   1 - access("N"=1) -------------------------------------------------------------------------------

Step9. 如果分析推荐的解析计划,性能跟高的话,通过accept_sql_profile接收该统计计划

DECLARE my_sqlprofile_name VARCHAR2(30); begin my_sqlprofile_name := DBMS_SQLTUNE.ACCEPT_SQL_PROFILE ( task_name => 'my_sql_tuning_task_2', name => 'my_sql_profile'); end; / PL/SQL procedure successfully completed.

Step10. 在新的session中,在执行原始SQL,系统已经忽略了hint,继续用索引进行扫描

SQL> set autotrace on SQL> select /*+ no_index(test test_idx) */ * from test where n=1; Execution Plan                                                                

------------------------------------------------------------------------- 

Plan hash value: 1416057887                                                   

                                                                              

----------------------------------------------------------------------------- 

| Id  | Operation        | Name     | Rows  | Bytes | Cost (%CPU)| Time     | 

----------------------------------------------------------------------------- 

|   0 | SELECT STATEMENT |          |     1 |     4 |     1   (0)| 00:00:01 | 

|*  1 |  INDEX RANGE SCAN| TEST_IDX |     1 |     4 |     1   (0)| 00:00:01 | 

----------------------------------------------------------------------------- 

 Predicate Information (identified by operation id): --------------------------------------------------- 

                                                                              

   1 - access("N"=1) --------------- 

Note ----- 

   - SQL profile "my_sql_profile" used for this statement                     

 

Thanks and Regards

参考:Oracle - Metalink: 271196.1

你可能感兴趣的:(profile)