欢迎使用CSDN-markdown编辑器

来源:互联网 发布:格灵深瞳怎么样 知乎 编辑:程序博客网 时间:2024/05/22 07:48

一般去重的SQL语句去重是这样写delete from 表名 a where a.rowid != ( select max(b.rowid) from 表名 b where a.字段1 = b.字段1 and a.字段2 = b.字段2 ),然而这样的效率很低,最近公司项目因为大量测试产生了大量的垃圾数据,总数据量为10w垃圾数据为1.5w,以这种SQL执行时会直接卡死数据库造成停止响应的后果.所以便结合网络上的资源写了这条语句.

--创建临时表,临时表中保存重复的数据和rowID--create table 临时表 as select  b.字段一,b.字段二,b.rowid as dataid from 去重表 b where b.rowid not in (select MAX(a.ROWID) dataid from 去重表 a GROUP BY a.字段一,a.字段二); --以临时表中rowID为唯一指定删除数据库中的重复数据--delete from 去重表 a  where a.rowid in  ( select b.dataid from 临时表 b  ); --提交事务--commit;--删除临时表,节省服务器资源--drop table 临时表
原创粉丝点击