条件中存在or很容易不走索引可以改成union all的形式
select * from table WHERE a is null
UNION ALL
select * from table WHERE table.col = a

解决方案 »

  1.   

    楼上正解,你可以使用执行计划看看到底走没走索引:
    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);
      

  2.   

    where(a is null or  table.col = a)  分析你的条件:
    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) */
      

  3.   

    a是变量,又是在存储过程里面
    不如用IF来处理一下
    至于用不用索引,要根据实际的CBO情况进行分析选择
    没有必要一定要走索引,有时做索引并不是最快最优的方案
      

  4.   

    加HINT能对语句进行充分的分析,不建议把HINT放到生产环境中
      

  5.   


    因为参数有可能传空 所以才用到这种格式,用union all要怎么写呢
      

  6.   


    因为参数有可能传空 所以才用到这种格式,用union all要怎么写呢不好意思 没仔细看 我试一下