ITPUB论坛 » Oracle开发 » sql语句请教
新一届的微软MVP评选已经开始,欢迎各位推荐!
2008-7-4 15:38 chenk818
sql语句请教

A,B两个表,通过NO字段关联,找出A中有B中没有的NO,A是主表,数据较小,B是从表,数据量特别大,用not in的话比较慢,有没有更好的方式解决?

2008-7-4 15:51 jack198409
select no from a
minus
select no from b

2008-7-4 16:03 zhangfengh
也可以用外连接

2008-7-4 16:07 joyw
[quote]原帖由 [i]jack198409[/i] 于 2008-7-4 15:51 发表 [url=http://www.itpub.net/redirect.php?goto=findpost&pid=10856376&ptid=1016800][img]http://www.itpub.net/images/common/back.gif[/img][/url]
select no from a
minus
select no from b [/quote]

:rose:


也可以用:

select * from a where no exists (select 1 from b where a.no=b.no);

2008-7-4 16:20 jvkojvko
据说用外联接不错的,
where 。。is null 就可以了

2008-7-4 17:44 gthboy
[quote]原帖由 [i]joyw[/i] 于 2008-7-4 16:07 发表 [url=http://www.itpub.net/redirect.php?goto=findpost&pid=10856617&ptid=1016800][img]http://www.itpub.net/images/common/back.gif[/img][/url]


:rose:


也可以用:

select * from a where [color=red]no[/color] exists (select 1 from b where a.no=b.no); [/quote]

这里应该是not吧?

2008-7-4 17:46 gthboy
[quote]原帖由 [i]jvkojvko[/i] 于 2008-7-4 16:20 发表 [url=http://www.itpub.net/redirect.php?goto=findpost&pid=10856814&ptid=1016800][img]http://www.itpub.net/images/common/back.gif[/img][/url]
据说用外联接不错的,
where 。。is null 就可以了 [/quote]

是不是这样的?

select a.* from a,b where a.no=b.no(+) and b.no is null;

2008-7-4 17:47 阿日
[quote]原帖由 [i]jvkojvko[/i] 于 2008-7-4 16:20 发表 [url=http://www.itpub.net/redirect.php?goto=findpost&pid=10856814&ptid=1016800][img]http://www.itpub.net/images/common/back.gif[/img][/url]
据说用外联接不错的,
where 。。is null 就可以了 [/quote]
小马哥,where ... is null怎么用呀
我一直都用not exists这种方式

2008-7-4 23:29 ccc0120
not exists用惯了,where   is null 不太习惯

2008-7-5 07:51 jvkojvko
[quote]原帖由 [i]gthboy[/i] 于 2008-7-4 17:46 发表 [url=http://www.itpub.net/redirect.php?goto=findpost&pid=10858038&ptid=1016800][img]http://www.itpub.net/images/common/back.gif[/img][/url]


是不是这样的?

select a.* from a,b where a.no=b.no(+) and b.no is null; [/quote]

好像就是这样,我也此上次偶然看到的e

2008-7-5 08:00 zhangweicai74
MINUS就行了

2008-7-5 08:29 sunfly1983
由于a表的数据不是太大用not in也可以吧
如:
select no from a not in (select distinct(no) from b);

2008-7-5 09:32 gthboy
[quote]原帖由 [i]sunfly1983[/i] 于 2008-7-5 08:29 发表 [url=http://www.itpub.net/redirect.php?goto=findpost&pid=10862239&ptid=1016800][img]http://www.itpub.net/images/common/back.gif[/img][/url]
由于a表的数据不是太大用not in也可以吧
如:
select no from a not in (select distinct(no) from b); [/quote]

晕!这个应该是效率很差的了。

高手来说一下,minus、not exists、外连接三种方式,哪种效率最高?

2008-7-5 09:49 sunfly1983
刚才做了个测试上面有查询的用的时间。
我用distinct(id)查50万行很快!时间在下面显示!
如果这个查询很快的话,根据楼主的条件应该是很快的!
看看我的测试结果是否有什么问题!
SQL> ed
已写入 file afiedt.buf

  1  begin
  2  for i in 1..100000 loop
  3  insert into test1 values(10);
  4  end loop;
  5* end;
SQL> /

PL/SQL 过程已成功完成。

SQL> ed
已写入 file afiedt.buf

  1  begin
  2  for i in 1..100000 loop
  3  insert into test1 values(20);
  4  end loop;
  5* end;
SQL> /

PL/SQL 过程已成功完成。

SQL> ed
已写入 file afiedt.buf

  1  begin
  2  for i in 1..100000 loop
  3  insert into test1 values(30);
  4  end loop;
  5* end;
SQL> /

PL/SQL 过程已成功完成。

SQL> ed
已写入 file afiedt.buf

  1  begin
  2  for i in 1..100000 loop
  3  insert into test1 values(40);
  4  end loop;
  5* end;
SQL> /

PL/SQL 过程已成功完成。

SQL> ed
已写入 file afiedt.buf

  1  begin
  2  for i in 1..100000 loop
  3  insert into test1 values(50);
  4  end loop;
  5* end;
SQL> /

PL/SQL 过程已成功完成。

SQL> set timing on
SQL> select dictinct(id) from test1;
select dictinct(id) from test1
       *
第 1 行出现错误:
ORA-00904: "DICTINCT": 标识符无效


已用时间:  00: 00: 00.01
SQL> select distinct(id) from test1;

        ID
----------
        30
        20
        40
        50
        10

已用时间:  00: 00: 00.11
SQL> select count(id) from test1;

COUNT(ID)
----------
    500000

已用时间:  00: 00: 00.04

2008-7-5 13:03 ispu
如果B表真是特别巨大的话就不要用minus了,它是把两个结果集来比较的
小的话就用minus最简洁

2008-7-5 13:43 sunfly1983
[quote]原帖由 [i]gthboy[/i] 于 2008-7-4 17:46 发表 [url=http://www.itpub.net/redirect.php?goto=findpost&pid=10858038&ptid=1016800][img]http://www.itpub.net/images/common/back.gif[/img][/url]


是不是这样的?

select a.* from a,b where a.no=b.no(+) and b.no is null; [/quote]

测试过了,这个最快!0.09s
not in那个0.44s
not exists 0.14s:)

2008-7-5 20:34 jvkojvko
[quote]原帖由 [i]sunfly1983[/i] 于 2008-7-5 13:43 发表 [url=http://www.itpub.net/redirect.php?goto=findpost&pid=10864736&ptid=1016800][img]http://www.itpub.net/images/common/back.gif[/img][/url]


测试过了,这个最快!0.09s
not in那个0.44s
not exists 0.14s:) [/quote]

就说了,我上次看到过优化的例子有这样说的,但是去找找不到了,本来想上传上来看看的,

2008-7-5 20:37 sunfly1983
小马哥,高人啊!

2008-7-5 20:43 jvkojvko
好像原理就在于扫描少一次

2008-7-9 09:24 chenk818
太强悍了,学习....



[[i] 本帖最后由 chenk818 于 2008-7-9 09:31 编辑 [/i]]

页: [1] 2
查看完整版本: sql语句请教


Powered by ITPUB论坛