Subquery Transformation [message #541112] |
Fri, 27 January 2012 12:16 |
|
Rahm
Messages: 11 Registered: July 2011
|
Junior Member |
|
|
I've issued the following SQL statement :
SELECT last_name , salary , department_id
FROM employees
WHERE department_id IN (SELECT department_id
FROM departments
WHERE department_name IN ('Executive' , 'Finance' , 'Sales'))
And that's the execution plan :
Execution Plan
----------------------------------------------------------
Plan hash value: 1737988890
-----------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 29 | 870 | 6 (17)| 00:00:01 |
|* 1 | HASH JOIN | | 29 | 870 | 6 (17)| 00:00:01 |
| 2 | INLIST ITERATOR | | | | | |
| 3 | TABLE ACCESS BY INDEX ROWID| DEPARTMENTS | 3 | 48 | 2 (0)| 00:00:01 |
|* 4 | INDEX RANGE SCAN | DEP_DNAME_IDX | 3 | | 1 (0)| 00:00:01 |
| 5 | TABLE ACCESS FULL | EMPLOYEES | 107 | 1498 | 3 (0)| 00:00:01 |
-----------------------------------------------------------------------------------------------
Is it possible to use hints (such as USE_NL) in order to instruct the optimizer to use different transformation ?
Thank you.
|
|
|
|
|
|
|
|
|
Re: Subquery Transformation [message #541122 is a reply to message #541119] |
Fri, 27 January 2012 13:15 |
John Watson
Messages: 8962 Registered: January 2010 Location: Global Village
|
Senior Member |
|
|
Quote:Ok, so lets say my Subquery transforms into a Nested Loop, now I want to explicitly change the execution plan so it will choose Hash Join. Is it possible ? Of course it is. Use hints, as you said in your first post.
|
|
|
|
|
Re: Subquery Transformation [message #541126 is a reply to message #541124] |
Fri, 27 January 2012 13:31 |
John Watson
Messages: 8962 Registered: January 2010 Location: Global Village
|
Senior Member |
|
|
It works for me:
orcl> set autot traceonly exp
orcl> SELECT /*+ USE_HASH (e d) */ e.last_name , e.salary , e.department_id
2 FROM employees e
3 WHERE department_id IN (SELECT d.department_id
4 FROM departments d
5 WHERE d.department_name IN ('Executive' , 'Finance' , 'Sales'));
Execution Plan
----------------------------------------------------------
Plan hash value: 2052257371
----------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 29 | 899 | 7 (15)| 00:00:01 |
|* 1 | HASH JOIN | | 29 | 899 | 7 (15)| 00:00:01 |
|* 2 | TABLE ACCESS FULL| DEPARTMENTS | 3 | 48 | 3 (0)| 00:00:01 |
| 3 | TABLE ACCESS FULL| EMPLOYEES | 107 | 1605 | 3 (0)| 00:00:01 |
----------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("DEPARTMENT_ID"="D"."DEPARTMENT_ID")
2 - filter("D"."DEPARTMENT_NAME"='Executive' OR
"D"."DEPARTMENT_NAME"='Finance' OR "D"."DEPARTMENT_NAME"='Sales')
orcl> SELECT /*+ USE_NL (e d) */ e.last_name , e.salary , e.department_id
2 FROM employees e
3 WHERE department_id IN (SELECT d.department_id
4 FROM departments d
5 WHERE d.department_name IN ('Executive' , 'Finance' , 'Sales'));
Execution Plan
----------------------------------------------------------
Plan hash value: 1021246405
--------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 29 | 899 | 4 (0)| 00:00:01 |
| 1 | NESTED LOOPS | | | | | |
| 2 | NESTED LOOPS | | 29 | 899 | 4 (0)| 00:00:01 |
|* 3 | TABLE ACCESS FULL | DEPARTMENTS | 3 | 48 | 3 (0)| 00:00:01 |
|* 4 | INDEX RANGE SCAN | EMP_DEPARTMENT_IX | 10 | | 0 (0)| 00:00:01 |
| 5 | TABLE ACCESS BY INDEX ROWID| EMPLOYEES | 10 | 150 | 1 (0)| 00:00:01 |
--------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - filter("D"."DEPARTMENT_NAME"='Executive' OR "D"."DEPARTMENT_NAME"='Finance' OR
"D"."DEPARTMENT_NAME"='Sales')
4 - access("DEPARTMENT_ID"="D"."DEPARTMENT_ID")
orcl>
|
|
|
|
|
|
Re: Subquery Transformation [message #541148 is a reply to message #541130] |
Fri, 27 January 2012 23:53 |
|
Michel Cadot
Messages: 68729 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
Yes/no is irrelevant in REAL word but you can live in your thought world if you want.
I just want no one follow your psychotic way and warn future readers.
Regards
Michel
|
|
|