Tens of millions of data scales, quickly add index ideas!

Preface

I believe you should be familiar with adding fields, and you can write it out at will. Just add fields to a table in MySQL and execute the following sql:

ALTER TABLE tbl_tpl ADD title(255) DEFAULT '' COMMENT '标题' AFTER id;

However, if the amount of data in a table online is large, the table will be locked when adding fields is performed. This process may take a long time or even cause the service to crash, so this operation is very risky.

Ideas for adding fields to MySQL large tables

① Create a temporary new table, first copy the structure of the old table (including indexes)

create table new_table like old_table;

② Add new fields to the new table
③ Copy the data from the old table

insert into new_table(filed1,filed2…) select filed1,filed2,… from old_table

④ Delete the old table, rename the name of the new table to the name of the old table

However, it should be noted here that when the third step is executed, this process may also take time. At this time, new data comes in, so if the original table has a field that records the writing time of the data, it is best. You can find and execute this After one step of operation, import the data into the new table repeatedly until the data difference is small. However, a very small amount of data may still be lost.

Therefore, if the data in the table is extremely large and the data is complete, it is best to stop the operation.

Another way

  1. Add fields in the slave library, and then switch between master and slave

  2. Use a third-party online tool to change fields

Under normal circumstances, the data volume of hundreds of thousands can be directly added to the field.

to sum up

The above is about the implementation ideas of adding fields to the MySQL large table. I hope that the content of this article can bring some help to your study or work. If you have any questions, you can leave a message to communicate.

Guess you like

Origin blog.csdn.net/A___B___C/article/details/114141088