null 值,谓词下推与 join 转换总结

null 值的影响

结论

target 中的 null 及相关计算就是 null
on/where 条件中的 null 及相关计算是 false 不是null
is null,is not null 判断在 target 与 on/where 条件中均表示 true 或 false,不是 null。

举例

  • where x = null => false
  • where null => false
  • where null_column > 0 => false
  • where null_column is null => true
  • where null > 0 => false
  • select null > 0 => null

谓词下推

考虑条件

1、where 条件还是 on 条件
2、条件是 join 列还是百 join 列
3、条件涉及 join 的 base 表还是 join 表

举例

  • t1 left join t2 on t1.x = t2.x where t1.x > 0; 可下推
  • t1 left join t2 on t1.x = t2.x where t1.y > 0; 可下推
  • t1 left join t2 on t1.x = t2.x where t2.x > 0; 不可下推
  • t1 left join t2 on t1.x = t2.x where t2.y > 0; 不可下推
  • t1 left join t2 on t1.x = t2.x and t1.x > 0; 不可下推
  • t1 left join t2 on t1.x = t2.x and t1.y > 0; 不可下推
  • t1 left join t2 on t1.x = t2.x and t2.x > 0; 可下推
  • t1 left join t2 on t1.x = t2.x and t2.y > 0; 可下推

结论

无论 join 列还是百 join 列,where 条件可以下推 base 表,on 条件可以下推 join 表。

join 转化

1 cross join => inner join

存在 where condition 时可以直接转换
例:t1,t2 where t1.x=t2.y => t1 inner join t2 on t1.x=t2.y

2 outer join => inner join

1、存在 join 表的 join 列作 where 条件时可直接转换(因为假设 join 列为 null,则 where 条件一定是 false)
例:t1 left join t2 on t1.x=t2.y where t2.y > 0 由于 t2.y>0 一定能过滤 null 值,所以可以转 inner join

2、如果是 on 条件 base 表的列可以转换,join 表的列不可以转换
例1:t1 left join t2 on t1.x=t2.y **and **t1.x > 0,能转,因为 t1.x = null 时可以过滤 t1 join null 的情况
例2:t1 left join t2 on t1.x=t2.y **and **t2.y > 0,不能转,因为 t2.y = null 时不能过滤 t1 join null 的情况

3 full join => left/right join

where 条件可以消去另一侧的表,可直接转换
例:t1 full join t2 on t1.x=t2.y where t1.x > 0,能转 left join,因为 t1.x = null 时可以消去 null join t2 的情况

4 left/right join 转 semi join

一侧的表不需要输出,只在 on 条件中出现,且 join 列为 unique 的
例:select t1.* from t1 left join t2 on t1.x=t2.y。其中 t2.y 是 unique 的列

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值