只要涉及到大数据,那就免不了和数据打交道
一、没有分区的表备份以及数据迁移
- 备份表(不包括表字段主键以及索引)
create table new_table_name select * from old_table_name where 1=1;
#查询下新表的字段结构
desc new_table_name;
- 此时经排查,表没有分区,则:
insert into new_table_name select * from old_table_name;
#查询下新表中的数据和原表的比较下
select count(*) from new_table_name;
select count(*) from old_table_name;
3.最后需要删除原表
drop table old_table_name;
#还得确认下,源表是否存在(输入查询语句报错就可以);
select * from old_table_name;
二、存在分区的表备份以及数据迁移
- 备份表(包括表字段主键以及索引)
create table new_table_name like old_table_name;
#查询下新表的字段结构
desc new_table_name;
- 此时经排查,表有分区,则:(一般情况下,点开数据库中的表,会显示列、分区、桶,分区有数据)
#partition(字段)中的分区要和表中的分区字段名字一样
insert into new_table_name partition(字段) select * from old_table_name;
#查询下新表中的数据和原表的比较下
select count(*) from new_table_name;
select count(*) from old_table_name;
3.最后需要删除原表
drop table old_table_name;
#还得确认下,源表是否存在(输入查询语句报错就可以);
select * from old_table_name;
PS:一般情况都是要备份字段的主键和索引的,咱们下期见