Mysql database large table archiving operation

Since a certain system data sheet of the company exceeds 100 million, considering the pressure of the data sheet. So according to a certain point in time the data table is archived. The following operation is to create a new table in the current database to store historical data, and then perform a cleanup operation on the production table. Archived data can be placed on a new database server if possible. (The specific table name and time node can be modified according to your needs)

 

Archive sql:

 Method 1: Copy the table and insert data according to the conditions (this method does not include other indexes except the primary key index)

 

[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;  

 

Method 2: Create an empty table with the same structure and index as the original table

 

[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";  

 

After the data is archived successfully, clean up the data 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";  


 

Copyright statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission. https://blog.csdn.net/bluestarf/article/details/49641047

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325263017&siteId=291194637
Recommended