前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >oracle中is not null,oracle之is null和is not null的优化「建议收藏」

oracle中is not null,oracle之is null和is not null的优化「建议收藏」

作者头像
全栈程序员站长
发布2022-09-27 11:13:10
2.5K0
发布2022-09-27 11:13:10
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君

oracle之优化is null语句

一:is null的优化

方法:通过nvl(字段,j)=j的方式,将字段中为空的数据转化为j,从而正常使用索引。

具体实现条件则是:i is null <===> j = nvl(i,j);

注意:使用时必须要确保字段的数据不包含j,例如:(age,15)=15,此时有可能age

内容是15,此时不可以,j的值要变换,保证不再age的范围之内。

函数介绍:

nvl(a,b,c…….):当a为空时取b,当b为空时取c,以此类推。

当然还有另外一种方式解决这个问题:将null包含到索引中

–使用nvl函数的方式(不用添加索引,推荐)

select * from student t where 1=nvl(t.age,1);

–当t.age不存在等于1的数据时等价于

–select * from student t where t.age is null;

–添加索引的方式

create index idx_age_x on tab_i(decode(age,null,1));

select * from student t where decode(t.age,null,1)=1;

二:is not null的优化

方法:结果集不包含j = nvl(i,j)即可

通常情况下使用not exists或者比较大小

示例:

1:not exists

select * from student t where not exists

(select 1 form student s where 1=nvl(s.age,1));

–11g版本后not in和not exists趋于相似,也可以用not in

–当t.col_x不存在等于1的数据时等价于

–select * from student t where t.age is not null;

2:比较大小

–当t.age为总是大于1的数值时

select * from student t where 1

–当t.age为总是小于1的数值时

select * from student t where 1>nvl(t.age,1);

–直接比较大小,暗含了 IS NOT NULL

select * from student t where t.age>1;

3:比较长度

–当t.age的长度总是大于1时

select * from student t where 2<=length(nvl(t.age,1));

–因为length函数的参数为空时,其结果为空,因而不能直接使用length函数

参考链接:

https://blog.csdn.net/qq_38880340/article/details/84290900

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/179178.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档