SQL ORACLE,表的左连接和子查询的左连接之间的区别
我正试图优化前同事的一些SQL代码,结果出现了一些问题。我想知道是左连接到整个表还是左连接到只需要子查询创建列的表的一部分更好 有没有办法测试这两种情况之间的性能? 例如:SQL ORACLE,表的左连接和子查询的左连接之间的区别,sql,oracle,left-join,Sql,Oracle,Left Join,我正试图优化前同事的一些SQL代码,结果出现了一些问题。我想知道是左连接到整个表还是左连接到只需要子查询创建列的表的一部分更好 有没有办法测试这两种情况之间的性能? 例如: 在这种情况下没有区别。您可以通过对这两个查询进行验证。有很多方法可以做到这一点 在问答网站上分享计划的一个好方法是: 在运行查询之前,请关闭ServerOutput 使用“收集计划统计信息”提示运行查询 通过调用dbms\u xplan.display\u cursor获取它们的执行计划 这样做,您将看到: create t
在这种情况下没有区别。您可以通过对这两个查询进行验证。有很多方法可以做到这一点 在问答网站上分享计划的一个好方法是: 在运行查询之前,请关闭ServerOutput 使用“收集计划统计信息”提示运行查询 通过调用dbms\u xplan.display\u cursor获取它们的执行计划 这样做,您将看到:
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)
请注意:
对于两个查询1267695137,计划哈希值相同
计划的“开始”、“A行”和“缓冲区”列中的值相同
=>查询使用了相同的计划并完成了相同的工作量。信任优化器,执行查询1!没有区别,但是如果您想知道,请查看执行计划oracle文档告诉您了什么?
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)