0
点赞
收藏
分享

微信扫一扫

mysql 高效复制存储数据到另一张表

ZMXQQ233 2022-01-20 阅读 123

最近有个需求,我们老大说让我写个存储过程把一张表的数据分组之后按时间取最近的三条数据,复制到另一张表中。刚开始他也没有说这么明白,我就用存储过程写了。写完之后他才说每天取最近三条数据。我一想,这根本用不到存储过程,直接两条SQL搞定如下:

drop table if exists new_user_contact_rel;

create  table new_user_contact_rel (select a.* from wx_cp_user_contact_rel a where exists (select count(*) from wx_cp_user_contact_rel where external_userid = a.external_userid and follow_user_createtime < a.follow_user_createtime having count(*) < 3) order by a.external_userid );
   

表里面有650万数据,这两条SQL执行完大概一分钟左右吧。如果加索引去优化的话可能更快,由于是每天执行一次,一分钟也不算太久所以先这样也可以。

举报

相关推荐

0 条评论