表A有1000条数据,有字段name,表B有200多W条数据,也有字段name
现在查询出没在表B中出现的name有哪些
select ID,name from A where name not in(select distinct(name) from B)查询出100多条记录,要1分多钟,速度很慢
单独运行select ID,name from A 和
select distinct(name) from B都只用1秒,select distinct(name) from B 查出来的name大概有900个

解决方案 »

  1.   

    试下
    select ID,name from A where not exists(select 1 from B where name = a.name)
      

  2.   

    检查b.name字段是否有索引,如果没有,加上
      

  3.   

    用连接提示 看看 直接 改成  hash join
      

  4.   

    select ID,name from A where not exists(select 1 from B where name = a.name)
    这个效率差不多,B表中有索引,要不单独查询它不会就1秒钟
      

  5.   

    select a.id,a.name from a  left join b  on a.name=b.name where a.name is not null
      

  6.   

    试试改为内部链接
    select a.id,a.name from a inner join b on a.name=b.name 
      

  7.   

    终于OK了,改一下
    select a.id,a.name from a left join b on a.name=b.name where b.name is null
    这样查询出来的就对了,查询时间就用了1秒