SQL语句删除重复的记录
发布网友
发布时间:2022-04-27 02:25
我来回答
共4个回答
懂视网
时间:2022-04-08 07:05
(select max(id) from 表 group by mobilePhone,officePhone,email )
or
delete from 表 where id not in
(select min(id) from 表 group by mobilePhone,officePhone,email )
[sql] view plaincopy
- delete from 表 where id not in
- (select max(id) from 表 group by mobilePhone,officePhone,email )
- or
- delete from 表 where id not in
(select min(id) from 表 group by mobilePhone,officePhone,email )
其中下面这条会稍快些。上面这条数据对于100万以内的数据效率还可以,重复数1/5的情况下几分钟到几十分钟不等,但是如果数据量达到300万以上,效率骤降,如果重复数据再多点的话,常常会几十小时跑不完,有时候会锁表跑一夜都跑不完。无奈只得重新寻找新的可行方法,今天终于有所收获:
Java代码
- //查询出唯一数据的ID,并把他们导入临时表tmp中
-
- select min(id) as mid into tmp from 表 group by mobilePhone,officePhone,email
-
-
-
- //查询出去重后的数据并插入finally表中
-
- insert into finally select (除ID以外的字段) from customers_1 where id in (select mid from tmp)
[java] view plaincopy
- //查询出唯一数据的ID,并把他们导入临时表tmp中
- select min(id) as mid into tmp from 表 group by mobilePhone,officePhone,email
- //查询出去重后的数据并插入finally表中
insert into finally select (除ID以外的字段) from customers_1 where id in (select mid from tmp)
效率对比:用delete方法对500万数据去重(1/2重复)约4小时。4小时,很长的时间。
用临时表插入对500万数据去重(1/2重复)不到10分钟。
其实用删除方式是比较慢的,可能是边找边删除的原因吧,而使用临时表,可以将没有重复的数据ID选出来放在临时表里,再将表的信息按临时表的选择出来的ID,将它们找出来插入到新的表,然后将原表删除,这样就可以快速去重啦。
SQL语句去掉重复记录,获取重复记录
按照某几个字段名称查找表中存在这几个字段的重复数据并按照插入的时间先后进行删除,条件取决于order by 和row_num。
方法一按照多条件重复处理:
Sql代码
- delete tmp from(
-
- select row_num = row_number() over(partition by 字段,字段 order by 时间 desc)
-
- from 表 where 时间> getdate()-1
-
- ) tmp
-
- where row_num > 1
[sql] view plaincopy
- delete tmp from(
- select row_num = row_number() over(partition by 字段,字段 order by 时间 desc)
- from 表 where 时间> getdate()-1
- ) tmp
where row_num > 1
方法二按照单一条件进行去重:
Sql代码
- delete from 表 where 主键ID not in(
-
- select max(主键ID) from 表 group by 需要去重的字段 having count(需要去重的字段)>=1
-
- )
[sql] view plaincopy
- delete from 表 where 主键ID not in(
- select max(主键ID) from 表 group by 需要去重的字段 having count(需要去重的字段)>=1
)
注意:为提高效率如上两个方法都可以使用临时表, not in 中的表可以先提取临时表#tmp,
然后采用not exists来执行,为避免数量过大,可批量用Top控制删除量
Java代码
- delete top(2) from 表
- where not exists (select 主键ID
from #tmp where #tmp.主键ID=表.主键ID)
用SQL语句去掉重复的记录(转)
标签:
热心网友
时间:2022-04-08 04:13
删除重复的数据
delete from tb where id not in (
select id from
(select fileSize,fileName ,max(id) id from tb group by filesize,filename ) a
)
现在完成了重复数据的删除,主要是利用了找出某个分组中最大的那个id,其中包括了所有不重复的id,然后使用not in将需要保留的排除。
热心网友
时间:2022-04-08 05:31
COL1 中有重复记录(COL1,COL2为主键),如何删除
1、有少数重复记录(在col1,col2上有索引比较好)
DELETE T
WHERE (COL1,COL2) IN
(SELECT COL1,COL2 FROM T GROUP BY COL1,COL2 HAVING COUNT(*) > 1)
AND
ROWID NOT IN
(SELECT MIN(ROWID) FROM T GROUP BY COL1,COL2 HAVING COUNT(*) > 1)
2、大部份记录有重复记录
DELETE T WHERE ROWID NOT IN
(SELECT MIN(ROWID) FROM T GROUP BY COL1,COL2)
3、其他写法
DELETE T WHERE ROWID IN
(SELECT A.ROWID FROM T A,T B
WHERE A.COL1=B.COL1 AND A.COL2 = B.COL2 AND A.ROWID > B.ROWID)
######################################
10. 删除重复记录
最高效的删除重复记录方法 ( 因为使用了ROWID)
DELETE FROM EMP E
WHERE E.ROWID > (SELECT MIN(X.ROWID)
FROM EMP X
WHERE X.EMP_NO = E.EMP_NO);
11. 用TRUNCATE替代DELETE
当删除表中的记录时,在通常情况下, 回滚段(rollback segments ) 用来存放可以被恢复的信息. 如果你没有COMMIT事务,ORACLE会将数据恢复到删除之前的状态(准确地说是
恢复到执行删除命令之前的状况)
而当运用TRUNCATE时, 回滚段不再存放任何可被恢复的信息.当命令运行后,数据不能被恢复.因此很少的资源被调用,执行时间也会很短.
(译者按: TRUNCATE只在删除全表适用,TRUNCATE是DDL不是DML)
12. 尽量多使用COMMIT
只要有可能,在程序中尽量多使用COMMIT, 这样程序的性能得到提高,需求也会因为COMMIT所释放的资源而减少:
COMMIT所释放的资源:
a. 回滚段上用于恢复数据的信息.
b. 被程序语句获得的锁
c. redo log buffer 中的空间
d. ORACLE为管理上述3种资源中的内部花费
参考资料:http://publish.it168.com/2005/1124/20051124122301.shtml
热心网友
时间:2022-04-08 07:06
1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断
select * from people where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录
delete from people where peopleName in (select peopleName from people group by peopleName having count(peopleName) > 1) and peopleId not in (select min(peopleId) from people group by peopleName having count(peopleName)>1)
3、查找表中多余的重复记录(多个字段)
select * from vitae a where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录
delete from vitae a where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1) and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)
5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录
select * from vitae a where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1) and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)
6.消除一个字段的左边的第一位:
update tableName set [Title]=Right([Title],(len([Title])-1)) where Title like '村%'
7.消除一个字段的右边的第一位:
update tableName set [Title]=left([Title],(len([Title])-1)) where Title like '%村'
8.假删除表中多余的重复记录(多个字段),不包含rowid最小的记录
update vitae set ispass=-1 where peopleId in (select peopleId from vitae group by peopleId