Reputation: 3
I'm trying to optimize some SQL code from former coworker and something comes up. I want to know if LEFT JOIN to a whole table or LEFT JOIN to a part of table with only needed columns (create by sub query) were better?
Is there any way to test the performance between these two cases? Example:
SELECT A.*, B.COL1, B.COL2, B.COL3
FROM TABLE_A A
LEFT JOIN TABLE_B ON A.ID = B.ID;
SELECT A.*, C.*
FROM TABLE_A
LEFT JOIN
(SELECT B.ID, B.COL1, B.COL2, B.COL3 FROM TABLE_B) C ON C.ID = A.ID
Upvotes: 0
Views: 62
Reputation: 9865
In this instance there's no difference. You can verify this by getting the execution plans for the two queries. There are many ways to do this.
A good technique to get a plan you can share on Q&A sites like this is:
set serverouput off
gather_plan_statistics
hintdbms_xplan.display_cursor
Do this and you'll see:
create table table_a (
id int
);
create table table_b (
id int,
col1 int,
col2 int,
col3 int
);
insert into table_a values ( 1 );
insert into table_a values ( 2 );
insert into table_b values ( 1, 1, 1, 1 );
insert into table_b values ( 3, 3, 3, 3 );
commit;
set serveroutput off
select /*+ gather_plan_statistics */
a.*, b.col1, b.col2, b.col3
from table_a a
left join table_b b
on a.id = b.id;
select *
from table(dbms_xplan.display_cursor(null, null, 'IOSTATS LAST'));
PLAN_TABLE_OUTPUT
SQL_ID 64516xvpa898t, child number 1
-------------------------------------
select /*+ gather_plan_statistics */ a.*, b.col1, b.col2, b.col3
from table_a a left join table_b b on a.id = b.id
Plan hash value: 1267695137
----------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers |
----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | 2 |00:00:00.01 | 14 |
|* 1 | HASH JOIN OUTER | | 1 | 2 | 2 |00:00:00.01 | 14 |
| 2 | TABLE ACCESS FULL| TABLE_A | 1 | 2 | 2 |00:00:00.01 | 7 |
| 3 | TABLE ACCESS FULL| TABLE_B | 1 | 2 | 2 |00:00:00.01 | 7 |
----------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("A"."ID"="B"."ID")
Note
-----
- dynamic sampling used for this statement (level=2)
select /*+ gather_plan_statistics */
a.*, c.*
from table_a a
left join (
select b.id, b.col1, b.col2, b.col3
from table_b b
) c
on c.id = a.id;
select *
from table(dbms_xplan.display_cursor(null, null, 'IOSTATS LAST'));
PLAN_TABLE_OUTPUT
SQL_ID b0abq59kzw8df, child number 0
-------------------------------------
select /*+ gather_plan_statistics */ a.*, c.* from table_a a
left join ( select b.id, b.col1, b.col2, b.col3 from table_b b )
c on c.id = a.id
Plan hash value: 1267695137
----------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers |
----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | 2 |00:00:00.01 | 14 |
|* 1 | HASH JOIN OUTER | | 1 | 2 | 2 |00:00:00.01 | 14 |
| 2 | TABLE ACCESS FULL| TABLE_A | 1 | 2 | 2 |00:00:00.01 | 7 |
| 3 | TABLE ACCESS FULL| TABLE_B | 1 | 2 | 2 |00:00:00.01 | 7 |
----------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("B"."ID"="A"."ID")
Note
-----
- dynamic sampling used for this statement (level=2)
Note that:
=> The queries used the same plan & did the same amount of work.
Upvotes: 1