mysql how to add fields to the big table

When you need to add table ddl operations such as indexing, add or delete columns, the amount of data online directly affect the hour is not to modify the table structure
when expressed to millionaires and online data can not directly modify table structure

The following is a specific process:
1, the backup data
SELECT * from ih_order INTO outfile '/bak/order.txt';
MySQL> SELECT * from ih_order INTO outfile 'D: /bak/order.txt';
Query the OK, 10001000 rows affected (1 min 30.18 sec)

2, the physical copy of the original table structure
create table ih_order_bak like ih_order;

3, performing the table structure modification, where the index is increased
alter table `ih_order_bak` add index (` consulate`);

4, the original table data into the new table
MySQL> INSERT INTO ih_order_bak SELECT * from ih_order;
Query the OK, 10001000 rows affected (10.30 min. 6 sec)
Records: 10001000 Duplicates: 0 Warnings: 0

5, delete the original table
drop table ih_order;

6, rename the new table the original table
rename table ih_order_bak to ih_order;


Note:
1, this method will affect business operations, it is recommended to use the online tool ddl: pt-online-schema-change , and in less time visit
2, most of the alter table operation will involve the lock -> copy to new table -> rename -> unlock process, the lock table very long time, and the process can not be alter table kill, upon execution would not be undone.
In mysql5.5 and earlier, things run production environment, execute the alter is a very difficult operation on a large table (more than several million records) in. Because the reconstruction table and the table lock, affect the user's.
From mysql5.6 beginning, Online DDL characteristics are introduced. Enhance the wide variety of Alter Table operation copies table and avoid lock table, while running Alter operation is allowed to run select, insert, update, delete statement. Therefore, in the latest version, we can suppress the file copy and locked by using ALGORITHM and LOCK options.
But even mysql5.6, there are still some alter operations (add / remove columns, add / remove the primary key, change the data type, etc.) need to be rebuilt
-------------------- -
author: love Code love life
source: CSDN
original: https: //blog.csdn.net/nuli888/article/details/52443165
copyright: This article is a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin www.cnblogs.com/ExMan/p/11247066.html