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

Mysql lock (the difference between S lock and X lock)

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

Share

Shulou(Shulou.com)06/01 Report--

Shared lock and exclusive lock

Mysql's locking system: shared lock and exclusive lock (shared and exclusive locks, also known as read and write locks, namely read lock and write lock)

Read locks are shared, or do not block each other

Write locks are exclusive, and one write lock blocks other write and read locks

In the actual database system, locking occurs all the time. When a user modifies a part of data 1, mysql will use 1 lock to prevent other users from reading unified data.

Shared lock [S lock]

Also known as read lock, if transaction T adds an S lock to the data object A, transaction T can read A but cannot modify A. other transactions can only add S lock to An instead of X lock until T releases the S lock on A.

This ensures that other transactions can read A, but no modifications can be made to A until T releases the S lock on A.

Exclusive lock [X lock]

Also known as write lock. If transaction T adds an X lock to the data object A, transaction T can read An or modify A, and other transactions can no longer lock A until T releases the lock on A.

This ensures that other transactions cannot read and modify A until T releases the lock on A.

Exclusive lock usage: for update, for example, select name from table where id=1 for update

Lock granularity and lock strategy

MySQL has three levels of locks: page level, table level, and row level.

MyISAM and MEMORY storage engines use table-level locks (table-level locking); BDB storage engines use page locks (page-level locking), but also support table-level locks; InnoDB storage engines support both row-level locks (row-level locking) and table-level locks, but row-level locks are used by default.

The characteristics of the three locks of MySQL can be roughly summarized as follows:

Table-level lock: low overhead, fast locking; no deadlock; large lock granularity, the highest probability of lock conflict and the lowest concurrency. Row-level locks: expensive and slow to add locks; deadlocks occur; locking granularity is the smallest, the probability of lock conflicts is the lowest, and the degree of concurrency is the highest. Page lock: the overhead and locking time are between table lock and row lock; deadlocks occur; lock granularity is between table lock and row lock, and the concurrency is general.

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