我现在有两张表  
表A  一个字段   aa
表B 两个字段   bb  cc
我要删除表A中所有  aa=bb 的数据
其中表A,B中都有几w条数据,我用一下的语句,执行起来特别的慢.delete from A where exists ( select aa from A ,B where aa=bb )

解决方案 »

  1.   

    delete from A where A.aa in (select aa from A,B where aa=bb) 
    用in好像好一点儿。
      

  2.   

    exists 比in的速度要快啊
      

  3.   

    表B 哪个是外键啊?delete from A where exists ( select aa from A ,B where aa=bb ) 
    这样对?
    子查询结果不为空,删除表A所有数据
      

  4.   

    Delete From A
        From A Inner Join B On A.aa=B.bb
      

  5.   

    表B 哪个是外键啊?delete from A where exists ( select aa from A ,B where aa=bb ) 
    这样对?
    子查询结果不为空,删除表A所有数据B 和 A 不存在主外键关系,是人为让他们关联的
    上面的语句是没有错的,可是执行起来特别的慢,因为两个表都有十几万条数据,而实际删除的数据只有几十条, 我改用in试了一下,速度快多了.谢谢各位.
      

  6.   

    我比较了一下这几条语句的执行效率, 结果如下: delete from A where A.aa in (select aa from A,B where aa=bb)  删除19条数据    0:00:03Delete From A
        From A Inner Join B On A.aa=B.bb删除19条数据    0:00:17delete from A where exists ( select aa from A ,B where aa=bb )删除19条数据    0:02:47
      

  7.   

    你有没有这样试过
    delete from A where exists(select * from B where A.aa=B.bb)
      

  8.   

    delete from A where exists(select * from B where A.aa=B.bb)
    我试过了,也比较慢的和
    delete from A where exists ( select aa from A ,B where aa=bb )
    差不多
      

  9.   

    没有其它办法 用exists速度快点。考虑服务器的性能,可做循环分布删除,一次删除500条
      

  10.   

    Delete From A
        From A Inner Join B On A.aa=B.bb数据量大的话应该上边的这个最快
      

  11.   

    1.第一种可能的语句:
    delete from A from A inner join B on A.aa=B.bb2.第二种可能的语句://这种肯定是最慢的
    delete from A where exists ( select 1 from A ,B where aa=bb )3.第三种可能的语句:
    delete from A where exists ( select 1 from B where aa=bb )------------------------------------------------
    具体的速度,会与bb或者aa上是否有索引相关联。