两个表,A表有200万数据,B表有150万数据,A表‘dm’字段和B表'dm'字段关联,现在要查出在B表中‘dm’字段有的数据而A表中‘dm’字段没有的数据。假如:A表中 ‘dm’字段有三行数据‘1’,‘2’,‘3’,B表中有4行数据‘1’,‘2’,‘5’,‘6’.要查出结果‘5’,‘6’。怎么查询才能得到这样的结果。因为两张表数据都不小,怎么查询才能比较快速的得到结果集。求教高效sql

解决方案 »

  1.   


    select dm from B
    minus
    select dm from A;
      

  2.   

    -加个dm索引看看
    select * from tbb b where not exixts(select 1 from tba a where a.dm=b.dm)
      

  3.   

    避免不了全表扫描,索引也没必要建了
    select dm from b where not exists (select 1 from b where a.bm = b.bm);
      

  4.   


    还是得在a表的bm子段上建立个索引