Mitigating Practices for updating the table structure of big Data

Source: Internet
Author: User
Scene

A few days ago, a problem was encountered, to add a new field to a table with a larger amount of data in the online database, and to assign a default value,
It takes a long time to execute, and if SQL is executed directly on the basis of the original table, it is feared that the table or even the database will be deadlocked.
Chat with the team brother to find a way, do not know also can draw on.

Workaround create a temporary table based on the source table

CREATE TABLE t_bdcards_temp like t_bdcards;

Add a field to a temporary table

ALTER TABLE T_bdcards_temp
ADD COLUMN bookId BIGINT NULL
DEFAULT NULL
COMMENT ' corresponding book ID ' after user_id;

Inserting data from a source table into a temporary table

INSERT into T_bdcards_temp (
Card_num,
STATUS,
CTime
USER_ID,
BookId
) SELECT
Card_num,
STATUS,
CTime
USER_ID,
100031
From
T_bdcards;

Verify that the data is correct

SELECT
COUNT (ID)
From
T_bdcards_temp;

SELECT
COUNT (ID)
From
T_bdcards;

Change the name of the source table to another noun

RENAME TABLE t_bdcards to T_bdcards_blank;

Modify the temporary table to the name of the source table

RENAME TABLE t_bdcards_temp to T_bdcards;

Mitigating Practices for updating the table structure of big Data

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.