Oracle: select 优化问题

请看这两句:
select * from table1 where id=:xx;
select * from table1 where id=:xx or :xx is null;
id是索引列,因此第一句极快;然而第二句非常慢,由于实际情况where中还有很多复杂的条件,我不想改写成:
select * from table1 where id=:xx
union all
select * from table1 where :xx is null;

有没有办法解决?

另:hint 如何使用?
[309 byte] By [MountLion-闷头睡] at [2008-4-29]
# 1
select * from table1 where nvl(id,:xx)=:xx
xinpingf-白开心 at 2007-10-22 > top of Msdn China Tech,其他数据库开发,SQL Anywhere Studio...
# 2
看出毛病来了,
select * from table1 where :xx is null;
这句sql怎么解释?
xinpingf-白开心 at 2007-10-22 > top of Msdn China Tech,其他数据库开发,SQL Anywhere Studio...
# 3
我猜应该是,
select * from table1 where id is null
xinpingf-白开心 at 2007-10-22 > top of Msdn China Tech,其他数据库开发,SQL Anywhere Studio...
# 4
:xx is null是怎么回事?
sky_blue-蓝天2007 at 2007-10-22 > top of Msdn China Tech,其他数据库开发,SQL Anywhere Studio...
# 5
不明白
# 6
:xx是外部变量
在form builder中使用外部变量
这么说吧:
在用户界面输入部门,如果部门为空则不考虑部门:
select sum(salary) from salary_info where dept=:text_dept or :text_dept is null;

应该比较明白了吧。
MountLion-闷头睡 at 2007-10-22 > top of Msdn China Tech,其他数据库开发,SQL Anywhere Studio...
# 7
白开心:
我是不会在id列使用函数的,那样更慢。
MountLion-闷头睡 at 2007-10-22 > top of Msdn China Tech,其他数据库开发,SQL Anywhere Studio...
# 8
用if比较合适
goodlucky-沉浮 at 2007-10-22 > top of Msdn China Tech,其他数据库开发,SQL Anywhere Studio...
# 9
用if还不如用union呢,
主要是真正的sql有些庞大,写两遍的话……怕修改的时候有遗漏,也不好看
MountLion-闷头睡 at 2007-10-22 > top of Msdn China Tech,其他数据库开发,SQL Anywhere Studio...
# 10
你看看 union 行吗?
goodlucky-沉浮 at 2007-10-22 > top of Msdn China Tech,其他数据库开发,SQL Anywhere Studio...
# 11
xx is null时
where :xx is null要检索所有行,改下你的逻辑。
goodlucky-沉浮 at 2007-10-22 > top of Msdn China Tech,其他数据库开发,SQL Anywhere Studio...
# 12
逻辑没有错,就是要检索所有行, when :xx is null
MountLion-闷头睡 at 2007-10-22 > top of Msdn China Tech,其他数据库开发,SQL Anywhere Studio...
# 13
在用户界面输入部门,如果部门为空则不考虑部门:

我猜你的意思是不是这样的:
IF :text_dept IS NULL THEN
select sum(salary) from salary_info;
ELSE
select sum(salary) from salary_info where dept=:text_dept;
END IF
MicroMouse-独步江湖 at 2007-10-22 > top of Msdn China Tech,其他数据库开发,SQL Anywhere Studio...
# 14
同意MicroMouse的说法。
按照我看到的一种说法,说对NULL是不会使用索引的,所以效率低。
pengz-米格 at 2007-10-22 > top of Msdn China Tech,其他数据库开发,SQL Anywhere Studio...
# 15
同意:MicroMouse(独步江湖)的做法。
使用union all还不如原来的写法呢。