Oracle <left> join on where 先过滤还是先join

一、left join on
on条件是在生成临时表时使用的条件,它不管on中的条件是否为真,都会返回左边表中的记录。


二、left join on and
(1)如果and语句是对左表进行过滤的,那么不管真假都不起任何作用。
(2)如果and语句是对右表过滤的,那么左表所有记录都返回,右表筛选以后再与左表连接返回。 说明and 也是先筛选,这个筛选有可能用得上index的,不过所有记录都要join,cost还是大


三、left join on where
where条件是在临时表生成好后,再对临时表进行过滤的条件。这时已经没有left join的含义(必须返回左边表的记录)了。-- where 才是 join 后过滤
(1)此时相当于inner join on
(2)此时on后的条件用来生成左右表关联的临时表,where后的条件对临时表中的记录进行过滤。


四、inner join on and 和 inner join on where
无区别,不管是对左表还是右表进行筛选,on and 和 on where都会对生成的临时表进行过滤。--为什么是对生成的过滤不是 先过滤再join?我这文档最后的测试结果先是filter

and where 应区别如何使用?

我们经常使用 join相关语句做关联查询,那么在join连接方式后边,on 结合and 和 where结果会发生什么变化呢?

在使用 join on 时 注意 and where 区别和如何使用

join on and

join on and 方式 类似于 on 条件1 and on 条件2,都是 基于join 关联两个表结果 ,取出关联后数据。 举例如下

select t2.object_id t2_id from t1 right join t2 on t1.object_id=t2.object_id and t1.object_id=1989;


92937 rows selected.
Elapsed: 00:00:05.33
Execution Plan
 (1)| 00:00:05 |
|*  1 |  HASH JOIN RIGHT OUTER|        |   102K|  2609K|   372     (1)| 00:00:05 |
|*  2 |   INDEX RANGE SCAN    | T1_IDX |     1 |    13 |     1     (0)| 00:00:01 |
|   3 |   TABLE ACCESS FULL   | T2     |   102K|  1304K|   371     (1)| 00:00:05 |

Predicate Information (identified by operation id):

   1 - access("T1"."OBJECT_ID"(+)="T2"."OBJECT_ID")  -- t1 右连接 t2的 object_id,以t2输出为主,也就是输出t2所有内容
   2 - access("T1"."OBJECT_ID"(+)=1989)  --同上,输出1989所有内容(这个条件在此可以忽略)
Note
   - dynamic sampling used for this statement (level=2)
Statistics

     16  recursive calls
      0  db block gets
       7580  consistent gets
      1  physical reads
      0  redo size
    1699670  bytes sent via SQL*Net to client
      68668  bytes received via SQL*Net from client
       6197  SQL*Net roundtrips to/from client
      0  sorts (memory)
      0  sorts (disk)
      92937  rows processed1

join on where

join on where ,是 join on连接之后对结果再进行筛选(为达到执行效率最有,是先进行where条件筛选,再join关联),举例如下

SQL> select t2.object_id t2_id from t1 right join t2 on t1.object_id=t2.object_id where t1.object_id=1989;
     T2_ID

      1989

一条!!!
Elapsed: 00:00:00.06
Execution Plan
   2 - access("T2"."OBJECT_ID"=1989)
   4 - access("T1"."OBJECT_ID"=1989)
Note

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

      0  db block gets
    147  consistent gets
      3  physical reads
      0  redo size
    524  bytes sent via SQL*Net to client
    523  bytes received via SQL*Net from client
      2  SQL*Net roundtrips to/from client
      1  sorts (memory)
      0  sorts (disk)
      1  rows processed

-------inner join 时都是先过滤再join 

------------left join 时 and 是不过滤的 where 是先过滤 再join

----创建index 后 where 可以使用index ,and不能使用index 说明where 可以先过滤

create index AF on A1 (OBJECT_ID)

  • 11
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值