not in 优化方法?

lula 2008-06-11 07:37:38
select * from emp where emp_no not in (select emp_no from emp_bill)

要求用两种SQL写法优化上面SQL,谢谢
...全文
2072 21 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
anerou 2010-01-13
  • 打赏
  • 举报
回复
收藏先
luyanvv 2009-10-09
  • 打赏
  • 举报
回复
今天天气不错,适合出来逛CSDN,~~~~~路过
changweishao 2008-06-14
  • 打赏
  • 举报
回复
解决方案挺多的,人齐,可以结贴
yugas 2008-06-14
  • 打赏
  • 举报
回复
执行一下疾患看看结果是否是一样...我感觉最主要是看计划...
DragonBill 2008-06-14
  • 打赏
  • 举报
回复
4楼的写得不错,大致补充几点:

1,

select a.* 
from emp a ,emp_bill b
where a.emp_no=b.emp_no(+)
and b.emp_no is null

改为
select a.* 
from emp a ,emp_bill b
where a.emp_no=b.emp_no(+)
and NVL(b.emp_no, '1') = '1'

要好一些


2, 如果DB是10g, 可考虑用LNNVL function, 如下:

SELECT A.* FROM EMP A, EMP_BILL B WHERE LNNVL(A.EMP_NO = B.EMP_NO);
sulins 2008-06-14
  • 打赏
  • 举报
回复
4楼正解。
samuellei 2008-06-13
  • 打赏
  • 举报
回复
1.
select a.*
from emp a, emp_no b
where a.emp_no != b.emp_no

2.
select *
from emp
where emp.emp_no in (select emp_no from emp minus select emp_no from emp_bll)
qixiao 2008-06-13
  • 打赏
  • 举报
回复
表连接效率最好,Not Exists其次,Not in最低
dencyzhang 2008-06-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 hebo2005 的回复:]
方法一 还有点错误



SQL codeselect *
from emp a
where not exists (select 1
from emp_bill b
where b.emp_no=a.emp_no)




方法2

SQL codeselect a.*
from emp a ,emp_bill b
where a.emp_no=b.emp_no(+)
and b.emp_no is null
[/Quote]

支持以上2种方法
huoxudong125 2008-06-13
  • 打赏
  • 举报
回复
这种情况一般用not exits处理。

snowy_howe 2008-06-12
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 LJY_AINILU 的回复:]
引用 5 楼 qiyousyc 的回复:
这种情况一般用exits处理。
select * from emp a where
not exists (select 1from emp_bill b where b.emp.no=a.emp_no)



select 1 有什么用啊 是什么意思
[/Quote]

select 1 是一种用法,也可以select 某字段
或者select 'x'都行的,也就是说exists其实
是判断是否有数据返回,而不关心返回的数据。
LJY_AINILU 2008-06-12
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 qiyousyc 的回复:]
这种情况一般用exits处理。
select * from emp a where
not exists (select 1from emp_bill b where b.emp.no=a.emp_no)


[/Quote]
select 1 有什么用啊 是什么意思
hhfwyl 2008-06-12
  • 打赏
  • 举报
回复
欢迎加入oracle qq群9701750
chensi05 2008-06-12
  • 打赏
  • 举报
回复
SELECT *
FROM EMP A
WHERE NOT EXISTS (SELECT 1 FROM EMP_BILL B WHERE B.EMP_NO = A.EMP_NO)
CathySun118 2008-06-12
  • 打赏
  • 举报
回复
select a.* from emp a,emp_bill b where a.emp_no <>b.emp_no
select * from emp a where emp_no not exists (select emp_no b from emp_bill where a.emp_no <>b.emp_no)
hebo2005 2008-06-11
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 arsenal1999 的回复:]
select *
from emp
where emp_no in(select emp_no from emp minus select emp_no from emp_bill );

select a.*
from emp a,(select emp_no from emp minus select emp_no from emp_bill ) b
where a.emp_no = b.emp_no;

可以么?


[/Quote]
你这效率不行的,要做三次访问表,去看下执行计划就知道了
qiyousyc 2008-06-11
  • 打赏
  • 举报
回复
这种情况一般用exits处理。
select * from emp a where
not exists (select 1from emp_bill b where b.emp.no=a.emp_no)

hebo2005 2008-06-11
  • 打赏
  • 举报
回复
方法一 还有点错误


select * 
from emp a
where not exists (select 1
from emp_bill b
where b.emp_no=a.emp_no)




方法2
select a.* 
from emp a ,emp_bill b
where a.emp_no=b.emp_no(+)
and b.emp_no is null
hebo2005 2008-06-11
  • 打赏
  • 举报
回复
方法一

select *
from emp a
where not exists (select 1
from emp_bill b
where b.emp.no=a.emp_no)
hebo2005 2008-06-11
  • 打赏
  • 举报
回复
select * 
from emp a
where no exists (select 1
from emp_bill b
where b.emp.no=a.emp_no)
加载更多回复(1)

17,382

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧