上次问过如何提高ACCESS中EXISTS的查询速度问题,最后是老鸟帮助解决,把EXISTS换成了INNER JOIN后,速度大幅度提升。
那我现在遇到了另外一个问题,就是ACCESS查询语句中出现NOT EXISTS时,并且数据量上到5万条时,速度就很慢,跟死机一样。谁能帮忙将NOT EXISTS换个效率高的SQL方法。如下所示:
update 电阻器 set flg='红色' where not exists (select resistor1.* from [D:\company_lib\PartTableDB.mdb;pwd=123].resistor1 where 电阻器.Part_Number=resistor1.Part_Number)功能是查找用户数据库中“电阻器”表与公司数据库“resistor1”表之间的不同记录。用两表的Part_Number字段进行匹配。

解决方案 »

  1.   

    我试图像下面这样改,但是update语句有错误:
    sql = "select distinct resistor1.Part_Number into b from [D:\company_lib\PartTableDB.mdb;pwd=123].resistor1"
    g_DB.Execute sql
    sql = "update 电阻器 a set flg='红色'  where a.Part_Number<> b.Part_Number"
    g_DB.Execute sql
      

  2.   

    楼主你好像操作的表分布在不同的数据库mdb文件中是吗,这可能会比较慢哦
      

  3.   


    你说的没错。但是我测试过,把数据放到同一个数据库的不同表中,结果是一样的,结论就是速度与是否跨库查询无关,只和NOT EXISTS有关。如何改用其他方法?
      

  4.   

    有没有方法可以绕过NOT EXISTS呢?
      

  5.   

    update 电阻器 a left join [D:\company_lib\PartTableDB.mdb;pwd=123].resistor1 b on a.Part_Number=b.Part_Number
    set a.flg='红色' 
    where b.Part_Number is null
      

  6.   


    谢谢你的回答。那如果比较的是两个字段,如下所示,可以吗?
    update 电阻器 a LEFT JOIN [D:\company_lib\PartTableDB.mdb;pwd=123].resistor1 b ON a.Part_Number=b.Part_Number and a.Material_code_13= b.Material_code_13 set a.flg='红色' where b.Part_Number is null  and b.Material_code_13 is null 
      

  7.   

    数据库:ACCESS
    有如下查询:
    update 电阻器 a LEFT JOIN [D:\company_lib\PartTableDB.mdb;pwd=123].resistor1 b ON a.Part_Number=b.Part_Number and a.Material_code_13= b.Material_code_13 set a.flg='红色' where b.Part_Number is null and b.Material_code_13 is null  目的是查找用户数据库中“电阻器”表与公司数据库“resistor1”表之间的不同记录。用两表的Part_Number字段和Material_code_13字段进行匹配,如果两个字段都不同,则认为是缺少元器件,并标记为“红色”。
    为什么我上面的查询b.Part_Number is null and b.Material_code_13 is null 中的and不起作用,把其中一个匹配字段不同,另一个匹配字段相同的记录也给查出来了?
    为什么我上面的查询b.Part_Number is null and b.Material_code_13 is null 中的and不起作用,把其中一个匹配字段不同,另一个匹配字段相同的记录也给查出来了?
    也就是使用:
    b.Part_Number is null and b.Material_code_13 is null  

    b.Part_Number is null or b.Material_code_13 is null
    查询出的结果竟然相同!很是郁闷!!