我的mysql以前用
select * from table where id not in (67308,74207,23149,22457,23721,75697,35007)
发现索引没有作用  着了半天  原来是not in 使索引无效了 
如果用in就没有问题网上说用not exists
我不会语法  那位大哥说说啊我用的
select * from table as a not exists(select id from table as b where a.id=b.id and b.id in(67308,74207,23149,22457,23721,75697,35007))效率照样低 
教教我怎么写啊

解决方案 »

  1.   


    67308,74207,23149,22457,23721,75697,35007 存放临时表,在此字段上建立索引,比如ID,
    工作表在ID上建立索引
    再与工作表连接select * from table a left join lsb b on a.id=b.id where b.id is null
      

  2.   

    这种情况很难使用索引!
    比如你表中有10000条记录,却要选出其中的9999条,此时如果去用索引反而浪费时间。所以MYSQL的方法是正确的,即使如楼上使用临时表,效率应该也是一样。除非你的表中只有几条记录。比如10 条记录中排除7条,选出三条。
      

  3.   

    select * from table force index(yourkey) where id not in (67308,74207,23149,22457,23721,75697,35007);
      

  4.   

    我也遇到了同样的问题,换个高版本的MySql吧,5.0就没有这样的问题