How to add fields to a large data table in MySQL

How to add fields to a large data table in MySQL

Preface

I believe everyone is familiar with adding fields. You can easily write it out. To add a field to a MySQL table, just execute the following SQL:

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

However, if an online table has a large amount of data, adding a field will lock the table. This process may take a long time or even cause the service to crash, so this operation is very risky.

So, the idea of ​​adding fields to a large MySQL table is as follows:

① 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 and rename the new table to the name of the old table

However, it should be noted that when executing the third step, this process may also take time. At this time, new data will come in, so it would be best if the original table has a field that records the time when the data was written. You can find the data after executing this step and import it into the new table repeatedly until the data difference is very small. However, a very small amount of data may still be lost.

Therefore, if the data in the table is particularly large and the data integrity must be guaranteed, it is best to shut down the operation.

Another method:

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

2. Use a third-party online field modification tool

Generally speaking, for data volumes of hundreds of thousands, you can directly add fields.

Summarize

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

You may also be interested in:
  • MYSQL uses .frm to restore the data table structure
  • Summary of methods for copying table structure in MySQL
  • Summary of MySQL table structure modification commands
  • Detailed example of how to modify the table structure in MySQL
  • Some things to note when modifying the table structure in MySQL
  • Mysql method of copying table structure and table data
  • mysql alter table command to modify table structure example
  • MySQL data source table structure diagram
  • Detailed explanation of Metadata Lock that you must know when changing the MySQL table structure
  • Analysis of how to create a stored procedure in MySQL to add new fields to a data table
  • mysql data table output according to a certain field classification
  • Basic operations of MySQL data tables: table structure operations, field operation example analysis

<<:  Use node-media-server to build a simple streaming media server

>>:  A practical record of handling the ddgs and qW3xT.2 mining viruses implanted in Linux servers

Recommend

MySQL foreign key (FOREIGN KEY) usage case detailed explanation

Introduction: The disadvantages of storing all da...

Pure CSS implementation of radio and checkbox effect example

radio-and-checkbox Pure CSS to achieve radio and ...

How to view nginx configuration file path and resource file path

View the nginx configuration file path Through ng...

Detailed explanation of Truncate usage in MySQL

Preface: When we want to clear a table, we often ...

Implementation of MySQL master-slave status check

1. Check the synchronization status of A and B da...

Summary of situations where MySQL indexes will not be used

Types of Indexes in MySQL Generally, they can be ...

MySQL 8.0.21 installation steps and problem solutions

Download the official website First go to the off...

Implementation steps of encapsulating components based on React

Table of contents Preface How does antd encapsula...

Steps for restoring a single MySQL table

When I was taking a break, a phone call completel...

Linux Basic Tutorial: Special Permissions SUID, SGID and SBIT

Preface For file or directory permissions in Linu...

Vue folding display multi-line text component implementation code

Folding display multi-line text component Fold an...

How to use MySQL common functions to process JSON

Official documentation: JSON Functions Name Descr...