当前位置:网站首页>sql is not null 优化(oracle语句索引优化)
sql is not null 优化(oracle语句索引优化)
2022-08-01 12:46:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
IS NULL的优化
优化方法: 通过nvl(字段i,j),将字段i中为空的数据转化为j,从而正常使用索引. 具体则是将条件 i is null 转化为 j = nvl(i,j); 数据量较大时转化is null 在所用oracle版本提升明显, 注意:使用时必须确保字段i的数据不包含j! 缺陷:字段i不能有值为j的数据 另外一种方式是将null包含到索引中
函数介绍: nvl(a,b,c,…) 当a为空时取b,当b为空取c,以此类推.
优化示例
--使用nvl函数的方式(不用添加索引,推荐)
select*from tab_i t where 1=nvl(t.col_x,1);
--当t.col_x不存在等于1的数据时等价于
--select*from tab_i t where t.col_x is null;
--添加索引的方式
create index idx_col_x on tab_i(decode(col_x,null,1));
select*from tab_i t where decode(t.col_x,null,1)=1;
IS NOT NULL的优化
优化方法 结果集不包含 j = nvl(i,j)即可,方式多样. 通常情况下可以使用not exists或者比较大小, 这两种效率一般高于比较长度
优化示例
- not exists
select*from tab_i t where not exists
(select 1 form tab_i i where 1=nvl(i.col_x,1));
--11g版本后not in和not exists趋于相似,也可以用not in
--当t.col_x不存在等于1的数据时等价于
--select*from tab_i t where t.col_x is not null;
- 比较大小
--当t.col_x为总是大于1的数值时
select*from tab_i t where 1<nvl(t.col_x,1);
--当t.col_x为总是小于1的数值时
select*from tab_i t where 1>nvl(t.col_x,1);
--直接比较大小,暗含了 IS NOT NULL
select*from tab_i t where t.col_x>1;
- 比较长度
--当t.col_x的长度总是大于1时
select*from tab_i t where 2<=length(nvl(t.col_x,1));
--因为length函数的参数为空时,其结果为空,因而不能直接使用length函数
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/126517.html原文链接:https://javaforall.cn
边栏推荐
- SQL function SQRT
- 通讯录(静态版)(C语言)(VS)
- HMS Core音频编辑服务音源分离与空间音频渲染,助力快速进入3D音频的世界
- 如何使用OpenCV测量图像中物体之间的距离
- Envoy source code flow chart
- 人像分割技术解析与应用
- CloudCompare&PCL ICP配准(点到面)
- 如何使用 Authing 单点登录,集成 Discourse 论坛?
- shell 中的 分发系统 expect脚本 (传递参数、自动同步文件、指定host和要传输的文件、(构建文件分发系统)(命令批量执行))
- 8. How does the SAP ABAP OData service support the Create operation
猜你喜欢
随机推荐
大中型网站列表页翻页过多怎么优化?
Dameng replaces the officially authorized dm.key
uniapp读取和写入文件
R language fitting ARIMA model: use the auto.arima function in the forecast package to automatically search for the best parameter combination, model order (p, d, q), set the seasonal parameter to spe
Programmer's self-cultivation
markdown常用数学符号cov(markdown求和符号)
Feign 从注册到调用原理分析
易周金融分析 | 银行ATM机智能化改造提速;互联网贷款新规带来挑战
.NET性能优化-使用SourceGenerator-Logger记录日志
让程序员早点下班的效率工具
Data frame and remote frame of CAN communication
小程序插件如何帮助开发者受益?
10年稳定性保障经验总结,故障复盘要回答哪三大关键问题?|TakinTalks大咖分享
MySQL调优
【讲座分享】“营收“看金融
脚本语言Lua的基础知识总结
CAN通信的数据帧和远程帧
HMS Core音频编辑服务音源分离与空间音频渲染,助力快速进入3D音频的世界
SQL函数 SQUARE
关于亚马逊测评,你了解多少?