一道面试题目

huangcaibing 2003-10-20 10:29:03
两个表A、B
结构相同,都有主键列ID,
每个表都有20万条以上得数据,如何快速获得在B表中存在而在A表不存在的数据?
...全文
87 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
isaacchang 2004-01-12
  • 打赏
  • 举报
回复
我觉得这个最快
select * from b
where id in
(select id from b minus select id from a);
sunluo 2004-01-12
  • 打赏
  • 举报
回复
为什么没有支持 robixiao(阿喜) ?
select b.*
from a,
b
where a.id(+) = b.id
and a.id is null
如果ID有索引的,数据库环境、系统环境都相同的话,这样是最快的!


sagac 2004-01-12
  • 打赏
  • 举报
回复
select id from b where not exists (select 1 from a where id=b.id);

抄也要抄个正确的。呵呵!
zzzstar 2004-01-12
  • 打赏
  • 举报
回复
看看这个吧!答案就在这里......

---------------------------------------------------------------
You Asked (Jump to Tom's latest followup)

Tom:

can you give me some example at which situation
IN is better than exist, and vice versa.


and we said...

Well, the two are processed very very differently.

Select * from T1 where x in ( select y from T2 )

is typically processed as:

select *
from t1, ( select distinct y from t2 ) t2
where t1.x = t2.y;

The subquery is evaluated, distinct'ed, indexed (or hashed or sorted) and then
joined to the original table -- typically.


As opposed to

select * from t1 where exists ( select null from t2 where y = x )

That is processed more like:


for x in ( select * from t1 )
loop
if ( exists ( select null from t2 where y = x.x )
then
OUTPUT THE RECORD
end if
end loop

It always results in a full scan of T1 whereas the first query can make use of
an index on T1(x).


So, when is where exists appropriate and in appropriate?

Lets say the result of the subquery
( select y from T2 )

is "huge" and takes a long time. But the table T1 is relatively small and
executing ( select null from t2 where y = x.x ) is very very fast (nice index on
t2(y)). Then the exists will be faster as the time to full scan T1 and do the
index probe into T2 could be less then the time to simply full scan T2 to build
the subquery we need to distinct on.


Lets say the result of the subquery is small -- then IN is typicaly more
appropriate.


If both the subquery and the outer table are huge -- either might work as well
as the other -- depends on the indexes and other factors.
S海鸥 2004-01-12
  • 打赏
  • 举报
回复
study!
chanet 2004-01-11
  • 打赏
  • 举报
回复
2.select * from b
minus
select * from a
diamondex 2003-10-25
  • 打赏
  • 举报
回复
mark
csyw 2003-10-25
  • 打赏
  • 举报
回复
up
yuxuan 2003-10-24
  • 打赏
  • 举报
回复
三种方法
1.select a.id from a,b where b.id=a.id(+) and a.id is null

2.select * from b
minus
select * from a

3.select id from b where not exists (select 1 from a where id=b.id);


oracs 2003-10-24
  • 打赏
  • 举报
回复
我觉得最简单的办法就是
select * from b
minus
select * from a
aceplus 2003-10-24
  • 打赏
  • 举报
回复
mark
yangqingdelphi 2003-10-22
  • 打赏
  • 举报
回复
oralce性能优化中讲应该是用exists比in快.
select id from b where not exists (select 1 from a where id=b.id);
to: youren537(youren537 你的测试时间不能作为依据.因为你第一次用的是exist然后再用in,其实数据都在缓存中了.in当然快了。你要看执行计划.
yzy 2003-10-22
  • 打赏
  • 举报
回复
我们一致裁定
select id from b where not exists (select 1 from a where id=b.id);
是标准答案!
大家鼓掌~~~~啪.啪.啪.啪.啪.啪.啪.啪.
huangcaibing 2003-10-22
  • 打赏
  • 举报
回复
我也是一头雾水,我测试一下再揭贴啊
北极星2013 2003-10-21
  • 打赏
  • 举报
回复
pengdali(大力 V3.0) 的办法不错,应该是最快的啦。
suleen 2003-10-21
  • 打赏
  • 举报
回复
select id from a 是全表扫描,应该是用not exists快.
youren537 2003-10-21
  • 打赏
  • 举报
回复
写倒了,应该是
select id from b
where not exists (select a.id from a where a.id = b.id);

两表各超过35万条记录,执行时间9.133seconds 出结果。


select id from b where not in (select id from a);

两表各超过35万条记录,执行时间
Executed in 2.514 seconds 出结果。
robixiao 2003-10-21
  • 打赏
  • 举报
回复
select b.*
from a,
b
where a.id(+) = b.id
and a.id is null
youren537 2003-10-21
  • 打赏
  • 举报
回复
select id from a
where not exists (select b.id from b where a.id = b.id);
wangzi163 2003-10-21
  • 打赏
  • 举报
回复
select * from a
where not exists (select b.id from b where a.id = b.id);
加载更多回复(6)
昨日,11.19,最新整理了,第61-80题,现在公布上传。 另加上之前公布的第1-60 题,在此做一次汇总上传,以飨各位。 可以这么说,绝大部分的面试题,都是这100 道题系列的翻版, 此微软等公司数据结构+算法面试100 题系列,是极具代表性的经典面试题。 而,对你更重要的是,我自个还提供了答案下载,提供思路,呵。 所以,这份资料+答案,在网上是独一无二的。 ------------------------------------ 整理资源,下载地址: 答案系列: 1.[最新答案V0.3 版]微软等数据结构+算法面试100 题[第21-40 题答案] http://download.csdn.net/source/2832862 2.[答案V0.2 版]精选微软数据结构+算法面试100 题[前20 题]--修正 http://download.csdn.net/source/2813890 //此份答案是针对最初的V0.1 版本,进行的校正与修正。 3.[答案V0.1 版]精选微软数据结构+算法面试100 题[前25 题] http://download.csdn.net/source/2796735 题系列: 4.[第一部分]精选微软等公司数据结构+算法经典面试100 题[1-40 题] http://download.csdn.net/source/2778852 5.[第1 题-60 题汇总]微软等数据结构+算法面试100 题 http://download.csdn.net/source/2826690 更多资源,下载地址: http://v_july_v.download.csdn.net/ 若你对以上任何题或任何答案,有任何问题,欢迎联系我: My E-mail: zhoulei0907@yahoo.cn ------------- 作者声明: 本人July 对以上公布的所有任何题或资源享有版权。转载以上公布的任何一题, 或上传百度文库资源,请注明出处,及作者我本人。 向你的厚道致敬。谢谢。 ---July、2010 年11 月20 日。 ------------------------------------------------------ 各位,若对以上100题任何一道,或对已上传的任何一题的答案, 有任何问题,请把你的思路、想法,回复到此帖子上, 微软等100题系列,永久维护地址(2010年11.26日): http://topic.csdn.net/u/20101126/10/b4f12a00-6280-492f-b785-cb6835a63dc9.html
精选微软等数据结构+算法面试100题答案修正V0.2版本 -------------------- 此份答案是针对,前期已公布的最初的那份答案的,初步校正与修正。 http://download.csdn.net/source/2796735(V0.1版) 相比第一份V0.1版答案,此份答案V0.2版更加准确,亦修正了不少题的答案。 此份20题的答案,思路更加清晰易懂,简介明了。 请享用。July、2010/11/06。 其它资源,下载地址: 1.[最新答案V0.3版]微软等数据结构+算法面试100题[第21-40题答案] http://download.csdn.net/source/2832862 2.[第1题-60题汇总]微软等数据结构+算法面试100题 http://download.csdn.net/source/2826690 3.[答案V0.2版]精选微软数据结构+算法面试100题[前20题]--修正 http://download.csdn.net/source/2813890 //此份答案是针对最初的V0.1版本,进行的校正与修正。 4.[答案V0.1版]精选微软数据结构+算法面试100题[前25题] http://download.csdn.net/source/2796735 5.[第二部分]精选微软等公司结构+算法面试100题[前41-60题]: http://download.csdn.net/source/2811703 6.[第一部分]精选微软等公司数据结构+算法经典面试100题[1-40题] http://download.csdn.net/source/2778852 更多资源,下载地址: http://v_july_v.download.csdn.net/ ------------------------------------------------------ 各位,若对以上100题任何一道,或对已上传的任何一题的答案, 有任何问题,请把你的思路、想法,回复到此帖子上, 微软等100题系列,永久维护地址(2010年11.26日): http://topic.csdn.net/u/20101126/10/b4f12a00-6280-492f-b785-cb6835a63dc9.html
火爆出炉:微软等数据结构+算法面试100题首次完整亮相 ---100题V0.1版最终完成 作者:July 2010年12月6日 微软等100题系列V0.1版终于结束了。 从2010年10月11日当天最初发表前40题以来,直至此刻,整理这100题,已有近2个月。 2个月,因为要整理这100题,很多很多其它的事都被我强迫性的搁置一旁, 如今,要好好专心去做因这100题而被耽误的、其它的事了。 这微软等数据结构+算法面试100题系列(题+答案),到底现在、或此刻、或未来, 对初学者有多大的意义,在此,我就不给予评说了。 由他们自己来认定。所谓,公道自在人心,我相信这句话。 任何人,对以下任何资料、题、或答案,有任何问题,欢迎联系我。 作者邮箱: zhoulei0907@yahoo.cn 作者声明: 转载或引用以下任何资料、或题,请注明作者本人July及出处。 向您的厚道致敬,谢谢。 好了,请享受这完完整整的100题吧,这可是首次完整亮相哦。:D。 ............ ............ 答案系列: 5.[最新答案V0.3版]微软等数据结构+算法面试100题[第21-40题答案] http://download.csdn.net/source/2832862 6.[答案V0.2版]精选微软数据结构+算法面试100题[前20题]--修正 http://download.csdn.net/source/2813890 //此份答案是针对最初的V0.1版本,进行的校正与修正。 7.[答案V0.1版]精选微软数据结构+算法面试100题[前25题] http://download.csdn.net/source/2796735 剩下的第41-100题答案,正在整理中。预计明年整理公布。 请各位,细心的等待。谢谢。 更多资源,下载地址: http://v_july_v.download.csdn.net/ ================================== 更多详情,请参见本人博客: My Blog: http://blog.csdn.net/v_JULY_v --------------------------------------------------------------------------------------- 各位,若对以上100题任何一道,或对已上传的任何一题的答案, 有任何问题,请把你的思路、想法,回复到此帖子上, 微软等100题系列,永久维护地址(2010年11.26日): http://topic.csdn.net/u/20101126/10/b4f12a00-6280-492f-b785-cb6835a63dc9.html

3,491

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 高级技术相关讨论专区
社区管理员
  • 高级技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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