比较一下这两句的效率哪个好?
要求:
在数据库中已经有大约3——5万人员数据,人员因为有可能存在两个人重名的情况,所以不能把姓名字段作UK。批量导入一批人员数据,大约1万条以下。需要能够提示新导入的数据中有和原来已有人员重名的情况,再逐个判断是否是同一个人,是同一个人则删除一个,否则保留。这种重名的现象一般比较少,应该不会超过10个。
我这么做的,先全都导入,然后选出所有重复姓名的人并且其中有纪录是新导入的。在全部导入之后,我的sql语句这么写的:
select * from xiaoyouInfo where xingming in ( select xingming from xiaoyouInfo group by xingming having count(xingming)>1 ) and xingming in (select xingming from xiaoyouInfo where isnew=1) order by xingming
我同学也写了一个:
select t.* from xiaoyouInfo t where (t.isnew=1 and exists( select xingming from xiaoyouInfo where xingming=t.xingming and isnew!=1)) or ( t.isnew!=1 and exists( select xingming from xiaoyouInfo where xingming=t.xingming and isnew=1)) order by t.xingming
这两个都可以达到目的。不过,我那个用了两个in,会不会效率上输一些呢?他那个相当于两张几万条数据的表联合查询,估计也快不到哪去。到底哪个效率更好一些呢?
请教大家。