Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of the usage of Select * for update in MySQ

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article shows you an example analysis of the use of Select * for update in MySQ. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Today, a little friend asked me when Mysql needs to use pessimistic locks, that is, to bring For Update when executing statements. Textbooks tell us that in some scenarios that are strict with data and have concurrency problems. So, what kind of scene is it?

Let me give you an example, we buy tickets online, from locking seats to issuing tickets is actually a "long" process, so there is such a business scenario, maybe when we lock seats, another transaction stopped the sale of the movie. Then, when the next transaction executes, some exceptions may occur.

Because when we purchase tickets, although we also need to query the sale status of the movie, because we only query the sale status of the movie, we can only get a read lock of the data in this line, unless the transaction level is serialized. otherwise, other transactions can still modify the data in this line. So, even if we are just a read operation, we still want to add a lock to this data so that other transactions can only read it and cannot modify it, which is the pessimistic lock of the database.

We can use a pose like Select * for update to use this feature. Note that we have different lock levels for this different usage.

If the primary key is explicitly specified and the primary key exists, then this is a row-level lock, and if the primary key does not exist, the lock is not added. If an index is explicitly specified, all rows that satisfy that index are locked.

If the primary key or index is not hit in the select condition, then this will be a table-level lock, that is, the data of the entire table cannot be changed, but the data can be newly inserted. Another case is that the index is not used correctly, for example, using like as a filter condition that is not equal to. It is important to note that concurrency will be greatly reduced if you are pessimistic about locking the table, so when using for update, remember to check if the index is hit.

The above is a sample analysis of the use of Select * for update in MySQ. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are 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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report