表A  表B  表C
从A表中查出与B表或C表兄的ID
select id from A where id in(select id from B)  or  id in(select id from C);不知道对不对,执行效率好低  怎么写才能高效

解决方案 »

  1.   

    select id from a where exists(select 1 from b where a.id=b.id) or exists(select 1 from c where a.id=c.id)
    需要3个id都建索引,可能会大幅提高你的速度
      

  2.   


    up, exists的效率要比in 高。 Oracle SQL的优化
    http://blog.csdn.net/tianlesoftware/archive/2009/10/15/4672023.aspx
    ------------------------------------------------------------------------------ 
    Blog: http://blog.csdn.net/tianlesoftware 
    网上资源: http://tianlesoftware.download.csdn.net 
    相关视频:http://blog.csdn.net/tianlesoftware/archive/2009/11/27/4886500.aspx 
    Q Q 群:62697716 
      

  3.   

    用union呢,select a1.id from A a1, B b1 where a1.id=b1.id
    union
    select a2.id from A a2, C c1 where a2.id=c1.id按2楼说的,用union比 or效率高,当然你的前提是 id是索引字段
      

  4.   

    把union exists都试试,试了就知道哪个效率高了。
      

  5.   

    exists的效率要比in 高 这可不一定哦   要看2个表的数据量的...
      

  6.   

    select id from A,B,C where A.id=B.id(+) and A.id=C.id(+) and B.id is not null and C.id is not null
      

  7.   


    1.
      id 加索引
    2.
      别用in 效率最低
    3。
    select id from a where exists(select 1 from b where a.id=b.id) or exists(select 1 from c where a.id=c.id) select id from A,B,C where A.id=B.id(+) and A.id=C.id(+) and B.id is not null and C.id is not null 
    然后再看看执行计划。哪个好
      

  8.   

    尝试下:select id from A where id = any ( select id from B union all select id from c)数据量大的话,要提高效率,还是要建立索引的,至于建立那种索引的话,要根据实际情况而定.而且不知道您数据库的版本是什么,还有数据库的优化器是RBO 还是CBO.效率是由很多参数而决定的.
    当然有时候不用索引会比用索引快,这个自己慢慢学习掌握吧
      

  9.   

    select * from A where exists(select 1 from b where a.id=b.id) union select * from A where exists(select 1 from c where a.id=c.id);试试
      

  10.   

    select a.id from a,b,c where a.id=b.id(+) and a.id=c.id(+) and b.id is not null and c.id is not null
      

  11.   

       你这样不能够从根本上解决问题  看看这个吧  理解思想 http://download.csdn.net/source/1820881