In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
Editor to share with you how to achieve mysql table lock demonstration statement, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
First of all, look at the table structure, engine innodb
+-- +
| | id | name | seat | |
+-- +
| | 1 | Administrator | 98 |
| | 2 | maintenance personnel | 98 |
| | 3 | Director | 97 |
| | 4 | head teacher | 96 | |
+-- +
Id is the primary key and there are no other indexes.
Let's take a look at the watch lock.
Send sql:
Set autocommit=0
Select * from role where seat=98 for update
Note here that set autocommit=0 is essential because the lock is automatically released if the data is submitted soon.
Send another sql query: select * from role where id = 1
Display the results:
+-- +
| | id | name | seat | |
+-- +
| | 1 | Administrator | 98 |
+-- +
1 row in set (0.00 sec)
This is because the select operation is not about locking.
Then look at the write operation: update role set seat=99 where id = 3
As you can see, after a long wait (after exceeding the default execution time of mysql), the following result is displayed
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
At this point, let's look at the locking record:
Send sql:SHOW PROCESSLIST
+-
-+
| | Id | User | Host | db | Command | Time | State | Info |
| |
+-
-+
| | 7 | root | localhost:50903 | mybatis | Sleep | 1403 | NULL | NULL |
| |
| | 16 | root | localhost:51326 | mybatis | Query | 0 | NULL | SHOW PROCESSL |
IST |
+-
-+
Column 6, Time 1403, represents the lock time, in seconds.
Conclusion: when the column after the where condition is not an index, plus for update locks the entire table so that no subsequent records can read or write. For example, in this id=3, seat=97. It is not the condition of inquiry 98. It's still locked. Update can't.
Drop the lock kill, and the write operation can continue:
Send sql: kill 7
There will be no demonstration here.
The above is all the contents of the article "how to implement the statement of mysql Table Lock demonstration". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.