现在有两张表,一张user主要的字段有username,usercode,depname,depid,adress,phone.一张是user_temp字段相同,但是没有ID相对应.两张表通过usercode和depid来确定是否同一条记录.
现在需要做的有:一是统计有多少条需要更新(usercode,depid相同,其它有一个或者多个字段不同),多少需要删除(usercode,depid在user表中有,在user_temp没有),多少需要插入(与删除相反);
二是,怎么样执行上面的更新,插入和删除.

解决方案 »

  1.   

    抛砖引玉,先写下我的同步的select count(*) number from user a,user_temp b where  a.userflag=b.userflag and a.depid=b.depid  and ( a.username!=b.username or a.depname!=b.depname) 
    select count(*) number from user a left join user_temp b on  a.userflag=b.userflag and a.depid=b.depid where b.userflag is null  and b.depid is null 
    select count(*) number from user_temp a left join user b on  a.userflag=b.userflag and a.depid=b.depid  where b.userflag is null  and b.depid is null  
      

  2.   

    select COUNT(*) from user where CONCAT(userflag,"--",depid) not in (select CONCAT(userflag,"--",depid) from user_temp) 尝试了这种in / not in 速度也是很差
      

  3.   

    usercode、depid建立复合索引没有
      

  4.   

    贴出 
    explain select count(*) number from user a,user_temp b where  a.userflag=b.userflag and a.depid=b.depid  and ( a.username!=b.username or a.depname!=b.depname)  
    explain select count(*) number from user a left join user_temp b on  a.userflag=b.userflag and a.depid=b.depid where b.userflag is null  and b.depid is null  
    explain select count(*) number from user_temp a left join user b on  a.userflag=b.userflag and a.depid=b.depid  where b.userflag is null  and b.depid is null 结果看一下,分析优化。