MySQL Basics-lock mechanism, table level lock, row level lock

Source: Internet
Author: User

One, table-level lock

1. Read lock,lock table t_student Read, add read lock, so that other Sessiona and sessionb can not modify the data, just read the data.

Show processlist; View process, modify the state is waiting for the table level lock, has been waiting for 8s

After unlocking unlock tables, SQL that modifies the data is also executed successfully, as shown in

2. Write the lock , when a process in a table after the write lock, after the process if the update (write, insert, UPDATE, delete), if the write lock is not released, the other process even to view the table permissions are not, only wait for it to release the write lock value, Other processes will be able to complete the corresponding operation. If the process does not update the table, the other processes can only do query operations, but the update operation cannot be implemented.

Second, row-level lock

The MyISAM engine does not support row-level locks, and InnoDB's engines support row-level locks. However, the InnoDB engine is implemented by locking the index entries on the index, which means that the InnoDB uses row-level locks only if the data is retrieved by index criteria, otherwise INNODB will use a table lock.

As shown, both Sessiona and SESSIONB have modified the record with ID 1 in the transaction, Sessiona executed first, and the sessionb is blocked. This is Sessiona a row-level lock on a row with ID 1, causing the other session to be blocked when the row is logged.

Looking at the next scenario, Sessiona modifies the record with ID 1, sessionb the record with ID 2, but does not cause blocking. Because row-level locks depend on the index.

It is important to note, however, that if you do not rely on the index to modify the table, the InnoDB engine uses the table-level lock by default. For example, if the ID in the table t_student is not a primary key or index, then table-level locks are enabled.

Third, deadlock

Originally Sessiona and SESSIONB respectively enabled Id=1 and id=2 row-level locks, but then Sessiona and sessionb respectively modified id=2 and id=1 data, the equivalent of each to operate the lock is not their own. It creates a deadlock.

MySQL Basics-lock mechanism, table level lock, row level lock

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.