MySQL5.6 online DDL table change test

Source: Internet
Author: User
First, I would like to popularize the basic knowledge here: in previous versions, online table modification, such as adding a field, the internal principle is as follows: 1. createtabletmpliket1 (create a temporary table) 2. insertintotmpselec

First, I would like to popularize the basic knowledge here: in previous versions, online table modification, such as adding a field, the internal principle is as follows: 1. createtabletmpliket1 (create a temporary table) 2. insertintotmpselec

First, popularize the basic knowledge here:

In earlier versions, online table modification, for example, adding a field, works as follows:

1. create table tmp like t1 (create a temporary table)

2. insert into tmp select * from t1 (one row copies the original table data to the temporary table and updates the index)

3. drop table t1; rename table tmp to t1 (delete the original table and change the temporary table to the original table t1)

In this process, the S lock (shared lock) will be applied to table t1, so this price is very high.

In MySQL5.6, It is optimized. When a field is added to session 1, other sessions add, delete, modify, and query fields. The table is not affected and will not be locked.

For example, in this test, you can use Sysbench to generate a data record of 10 million rows. If the data size is larger, you can see the effect.

In session 1, run:

  • Alter table sbtest add name varchar (10) after pad;
  • Session 2: Execute:

  • Insert into sbtest values (1000,1, 'abc ');
  • At this time, you will find that the table is not locked and the execution is complete successfully.

    However, in MySQL5.5, the Hong Kong Space and website space will lock the table ,:

    So does MySQL5.6 online DDL lock the table? Let's test again like this:

    In Session 1:

  • Select * from sbtest;
  • Deliberately execute a query with large results, and then delete the added field name.

  • Alter table sbtest drop name;
  • At this time, the table will be locked. Hong Kong Space ,:

    That is to say, when an alter table is executed, adding, deleting, modifying, and querying the table will not lock the table. Before that, when the table is accessed, you must wait until it is executed.

    Therefore, when going online in the early morning, be sure to check whether a slow SQL statement operates the table at this moment to avoid lock wait during table modification.

    Reference manual:

    This article is from the "hechun's technical column" blog. Be sure to keep this source

    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.