Most of us face with similar issue on our SQL which are running on our databases. Sometimes some SQL could choose different plan and performance goes to worst. Normally with correct plan SQL took 5 min, but with wrong plan choose it took 20 minutes. In this article I am trying to explain how we can set execution plan for sql. Before start I can say there are some problem on 10g level such as even We set plan ,sql could use different plan. We have been opened SR for this issue and finally Support suggest us to use MOS 445126.1 for 10g. You can try below steps for 11g and also 10g .

My db version is 2 node RAC system.

Here is the steps:

1. Firstly We need to know our SQL’s SQL_ID. This SQL_ID information will be use at step 2. For can find SQL_ID you can use

– OEM screen
– v$sql and v$sqlarea views

In this example, my SQL_ID is dp2sc627nnpd4

2. In this steps We will use coe_xfr_sql_profile.sql which maden by  Carlos Sierra. This sql also avaliable at metalink.

You can use this sql please click on  coe_xfr_sql_profile.sql for can get this sql

3. Now as sysdba I am invoking coe_xfr_sql_profile.sql. While I am running this sql, it will ask me some question such as:

As oracle user:

$cd /tmp
$ sqlplus “/as sysdba”
SQL> @coe_xfr_sql_profile.sql

Parameter 1:
SQL_ID (required)

Enter value for 1: dp2sc627nnpd4 — this info comes from step 1
————— ———–
436961061 10.3
2424534850 1354.994

— As you can see there are 2 plan_hash_value avaliable for this sql. I need to fix first one which give me best AVG_ET_SEC

Parameter 2:
PLAN_HASH_VALUE (required)

Enter value for 2: 436961061

Values passed:
SQL_ID : “dp2sc627nnpd4”
PLAN_HASH_VALUE: “436961061”
Execute coe_xfr_sql_profile_dp2sc627nnpd4_436961061.sql
on TARGET system in order to create a custom SQL Profile
with plan 436961061 linked to adjusted sql_text.

4. After We complate step 3 coe_xfr_sql_profile.sql will be create
coe_xfr_sql_profile_dp2sc627nnpd4_436961061.sql output file under /tmp

(In step 3 I  used cd /tmp command,so file has been created in this path)

5. coe_xfr_sql_profile_dp2sc627nnpd4_436961061.sql file open with vi editor. We need to change force_match parameter as TRUE

Orginal state
category => ‘DEFAULT’,
validate => TRUE,
replace => TRUE,
force_match => FALSE /* TRUE:FORCE (match even when different literals in SQL). FALSE:EXACT (similar to CURSOR_SHARING) */ );

After edit
category => ‘DEFAULT’,
validate => TRUE,
replace => TRUE,
force_match => TRUE /* TRUE:FORCE (match even when different literals in SQL). FALSE:EXACT (similar to CURSOR_SHARING) */ );

6. Now We need to run coe_xfr_sql_profile_dp2sc627nnpd4_436961061.sql for can fix plan for my sql.

$cd /tmp
$ sqlplus “/as sysdba”
SQL> @coe_xfr_sql_profile_dp2sc627nnpd4_436961061.sql