最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

MYSQL的中删除重复记录的方法和代码(2)_MySQL

来源:动视网 责编:小采 时间:2020-11-09 17:59:03
文档

MYSQL的中删除重复记录的方法和代码(2)_MySQL

MYSQL的中删除重复记录的方法和代码(2)_MySQL:bitsCN.com 4 rows in set (0.00 sec)mysql> create temporary table tmp_wrap select * from users_groups group by uid having count(1) > 1 union allselect * from users_groups group by uid having count(1) = 1;Query OK, 7 rows affected (0.11 sec)
推荐度:
导读MYSQL的中删除重复记录的方法和代码(2)_MySQL:bitsCN.com 4 rows in set (0.00 sec)mysql> create temporary table tmp_wrap select * from users_groups group by uid having count(1) > 1 union allselect * from users_groups group by uid having count(1) = 1;Query OK, 7 rows affected (0.11 sec)


bitsCN.com

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


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

bitsCN.com

文档

MYSQL的中删除重复记录的方法和代码(2)_MySQL

MYSQL的中删除重复记录的方法和代码(2)_MySQL:bitsCN.com 4 rows in set (0.00 sec)mysql> create temporary table tmp_wrap select * from users_groups group by uid having count(1) > 1 union allselect * from users_groups group by uid having count(1) = 1;Query OK, 7 rows affected (0.11 sec)
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top