MySQL5.6 online DDL table change test

Source: Internet
Author: User

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 to create a temporary table)

2. insert into tmp select * from t1 copy the original table data to the temporary table and update 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 will be added 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.

 
 
  1. sysbench --test=oltp --mysql-table-engine=innodb --oltp-table-size=10000000 --max-requests=100 --num-threads=16 
  2. --mysql-host=192.168.110.140 --mysql-port=3306 --mysql-user=admin --mysql-password=123456 --mysql-db=test 
  3. --mysql-socket=/tmp/mysql.sock prepare 

In session 1, run:

 
 
  1. alter table sbtest add name varchar(10) after pad; 

Session 2: Execute:

 
 
  1. delete from sbtest where id=1000;
 
 
  1. insert into sbtest values(1000,1,'abc','abc','abc'); 
 
 
  1. update sbtest set k=11 where id=1000; 

At this time, you will find that the table is not locked and the execution is complete successfully.

650) this. width = 650; "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/2255334Q1-0.jpg "alt =" "/>

However, in MySQL5.5, such an operation locks the table ,:

650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/22553321a-1.jpg "/>

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

In Session 1:

 
 
  1. select * from sbtest; 

Deliberately execute a query with large results, and then delete the added field name.

 
 
  1. alter table sbtest drop name; 

The table is locked ,:

650) this. width = 650; "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/2255334010-2.jpg "alt =" "/>

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:

650) this. width = 650; "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/2255332337-3.jpg "alt =" "/>

 

This article is from the "hechun's technical column" blog, please be sure to keep this source http://hcymysql.blog.51cto.com/5223301/1185846

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.