Home >Database >Mysql Tutorial >What is mysql lock granularity?

What is mysql lock granularity?

王林
王林Original
2020-06-28 11:54:014352browse

Mysql lock granularity is what we usually call the lock level. The database engine has multi-granularity locking, allowing one transaction to lock different types of resources. MySQL database has three lock levels: page-level locks, table-level locks and row-level locks.

What is mysql lock granularity?

Lock granularity

(Recommended tutorial: mysql tutorial)

Lock granularity is what we usually call lock level.

The database engine has multi-granularity locking, allowing one transaction to lock different types of resources. To minimize locking overhead, the database engine automatically locks resources at a level appropriate for the task.

Locking at a smaller granularity (such as rows) can improve concurrency, but the overhead is higher because if many rows are locked, more locks need to be held. Locking at a larger granularity (such as a table) reduces concurrency because locking the entire table restricts other transactions from accessing any part of the table. But its overhead is lower because fewer locks need to be maintained.

Lock Hierarchy

Database engines often must acquire locks at multiple levels of granularity to fully protect resources. This set of locks at multiple levels of granularity is called a lock hierarchy. For example, to fully protect reads from an index, a Database Engine instance might have to acquire shared locks on rows and intent shared locks on pages and tables.

MySQL has three lock levels: page level, table level, row level

MyISAM and MEMORY storage engines use table-level locking (table-level locking) ;

The BDB storage engine uses page-level locking, but also supports table-level locking;

The InnoDB storage engine supports both row-level locking (row-level locking), Table-level locking is also supported, but row-level locking is used by default.

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

Table-level locks: low overhead, fast locking; no deadlocks Lock; the locking granularity is large, the probability of lock conflict is the highest, and the concurrency is the lowest.

Row-level locks: high overhead, slow locking; deadlocks may occur; the locking granularity is the smallest, the probability of lock conflicts is the lowest, and the concurrency is the highest.

Page lock: The cost and locking time are between table locks and row locks; deadlocks will occur; the locking granularity is between table locks and row locks, and the concurrency is average.

The above is the detailed content of What is mysql lock granularity?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more