• Mysql数据库大表归档操作


    由于公司的某个系统数据表过亿,考虑到数据表的压力。所以根据某个时间点对数据表做了一个归档。以下的操作是在当前的数据库新建表用于存储历史数据,然后再对生产表做一个清理操作。如果有条件的话可以把归档的数据放在一台新的数据库服务器上。(具体的表名和时间结点根据自己需要修改)

    归档sql:

     方法一:  复制表并且按照条件插入数据(此种方法除了主键索引不包括其他索引)

    [sql] view plain copy
     
    1. CREATE TABLE lime_survey_549656_20151001 as select * from lime_survey_549656  where submitdate < "2015-10-01 00:00:00";  
    2.    
    3. ALTER TABLE lime_survey_549656_20151001 change id id int primary key auto_increment;  
    4.    
    5. CREATE TABLE lime_survey_186194_20151001 as select * from lime_survey_186194 where submitdate < "2015-10-01 00:00:00";  
    6.    
    7. ALTER TABLE lime_survey_186194_20151001 change id id int primary key auto_increment;  
    8.    
    9. CREATE TABLE lime_survey_279575_20151001 as select * from lime_survey_279575 where submitdate < "2015-10-01 00:00:00";  
    10.    
    11. ALTER TABLE lime_survey_279575_20151001 change id id int primary key auto_increment;  

    方法二: 创建一张空表,结构和索引和原表一样

    [sql] view plain copy
     
    1. create table lime_survey_549656_20151001 like lime_survey_549656;   
    2. INSERT INTO lime_survey_549656_20151001 select * from lime_survey_549656  where submitdate < "2015-10-01 00:00:00";  
    3.   
    4. create table lime_survey_186194_20151001 like lime_survey_186194;   
    5. INSERT INTO lime_survey_186194_20151001 select * from lime_survey_186194  where submitdate < "2015-10-01 00:00:00";  
    6.   
    7. create table lime_survey_279575_20151001 like lime_survey_279575;   
    8. INSERT INTO lime_survey_279575_20151001 select * from lime_survey_279575  where submitdate < "2015-10-01 00:00:00";  

    数据归档成功后清理数据sql:

    [sql] view plain copy
     
    1. delete from lime_survey_549656  where submitdate < "2015-10-0100:00:00";  
    2.    
    3. delete from lime_survey_186194  where submitdate < "2015-10-0100:00:00";  
    4.    
    5. delete from lime_survey_279575  where submitdate < "2015-10-0100:00:00";  


    版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/bluestarf/article/details/49641047
  • 相关阅读:
    这是阿里技术专家对 SRE 和稳定性保障的理解
    阿里四年技术 TL 的得失总结:如何做好技术 Team Leader
    深度 | 阿里云蒋江伟:什么是真正的云原生?
    亲历者说 | 完整记录一年多考拉海购的云原生之路
    Seata RPC 模块的重构之路
    对容器镜像的思考和讨论
    20 行代码:Serverless 架构下用 Python 轻松搞定图像分类和预测
    怎么提升写代码的能力
    云原生 DevOps 的 5 步升级路径
    dubbo-go 白话文 | 从零搭建 dubbogo 和 dubbo 的简单用例
  • 原文地址:https://www.cnblogs.com/soundcode/p/8989717.html
Copyright © 2020-2023  润新知