日期:2014-05-17  浏览次数:20678 次

求一个巨简单的查询(但对速度有严格要求)求SQL急速圣手指点!
EBAY的数据库,有300多万条记录,将问题简化如下
两个表TableA,TableB,均有ID字段,互不包含,但含有ID相同的记录。

现在想把TableA中不在A、B交集中的部分找出来。
即A-A∩B   的部分。
即select   *   from   A   where   A.id   not   in   (select   A.id   from   A,B   where   A.id=B.id)

此操作为经常性操作,要求速度一定要能忍受。
求SQL急速圣手指点!


------解决方案--------------------
我觉得这个问题如果需要很好的速度,应该在日常做好功课,而不能等到需要查询时才做。
比如,在表A中加一个字段,表示这个id在表B中的存在标记,在日常表B更新时,同时更新表A中的那个标记(可用触发器),当你需要的时候只需查表A中没有标记过的记录即可。
------解决方案--------------------
select * from A
minus
(select A.id from A,B where A.id=B.id)
会快许多

------解决方案--------------------
顶下楼上
select * from A
minus
(select A.id from A,B where A.id=B.id)
------解决方案--------------------
b.id建索引,用下面这条语句效率最高
select a.id from A, b where a.id=b.id(+) and b.id is not null