条件中存在or很容易不走索引可以改成union all的形式
select * from table WHERE a is null
UNION ALL
select * from table WHERE table.col = a
select * from table WHERE a is null
UNION ALL
select * from table WHERE table.col = a
调试欢乐多
explain plan for
select * from table where a is null
union all
select * from table where table.col=a--查看执行计划:
select * from table(dbms_xplan.display);
a是转进来的常量如果a不是null 其实就相当于 where table.col=a
如果a是null 条件就相当于 where (null is null or table.col=null)因为null is null 永远为真返回所有 这是你要的数据吗?可以写条件判断
if a is null then
where table.col is null ; --语句条件
else
where table.col=a ; --语句条件另外语句不一定走索引效率就高,要对比执行计划,如果你想让它走索引可以是用hint 提示 /*+ INDEX (tablename indexname) */
不如用IF来处理一下
至于用不用索引,要根据实际的CBO情况进行分析选择
没有必要一定要走索引,有时做索引并不是最快最优的方案
因为参数有可能传空 所以才用到这种格式,用union all要怎么写呢
因为参数有可能传空 所以才用到这种格式,用union all要怎么写呢不好意思 没仔细看 我试一下