MYSQL中删除重复记录的方法

在实际应用中,很可能会碰到一些需要删除某些字段的重复记录,我现在把我能想到的写下来,望高手们补充。

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
rminated 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)

iduidgid
111502
2107502
3100503
4110501
5112501
6104502
7100502
8100501
9102501
10104502
11100502
12100501
13102501
14110501
14 rows in set (0.00 sec)

mysql> create temporary table tmp_wrap select * from users_groups group by uid having count(1) > 1 union all
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)

iduidgid
111502
2107502
3100503
4110501
5112501
6104502
9102501


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)

iduidgid
111502
2107502
3100503
4110501
5112501
6104502
9102501

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)

idselect_typetabletypepossible_keyskeykey_lenrefrowsExtra
1PRIMARYusers_groupsindex(NULL)f_uid4(NULL)14 
2UNIONusers_groupsindex(NULL)f_uid4(NULL)14 
(NULL)UNION RESULT<union1,2>ALL(NULL)(NULL)(NULL)(NULL)(NULL) 

 

query result(3 records)

idselect_typetabletypepossible_keyskeykey_lenrefrowsExtra
1PRIMARY<derived2>ALL(NULL)(NULL)(NULL)(NULL)4 
1PRIMARYarefPRIMARY,f_uidf_uid4b.uid1Using where
2DERIVEDusers_groupsindex(NULL)f_uid4(NULL)14 

 

 

很明显的第二个比第一个扫描的函数要少。

 
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值