Table lock, Row lock difference

Source: Internet
Author: User

In the invocation of the stored procedure, the table lock is involved, the concept of row lock: The so-called difference: when indexed is a row lock, when there is no index is the table.

InnoDB row locks are indexed, and tables with no indexes are locked across the table.

Table lock Demo (no index)

Session1:

Mysql> set autocommit=0;

Mysql> select * from Innodb_test;
+------+-------------+
| ID | name |
+------+-------------+
| 1 | Woshiceshi |
| 2 | Woshiceshi2 |
| 3 | Woshiceshi3 |
+------+-------------+

Mysql> SELECT * from innodb_test where id = 2 for update;
+------+------------+
| ID | name |
+------+------------+
| 2 | Woshiceshi2 |
+------+------------+

Session2:

mysql> Update innodb_test set name= ' sjis ' where id = 1;
In wait state ....

Then back to Session1 commit, Session2 came out of the result (locked for 8 seconds, after 8 seconds or so to Session1 submitted).

mysql> Update innodb_test set name= ' sjis ' where id = 1;
Query OK, 1 row affected (8.11 sec)
Rows matched:1 changed:1 warnings:0

The result: My for update operation in Session1 seemed to lock only the rows with ID 2 in fact locking the whole table so that the subsequent session2 of row update with ID 1 had to wait for the release of the Session1 lock.

Row lock Demo (index is ID)

Session1:
Mysql> ALTER TABLE Innodb_test Add index idx_id (ID);
Query OK, 4 rows affected (0.01 sec)
Records:4 duplicates:0 warnings:0

Mysql> SELECT * from innodb_test where id = 2 for update;
+------+------------+
| ID | name |
+------+------------+
| 2 | Woshiceshi2 |
+------+------------+

Session2:

mysql> Update innodb_test set name= ' Wohaishiceshi ' where id = 1;
Query OK, 1 row affected (0.02 sec)
Rows matched:1 changed:1 warnings:0
Mysql> SELECT * from innodb_test where id = 1;
+------+---------------+
| ID | name |
+------+---------------+
| 1 | Wohaishiceshi |
+------+---------------+
1 row in Set (0.00 sec)

Experiment results: This time the lock is a locked row, so there are no locked rows (rows with an ID of 2) can be update ...

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.