session1 | session2 |
---|---|
mysql>set autocommit=0; Query OK,0 rows affected(0.00sec) |
mysql>set autocommit=0; Query OK,0 rows affected(0.00sec) |
mysql> select * from test_lock wehre id = 1 for update; ±-----±-----+ | id | name | | 1 | hua zi | 1 row in set(0.00sec) |
|
mysql> select * from test_lock wehre id = 2 for update; ±-----±-----+ | id | name | | 2 | guo zi | 1 row in set(0.00sec) |
|
[2] When using non-indexed fields to search data, it will be upgraded to a table lock and the rest of the columns cannot be operated. | |
session1 | session2 |
---- | ---- |
mysql>set autocommit=0; Query OK,0 rows affected(0.00sec) |
mysql>set autocommit=0; Query OK,0 rows affected(0.00sec) |
mysql> select * from test_lock wehre name = ‘hua zi’ for update; ±-----±-----+ | id | name | | 1 | hua zi | 1 row in set(0.00sec) |
|
mysql> select * from test_lock wehre name = ‘guo zi’ for update; wait |
|
【3】BecauseMySQL The row lock is a lock added for the index, not a lock added for the record, so although it accesses records of different rows, if the same index key is used, a lock conflict will occur. Pay attention to this when applying design. Although session_2 and session_1 access different records, because the same index is used, you need to wait for the lock. |
|
session1 | session2 |
---- | ---- |
mysql>set autocommit=0; Query OK,0 rows affected(0.00sec) |
mysql>set autocommit=0; Query OK,0 rows affected(0.00sec) |
mysql> select * from test_lock wehre id = 1 and name = ‘hua zi’ for update; ±-----±-----+ | id | name | | 1 | hua zi | 1 row in set(0.00sec) |
|
mysql> select * from test_lock wehre id = 1 and name = ‘guo zi’ for update; wait |
|
【4】When the table has multiple indexes, different transactions can use different indexes to lock different rows. In addition, whether it is a primary key index, a unique index or a normal index,InnoDB Line locks are used to lock data. |
|
session1 | session2 |
---- | ---- |
mysql>set autocommit=0; Query OK,0 rows affected(0.00sec) |
mysql>set autocommit=0; Query OK,0 rows affected(0.00sec) |
mysql> select * from test_lock wehre id = 1 for update; ±-----±-----+ | id | name | | 1 | hua zi | 1 row in set(0.00sec) |
|
mysql> select * from test_lock wehre name = ‘guo zi’ for update; ±-----±-----+ | id | name | | 2 | guo zi | 1 row in set(0.00sec) |
|
mysql> select * from test_lock wehre name = ‘hua zi’ for update; wait |