使用SQL语句去掉重复的记录【两种方法】

作者:deng214 时间:2024-01-18 16:55:59 

海量数据(百万以上),其中有些全部字段都相同,有些部分字段相同,怎样高效去除重复?

如果要删除手机(mobilePhone),电话(officePhone),邮件(email)同时都相同的数据,以前一直使用这条语句进行去重:


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 )

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万以上,效率骤降,如果重复数据再多点的话,常常会几十小时跑不完,有时候会锁表跑一夜都跑不完。无奈只得重新寻找新的可行方法,今天终于有所收获:


//查询出唯一数据的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)

//查询出唯一数据的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。

方法一按照多条件重复处理:


delete tmp from(  
select row_num = row_number() over(partition by 字段,字段 order by 时间 desc)  
from 表 where 时间> getdate()-1  
) tmp  
where row_num > 1
delete tmp from(
select row_num = row_number() over(partition by 字段,字段 order by 时间 desc)
from 表 where 时间> getdate()-1
) tmp
where row_num > 1

方法二按照单一条件进行去重:


delete from 表 where 主键ID not in(  
select max(主键ID) from 表 group by 需要去重的字段 having count(需要去重的字段)>=1  
)
delete from 表 where 主键ID not in(
select max(主键ID) from 表 group by 需要去重的字段 having count(需要去重的字段)>=1
)

注意:为提高效率如上两个方法都可以使用临时表, not in 中的表可以先提取临时表#tmp,

然后采用not exists来执行,为避免数量过大,可批量用Top控制删除量


delete top(2) from 表  
  where not exists (select 主键ID  
from #tmp where #tmp.主键ID=表.主键ID)

总结

以上所述是小编给大家介绍的使用SQL语句去掉重复的记录网站的支持!

来源:https://blog.csdn.net/deng214/article/details/80430109

标签:sql,去重复,记录
0
投稿

猜你喜欢

  • python 实现学生信息管理系统的示例

    2021-05-01 01:37:24
  • MySQL数据库的事务和索引详解

    2024-01-21 00:40:48
  • Python语音合成的项目实战(PyQt5+pyttsx3)

    2021-06-15 09:14:13
  • Pandas 如何处理DataFrame中的inf值

    2021-03-12 14:03:56
  • 基于vue实现swipe分页组件实例

    2024-04-30 10:30:56
  • 在Oracle PL/SQL中游标声明中表名动态变化的方法

    2009-02-28 10:39:00
  • 对python中Matplotlib的坐标轴的坐标区间的设定实例讲解

    2023-06-28 10:18:40
  • Div+CSS布局入门教程

    2007-09-13 12:52:00
  • python中数组和列表的简单实例

    2021-04-15 20:04:42
  • Python实现通过解析域名获取ip地址的方法分析

    2023-08-14 16:10:29
  • 解决缩小图标变样问题

    2007-10-08 19:13:00
  • python中range()与xrange()用法分析

    2021-03-23 00:31:30
  • python实现学生成绩测评系统

    2023-08-09 19:40:56
  • PyCharm代码回滚,恢复历史版本的解决方法

    2021-01-08 21:19:55
  • python logging日志模块的详解

    2021-04-27 19:16:55
  • 用 SQL 脚本将 Access 导入 MSSQL 2000/2005 方法

    2008-10-22 13:51:00
  • Django Serializer HiddenField隐藏字段实例

    2022-10-29 02:39:07
  • 关于Python3的import问题(pycharm可以运行命令行import错误)

    2022-11-11 11:34:11
  • python 实现socket服务端并发的四种方式

    2022-08-09 22:19:46
  • python做量化投资系列之比特币初始配置

    2021-06-28 06:01:31
  • asp之家 网络编程 m.aspxhome.com