You are on page 1of 7

8/30/2015

changetheexecutionplanwithoutchangingthesqlqueryDBACLASSDBACLASS

DATABASE

ORACLE RAC

GOLDENGATE

DB SCRIPTS

HOME / PERFORMANCE TUNING / CHANGE THE EXECUTION PLAN


WITHOUT CHANGING THE SQL QUERY

Change The Execution


Plan Without Changing
The Sql Query
58 views

Less than a minute

There are few situations where we know the execution plan of a


particular query can be improved with use of proper hint, But if

HOW TO

FORUM

BACKUP & RECOVERY 5


DATABASE

11

DATABASE SCRIPTS

18

DATAGUARD

GOLDENGATE

HOW TO

17

ORACLE 12C

17

ORACLE RAC

ORACLE SECURITY

PERFORMANCE
TUNING

TROUBLESHOOTING

we are not allowed to change the application query in any way(we


cant even add a hint also),then how to achive this?
Example:
For the below query, we want to force it to use parallel 4 Hint
execution plan, But without changing the query.
select count(*) from bigtab where weight in ( select distinct(
weight) from bigtab);

09:33:13 select count(*) from bigtab where weight in ( select

13899072

Elapsed: 00:00:17.29

Questions? Chat with us


http://dbaclass.com/index.php/article/changetheexecutionplanwithoutchangingthesqlquery/

How to archive table


rows in oracle 12c
769 4

how to use
DBMS_PRIVILEGE_CAPTURE
to capture privs in oracle
12c
164 0

1/9

8/30/2015

changetheexecutionplanwithoutchangingthesqlqueryDBACLASSDBACLASS

select * from table (dbms_xplan.display_cursor(null,null,'TYPICAL'


SQL_IDg5ch3zx56jawc, child number 1
------------------------------------select count(*) from bigtab where weight in ( select distinct
from bigtab)

Plan hash value: 1484132691

-----------------------------------------------------------| Id| Operation | Name | Rows| Bytes |TempSpc


-----------------------------------------------------------| 0 | SELECT STATEMENT|| | |
| 1 |SORT AGGREGATE || 1 |26 |
|*2 | HASH JOIN SEMI||10M| 258M| 248M
| 3 |TABLE ACCESS FULL| BIGTAB |10M| 129M|
| 4 |TABLE ACCESS FULL| BIGTAB |10M| 129M|
-----------------------------------------------------------
Predicate Information (identified by operation id):
--------------------------------------------------
2 - access("WEIGHT"="WEIGHT")

Note
---- - dynamic sampling used for this statement (level=2)

26 rows selected.

Elapsed: 00:00:00.03

stats and Publish


Gather
them later in oracle
130 0

change the execution


plan without changing
the sql query
58 0

How to configure
manager process in
golden gate 11g
52 0

How to find sql text from


a sid
49 1

Apply PSU patch on


oracle 12c using
datapatch
44 0

Currently the query is taking around 17 seconds.


Lets create a baseline for this query.

Oracle data guard


concepts

42 0

SQL > variable cnt number;


execute :cnt := dbms_spm.load_plans_from_cursor_cache(sql_id

PL/SQL procedure successfully completed.

Elapsed: 00:00:00.05
Email *

SQL> select sql_handle,sql_text,plan_name,enabled


from
dba_sql_plan_baselines
where
Subscribe!
sql_text like '%bigtab%';09:37:35 209:37:35 309:37:

09:38:02 SQL > 09:38:02 209:38:02 309:38:02 409


SQL_HANDLE SQL_TEXT
------------------------------ ---------------------------SQL_a868c91ef7af57ad select count(*) from bigtab
tab)

Subscribe to our
Newsletter

http://dbaclass.com/index.php/article/changetheexecutionplanwithoutchangingthesqlquery/

2/9

8/30/2015

changetheexecutionplanwithoutchangingthesqlqueryDBACLASSDBACLASS

SQL_a868c91ef7af57ad select count(*) from bigtab


tab)

Elapsed: 00:00:00.01

We can see two plans got loaded. Lets disable these existing
plans.

09:41:16 SQL > exec :cnt := dbms_spm.alter_sql_plan_baseline


plan_name = >
attribute_name
attribute_value

PL/SQL procedure successfully completed.

Elapsed: 00:00:00.05
09:42:49 SQL > exec :cnt := dbms_spm.alter_sql_plan_baseline
plan_name = >
attribute_name
attribute_value

PL/SQL procedure successfully completed.

Elapsed: 00:00:00.01
09:43:02 SQL > select sql_handle,sql_text,plan_name,enabled
from
dba_sql_plan_baselines
where
sql_text like '%bigtab%';09:43:05 209:43:05 309:43:

SQL_HANDLE SQL_TEXT
------------------------------ ---------------------------SQL_a868c91ef7af57ad select count(*) from bigtab
tab)

SQL_a868c91ef7af57ad select count(*) from bigtab


tab)

Elapsed: 00:00:00.01

Lets run the the same query with parallel hint and get the
execution plan.

09:33:37 SQL > select /*+ parallel (4) */ count(*) from bigtab
13899072

Elapsed: 00:00:07.41
09:33:57 SQL > select * from table (dbms_xplan.display_cursor
SQL_IDgq322uk9152yg, child number 0
http://dbaclass.com/index.php/article/changetheexecutionplanwithoutchangingthesqlquery/

3/9

8/30/2015

changetheexecutionplanwithoutchangingthesqlqueryDBACLASSDBACLASS

------------------------------------select /*+ parallel (4) */ count(*) from bigtab where weight


select distinct( weight)from bigtab)

Plan hash value: 1244248794

-----------------------------------------------------------| Id| Operation | Name | Rows| Bytes


-----------------------------------------------------------| 0 | SELECT STATEMENT|| |
| 1 |SORT AGGREGATE || 1 |26
| 2 | PX COORDINATOR|| |
| 3 |PX SEND QC (RANDOM)| :TQ10002 | 1 |26
| 4 | SORT AGGREGATE|| 1 |26
|*5 |HASH JOIN SEMI ||10M| 258M
| 6 | PX RECEIVE||10M| 129M
| 7 |PX SEND HASH | :TQ10000 |10M| 129M
| 8 | PX BLOCK ITERATOR ||10M| 129M
|*9 |TABLE ACCESS FULL| BIGTAB |10M| 129M
|10 | PX RECEIVE||10M| 129M
|11 |PX SEND HASH | :TQ10001 |10M| 129M
|12 | PX BLOCK ITERATOR ||10M| 129M
|* 13 |TABLE ACCESS FULL| BIGTAB |10M| 129M
-----------------------------------------------------------
Predicate Information (identified by operation id):
--------------------------------------------------
5 - access("WEIGHT"="WEIGHT")
9 - access(:Z>=:Z AND :Z<=:Z) 13 - access(:Z>=:Z AND :

Note
---- - dynamic sampling used for this statement (level=2)
- Degree of Parallelism is 4 because of hint

38 rows selected.

Elapsed: 00:00:00.02

So for the query with parallel hint, you can get these two details
from the above execution plan.
SQL ID: gq322uk9152yg
Plan hash value: 1244248794
Now load these two values to the existing sql handle id
SQL_a868c91ef7af57ad

09:43:02 SQL > select sql_handle,sql_text,plan_name,enabled


from
dba_sql_plan_baselines
http://dbaclass.com/index.php/article/changetheexecutionplanwithoutchangingthesqlquery/

4/9

8/30/2015

changetheexecutionplanwithoutchangingthesqlqueryDBACLASSDBACLASS

where
sql_text like '%bigtab%';09:43:05 209:43:05 309:43:

SQL_HANDLE SQL_TEXT
------------------------------ ---------------------------SQL_a868c91ef7af57ad select count(*) from bigtab
tab)

SQL_a868c91ef7af57ad select count(*) from bigtab


tab)

Elapsed: 00:00:00.01

09:47:45 SQL > execute :cnt := dbms_spm.load_plans_from_cursor_cache


plan_hash_value
sql_handle
/

PL/SQL procedure successfully completed.

Elapsed: 00:00:00.06

09:50:43 SQL > select sql_handle,sql_text,plan_name,enabled


from
dba_sql_plan_baselines
where
sql_text like '%bigtab%';09:50:49 209:50:49 309:50:

SQL_HANDLE SQL_TEXT
------------------------------ ---------------------------SQL_a868c91ef7af57ad select count(*) from bigtab
tab)

SQL_a868c91ef7af57ad select count(*) from bigtab


tab)

SQL_a868c91ef7af57ad select count(*) from bigtab


tab)

Elapsed: 00:00:00.01

We can see a new plan has been enabled for this sql. This is the
parallel hinted plan.
Lets check the explain plan of the orginal query and verify
whether it is using the parallel hinted execution plan or not.

09:50:49 SQL > select count(*) from bigtab where weight in


http://dbaclass.com/index.php/article/changetheexecutionplanwithoutchangingthesqlquery/

5/9

8/30/2015

changetheexecutionplanwithoutchangingthesqlqueryDBACLASSDBACLASS

COUNT(*)
---------13899072

Elapsed: 00:00:10.98
09:51:25 SQL > explain plan for
09:51:32 2 select count(*) from bigtab where weight in

Explained.

Elapsed: 00:00:00.03
09:51:35 SQL > select plan_table_output from
table(dbms_xplan.display('plan_table',null,'typical -cost -bytes'

PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------Plan hash value: 1244248794

-----------------------------------------------------------| Id| Operation | Name | Rows| Time


-----------------------------------------------------------| 0 | SELECT STATEMENT|| 1 | 00:00
| 1 |SORT AGGREGATE || 1 |
| 2 | PX COORDINATOR|| |
| 3 |PX SEND QC (RANDOM)| :TQ10002 | 1 |
| 4 | SORT AGGREGATE|| 1 |
|*5 |HASH JOIN SEMI ||3847K| 00:00
| 6 | PX RECEIVE||3847K| 00:00
| 7 |PX SEND HASH | :TQ10000 |3847K| 00:00
| 8 | PX BLOCK ITERATOR ||3847K| 00:00
| 9 |TABLE ACCESS FULL| BIGTAB |3847K| 00:00
|10 | PX RECEIVE||3847K| 00:00
|11 |PX SEND HASH | :TQ10001 |3847K| 00:00
|12 | PX BLOCK ITERATOR ||3847K| 00:00
|13 |TABLE ACCESS FULL| BIGTAB |3847K| 00:00
-----------------------------------------------------------
Predicate Information (identified by operation id):
--------------------------------------------------
5 - access("WEIGHT"="WEIGHT")

Note
---- - Degree of Parallelism is 4 because of hint
- SQL plan baseline "SQL_PLAN_ahu693vvuypxd6e73d422" used

30 rows selected.

Elapsed: 00:00:00.03

Now it is using the parallel hint execution plan, even if we havent


used that hint in the query.

http://dbaclass.com/index.php/article/changetheexecutionplanwithoutchangingthesqlquery/

6/9

8/30/2015

changetheexecutionplanwithoutchangingthesqlqueryDBACLASSDBACLASS

PERFORMANCE TUNING

About admin
View all posts by admin

Related Articles
How to export and import statistics in oracle
Gather stats and Publish them later in oracle
Get explain plans of a sql query from AWR using
DBMS_XPLAN.DISPLAY_AWR

Realtime monitoring of sql query using v$sql_plan_monitor

Leave a Reply
Your email address will not be published. Required fields are marked
*

Name *

Email *

Website

http://dbaclass.com/index.php/article/changetheexecutionplanwithoutchangingthesqlquery/

7/9

You might also like