1.先备份原表数据(为了不影响原表使用,如果暂时没人用,可以省略此步骤)
sql
create table source_tmp as select * from source;
2.改造原表ddl,创建分区表source_partition
具体参考:https://www.cnblogs.com/hujunwei/p/19178124
3.把备份表的数据导入到新建的分区表
sql
-- 数据量小的话可以直接insert into select,大的话可以加日期范围筛选,这里实测:同步100多万数据需要20s左右。
insert into source_partition select * from source_tmp where ...;
4.数据完整性校验
sql
select count(1) from source;
select count(1) from source_partition;
5.修改表名
sql
RENAME source TO source_back;
RENAME source_partition TO source;