MYSQL中删除重复记录的方法

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介:
在实际应用中,很可能会碰到一些需要删除某些字段的重复记录,我现在把我能想到的写下来,望高手们补充。
1、
具体实现如下:

Table         Create Table                                            
------------  --------------------------------------------------------
users_groups  CREATE TABLE `users_groups` (                           
                `id` int(10) unsigned NOT NULL AUTO_INCREMENT,        
                `uid` int(11) NOT NULL,                               
                `gid` int(11) NOT NULL,                               
                PRIMARY KEY (`id`)                                    
              ) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8  

users_groups.txt内容:
1,11,502
2,107,502
3,100,503
4,110,501
5,112,501
6,104,502
7,100,502
8,100,501
9,102,501
10,104,502
11,100,502
12,100,501
13,102,501
14,110,501

mysql> load data infile 'c:\\users_groups.txt' into table users_groups fields
terminated by ',' lines terminated by '\n';
Query OK, 14 rows affected (0.05 sec)
Records: 14  Deleted: 0  Skipped: 0  Warnings: 0

mysql> select * from users_groups;

query result(14 records)

id uid gid
1 11 502
2 107 502
3 100 503
4 110 501
5 112 501
6 104 502
7 100 502
8 100 501
9 102 501
10 104 502
11 100 502
12 100 501
13 102 501
14 110 501
14 rows in set (0.00 sec)
根据一位兄弟的建议修改。
mysql> create temporary table tmp_wrap select * from users_groups group by uid having count(1) >= 1;
Query OK, 7 rows affected (0.11 sec)
Records: 7  Duplicates: 0  Warnings: 0

mysql> truncate table users_groups;
Query OK, 14 rows affected (0.03 sec)

mysql> insert into users_groups select * from tmp_wrap;
Query OK, 7 rows affected (0.03 sec)
Records: 7  Duplicates: 0  Warnings: 0

mysql> select * from users_groups;

query result(7 records)

id uid gid
1 11 502
2 107 502
3 100 503
4 110 501
5 112 501
6 104 502
9 102 501


mysql> drop table tmp_wrap;
Query OK, 0 rows affected (0.05 sec)
2、还有一个很精简的办法。
查找重复的,并且除掉最小的那个。
delete users_groups as a from users_groups as a,
(
select *,min(id) from users_groups group by uid having count(1) > 1
) as b
 where a.uid = b.uid and a.id > b.id;
(7 row(s)affected)
(0 ms taken)
 

query result(7 records)

id uid gid
1 11 502
2 107 502
3 100 503
4 110 501
5 112 501
6 104 502
9 102 501
3、现在来看一下这两个办法的效率。
运行一下以下SQL 语句
create index f_uid on users_groups(uid);
explain select * from users_groups group by uid having count(1) > 1 union all
select * from users_groups group by uid having count(1) = 1;
explain select * from  users_groups as a,
(
select *,min(id) from users_groups group by uid having count(1) > 1
) as b
 where a.uid = b.uid and a.id > b.id;

query result(3 records)

id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY users_groups index (NULL) f_uid 4 (NULL) 14  
2 UNION users_groups index (NULL) f_uid 4 (NULL) 14  
(NULL) UNION RESULT <union1,2> ALL (NULL) (NULL) (NULL) (NULL) (NULL)  
 

query result(3 records)

id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL (NULL) (NULL) (NULL) (NULL) 4  
1 PRIMARY a ref PRIMARY,f_uid f_uid 4 b.uid 1 Using where
2 DERIVED users_groups index (NULL) f_uid 4 (NULL) 14  
 
 
很明显的第二个比第一个扫描的函数要少。





本文转自 yarin 51CTO博客,原文链接:http://blog.51cto.com/yueliangdao0608/81391 ,如需转载请自行联系原作者
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
3天前
|
关系型数据库 MySQL 数据库
docker MySQL删除数据库时的错误(errno: 39)
docker MySQL删除数据库时的错误(errno: 39)
10 0
|
2月前
|
关系型数据库 MySQL 数据库连接
关于MySQL-ODBC的zip包安装方法
关于MySQL-ODBC的zip包安装方法
|
4月前
|
关系型数据库 MySQL
解决MySQL无法插入中文的方法
解决MySQL无法插入中文的方法
26 0
|
4月前
|
设计模式 NoSQL Java
常用的设计模式以及操作Redis、MySQL数据库、各种MQ、数据类型转换的方法
常用的设计模式以及操作Redis、MySQL数据库、各种MQ、数据类型转换的方法
|
2月前
|
SQL 关系型数据库 MySQL
【MySQL】— —熟练掌握用SQL语句实现数据库和基本表的创建。熟练掌握MySQL的安装、客户端登录方法;熟练掌握MySQL的编码、数据类型等基础知识;掌握实体完整性的定义和维护方法、掌握参照完整性
【MySQL】— —熟练掌握用SQL语句实现数据库和基本表的创建。熟练掌握MySQL的安装、客户端登录方法;熟练掌握MySQL的编码、数据类型等基础知识;掌握实体完整性的定义和维护方法、掌握参照完整性
111 1
|
2月前
|
SQL 关系型数据库 MySQL
MYSQL分页limit速度太慢优化方法
MYSQL分页limit速度太慢优化方法
33 0
|
2月前
|
关系型数据库 MySQL 测试技术
mysql中删除数据的几种方法
在MySQL数据库中,删除数据是一个常见的操作,它允许从表中移除不再需要的数据。在执行删除操作时,需要谨慎,以免误删重要数据。
60 3
|
1月前
|
关系型数据库 MySQL Linux
linux CentOS 7.4下 mysql5.7.20 密码改简单的方法
linux CentOS 7.4下 mysql5.7.20 密码改简单的方法
25 0
|
5天前
|
存储 关系型数据库 MySQL
Mysql大数据批量插入方法
Mysql大数据批量插入方法
21 0
|
13天前
|
缓存 关系型数据库 MySQL
mysql用in查询大量数据的方法
在MySQL中使用 IN 子句来查询大量数据时,性能可能会成为一个问题

推荐镜像

更多