a表有数据15万,b表有数据30万a表和b表通过id关联,现在要找到a表中的id在b表中不存在的。如何写sql为高效?试过not in,not exists left join 均未果。请大侠给个办法,谢谢!

解决方案 »

  1.   

    在A、B表ID字段上建立索引
    select a.* from a left join b on a.id=b.id where b.id is null
      

  2.   

    这个sql我写过的,不行的,速度依然很慢。
      

  3.   

    SHOW CREATE TABLE TBNAME
    看看
      

  4.   

    a,b 表上ID是否有索引?show index from a;
    show index from b;explain select ....
    贴出来看一下。
      

  5.   

    索引是有的explain之后的结果为1;SIMPLE;a;ALL;NULL;NULL;NULL;NULL;155801;
    1;SIMPLE;b;index;NULL;idx_a;63;NULL;283545;Using index;显示a表是全表扫,b用上了索引,a表数据为155801,b表为283545请问这是否是最佳的办法了?
      

  6.   

    select a.* from a left join b on a.id=b.id where b.id is null这种需求至少要存在对A表的全表扫描,无法避免。