两个存放有千万级数据的表
A(agent_id,name)和B(agent_id,xxx),A表的agent_id是主键,B表的agent_id是外键,关联到A表,
写条sql,从A表取出不存在于B表的数据求高效的sql写法

解决方案 »

  1.   

    通常有3种写法,not in, not exists 和 left join
    select * from a
    where agent_id not in (select agent_id from b);
    select * from a
    where not exists (select 1 from b where agent_id=a.agent_id);
    select a.* from a, b
    where a.agent_id = b.agent_id (+)
    and b.rowid is null;
    大部分情况下,left join效率高。
      

  2.   

    select * from a
    where not exists(select 1 from b
      where agent_id=a.agent_id)
      

  3.   


    select a.* from a, b
    where a.agent_id = b.agent_id (+)
    and b.rowid is null;和select * from a
    where not exists(select 1 from b
      where agent_id=a.agent_id) 
    那个高一些
      

  4.   

    Oracle查询优化(在可能的情况下,用or不用in,用exist不用count,用外连查询不用子查询,子 查询要进行全表扫描,对性能影响大等),外连查询,索引(对性能的影响),锁(表级锁和行级锁),存储过程,报表查询(其实也就是复杂业务的查询)
      

  5.   

    还以为not exists效率最高呢
      

  6.   

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

  7.   

    select a.* from a, b 
    where a.agent_id != b.agent_id  最简单了