MySQL type conversion causes row locks to be promoted to table locks

Source: Internet
Author: User
Tags mysql in

In the MySQL write statement, the table column assignment and table type does not match, the MySQL bottom of the optimizer to play a role, will do a forced type conversion, this time can be normal operation, but will cause row lock escalation to table lock. Examples such as the following

For example, table field type student:
The table reads as follows:

Open two sessions Session window and change the automatic commit mode of MySQL in two session window to manual commit

>set Autocommit=false;

The UPDATE statement is executed in Session window 1, but the transaction is not committed. The age column specifies an int type when the table is being built, where the UPDATE statement is assigned with the string ' 100 ', and the string ' 100 ' is automatically coerced into 100 in the MySQL optimizer, and then the SQL retrieval is performed.

>update student set class=3 where age= ' 100 '

Then I'll talk to you. Windows 2 performs update operations on data that is not okay

>update student set age=28 where name= ' lzj ';

Normally, two SQL statements operate with different row data and do not affect each other, but the update operation in actual session 1 blocks the update operation in session 2
The update operation was performed in Session 1, but the transaction commit was not performed, and the isolation level of the transaction was read Committed, so in session 2 it was not seen to be the result of the update in conversation 1. However, when you perform an update operation on other rows in reply 2, a blocking occurs. It can be seen that the assignment of SQL statements in Session 1 has a strong turn, resulting in session 1 being escalated to a table lock by a row lock, locking the entire student table, thus blocking SQL in session 2. The following transaction commit is performed for the update operation in Session 1, then the update operation in session 2 will continue to execute
After a manual commit transaction is performed on the update operation in session 1 commit , session 1 releases the student table lock and the update operation in session 2 can continue.
Finally, the update to session 2 also performs commit transactional commits, and two SQL updates are complete, and the student table reads as follows:

From the above case, when the SQL statement assignment does not match the table column type, the MySQL optimizer is forced to convert to a matching type, causing the row lock to escalate to a table lock. Therefore, the development must pay attention to the type of matching, avoid row lock escalation to table lock, affecting concurrency performance.

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.