SQL Profiles的force_match参数在不改变代码的情况下解决没有使用绑定变量的问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/EVISWANG/article/details/81474357

How To Use SQL Profiles for Queries Using Different Literals Using the Force_Match Parameter of DBMS_SQLTUNE.ACCEPT_SQL_PROFILE (Doc ID 1253696.1)

如何使用SQL Profiles的force_match参数在不改变代码的情况下解决没有使用绑定变量的问题

一、创建表,索引,收集统计信息

建表:

create table test (n number );

填数:

declare

begin

for i in 1 .. 10000

loop

insert into test values(i);

commit;

end loop;

end;

/

创建索引:

create index test_idx on test(n);

收集统计信息:

analyze table test estimate statistics ;

二、 打开执行计划收集

set autotrace on

查询n=1

select /*+ no_index(test test_idx) */ * from test where n=1;

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

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

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

| 0 | SELECT STATEMENT | | 1 | 3 | 6 (0)| 00:00:01 |

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

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

三、 用 SQL Tuning Advisor 为以上查询创建一个profile

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;

/

SQL> exec DBMS_SQLTUNE.EXECUTE_TUNING_TASK( task_name => 'my_sql_tuning_task_2');

PL/SQL procedure successfully completed.

SQL> set long 2000

SQL> SELECT DBMS_SQLTUNE.REPORT_TUNING_TASK( 'my_sql_tuning_task_2') from DUAL;

DBMS_SQLTUNE.REPORT_TUNING_TASK('MY_SQL_TUNING_TASK_2')

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

A potentially better execution plan was found for this statement.

Recommendation (estimated benefit: 83.98%)

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

- Consider accepting the recommended SQL profile.

execute dbms_sqltune.accept_sql_profile(task_name =>

'my_sql_tuning_task_2', replace => TRUE);

四、如果接受优化后的建议,执行如下:

SQL> execute dbms_sqltune.accept_sql_profile(task_name =>'my_sql_tuning_task_2', replace => TRUE);

在执行select查询发现:

SQL> select /*+ no_index(test test_idx) */ * from test where n=1;

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

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

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

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

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

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

但是当我们执行n=2时

SQL> select /*+ no_index(test test_idx) */ * from test where n=2;

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

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

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

| 0 | SELECT STATEMENT | | 1 | 3 | 6 (0)| 00:00:01 |

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

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

五、这时候我们就要对DBMS_SQLTUNE.ACCEPT_SQL_PROFILE使用Force_Match参数了

SQL> execute dbms_sqltune.accept_sql_profile(task_name =>'my_sql_tuning_task_2', replace => TRUE, force_match=>true);

SQL> select /*+ no_index(test test_idx) */ * from test where n=10;

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

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

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

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

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

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

SQL profiles created with FORCE_MATCH enabled does not recognize the addition of a comment to the SQL statement.  

If a comment is added to the SQL statement, then the SQL profile will not match to it. 

To overcome the problem, Patch 9488694 can be applied so that the comments are ignored and the SQL profiles will match the signature and get used.

猜你喜欢

转载自blog.csdn.net/EVISWANG/article/details/81474357