search
HomeDatabaseMysql TutorialIntroduction to innoDB lock in mysql

Introduction to innoDB lock in mysql

Oct 26, 2017 am 09:22 AM
innodbmysqlintroduce

Why do we need to start transaction before locking InnoDB

The lock in innodb is released after the transaction is submitted/rolled back. Once the transaction is submitted/rolled back, the lock in the transaction will be automatically released. Innodb defaults to autocommit=1, which turns on automatic submission.

The difference between locks using indexes and not using indexes for retrieval conditions:

When the retrieval conditions have indexes, specific rows will be locked.

If the search conditions are not used, a full table scan will be performed to lock all rows (including non-existent records)

Read lock:

Read lock is Shared, or non-blocking. Multiple users can read the same resource at the same time without interfering with each other.

Write lock:

Write lock is exclusive, which means that a write lock will block other write locks and read locks. In addition, write locks have a higher priority than read locks, so a write lock request may be inserted in front of the read lock queue, but the read lock will not be inserted in front of the write lock.

Table lock :

 InnoDB also has two table locks: intention shared lock (IS), intention exclusive lock (IX)

Row lock:

 InnoDB implements two types of row-level locks, shared locks and exclusive locks

## Optimistic lock:

Optimistic lock, also called optimistic concurrency control, assumes that multi-user concurrent transactions will not affect each other during processing, and each transaction can handle the part of its impact without generating a lock. data. Before committing data updates, each transaction will first check whether other transactions have modified the data after the transaction read the data. If there are updates from other transactions, the currently committing transaction will be rolled back.

Pessimistic lock:

Pessimistic lock, also called pessimistic concurrency control, when transaction A applies a lock to a certain row of data, and when this transaction releases the lock, other transactions can execute and The lock conflict operation, the lock imposed by transaction A here is called pessimistic lock. Shared locks and exclusive locks (row locks, gap locks, next-key locks) are both pessimistic locks

How to implement pessimistic locks and optimistic locks:

The implementation of pessimistic locks relies on the database The provided lock mechanism is implemented, such as select * from news where id=12 for update, while optimistic locking relies on recording the data version, that is, by adding the version number field to the table as a key factor in whether the submission can be successful.

Shared lock (S):

Shared lock is also called read lock. If a transaction acquires a shared lock of a data row, other transactions can acquire the shared lock. The shared lock corresponding to the row, but the exclusive lock cannot be obtained, that is, when a transaction reads a data row, other transactions can also read, but cannot add, delete or modify the data row

Setting the shared lock: SELECT .... LOCK IN SHARE MODE;

Exclusive lock (X):

Exclusive lock is also called write lock. If a transaction acquires an exclusive lock on a data row, other transactions will No other locks (exclusive locks or shared locks) can be acquired on the row. That is, when a transaction reads a data row, other transactions cannot add, delete, modify or check the data row

Setting an exclusive lock: SELECT .... FOR UPDATE

 

Note:

  • For select statements, innodb will not add any locks , that is, multiple select operations can be performed concurrently without any lock conflicts because there are no locks at all.

  • For insert, update, and delete operations, innodb will automatically add exclusive locks to the data involved. Only query select requires us to manually set exclusive locks.

Intention shared lock (IS):

Notify the database of what locks need to be applied next and lock the table. If you need to add a shared lock to record A, then innodb will first find this table, add an intention shared lock to the table, and then add a shared lock to record A. That is to say, before adding a shared lock to a data row, you must first obtain the IS lock of the table

Intention exclusive lock (IX):

Notify the database of what lock needs to be applied next and add the table Lock. If you need to add an exclusive lock to record A, then innodb will first find this table, add an intentional exclusive lock to the table, and then add a shared lock to record A. That is to say, before adding an exclusive lock to a data row, you must first obtain the IX lock of the table

The difference between shared locks and intention shared locks, exclusive locks and intention exclusive locks:

  • Shared locks and exclusive locks, the system will automatically add shared locks or exclusive locks under certain conditions, or you can manually add shared locks or exclusive locks.

  • Intention shared locks and intention exclusive locks are automatically added and automatically released by the system, and the entire process does not require manual intervention.

  • Shared locks and exclusive locks both lock row records, and intention shared locks and intention exclusive locks lock the table.

How to implement locks:

In MySQL, row-level locks do not directly lock records, but lock indexes. Indexes are divided into primary key indexes and non-primary key indexes. If a sql statement operates on the primary key index, MySQL will lock the primary key index; if a statement operates on a non-primary key index, MySQL will first lock the non-primary key index. , and then lock the relevant primary key index .

InnoDB row locks are implemented by locking index entries. If there is no index, InnoDB will lock records through a hidden clustered index. That is to say: if the data is not retrieved through index conditions, then InnoDB will lock all data in the table. The actual effect is the same as the table lock.

Row locks are divided into three situations:

Record Lock: Lock the index item, that is, lock a record.

Gap Lock: Lock the 'gap' between index items, the gap before the first record or the gap after the last record, that is, lock a range of records, excluding the record itself

Next-key Lock: Lock a range of records and include the record itself (a combination of the above two)

Note: The default level of InnoDB is repeatable-read (repeated read) level. The ANSI/IOS SQL standard defines four transaction isolation levels: read uncommitted, read committed, repeatable read, serializable, Gap Lock and The difference between Next-key Lock:

 

Next-Key Lock is a combination of row lock and gap lock. In this way, when InnoDB scans the index record, it will first add row lock to the selected index record ( Record Lock), and then add gap lock (Gap Lock) to the gaps on both sides of the index record. If a gap is locked by transaction T1, other transactions cannot insert records in this gap.

Row locks prevent other transactions from being modified or deleted, Gap locks prevent other transactions from being added, and the Next-Key lock formed by the combination of row locks and GAP locks jointly solves the problem of the RR sector. Phantom reading problem when writing data.

When to use table locks in InnoDB:

InnoDB will use row-level locks in most cases because of transaction and row locks This is often the reason why we choose InnoDB, but in some cases we also consider using table-level locks

  • When the transaction needs to update most of the data, the table is relatively large , if the default row lock is used, it is not only inefficient, but also easily causes other transactions to wait for a long time and lock conflicts.

  • #Transactions are relatively complex and may cause deadlocks and rollbacks.

  • Under InnoDB, pay attention to the following two points when using table locks.

(1) Although you can add table-level locks to InnoDB using LOCK TALBES, it must be noted that

table locks are not managed by the InnoDB storage engine layer

, but The upper layer MySQL Server is responsible for . Only when autocommit=0, innodb_table_lock=1 (default setting), the InnoDB layer can know the table lock added by MySQL, and MySQL Server can sense the row lock added by InnoDB. In this case Only under this condition can InnoDB automatically identify deadlocks involving table-level locks; otherwise, InnoDB will not be able to automatically detect and handle such deadlocks. (2) When using LOCAK TABLES to lock InnoDB, be careful to set AUTOCOMMIT to 0, otherwise MySQL will not lock the table; before the end of the transaction, do not use UNLOCAK TABLES to release the table lock, because UNLOCK TABLES will implicitly commit the transaction; COMMIT or ROLLBACK cannot release the table-level lock added with LOCAK TABLES. You must use UNLOCK TABLES to release the table lock. The correct method is as follows:

For example: If you need to write table t1 and Read from table t

SET AUTOCOMMIT=0;
LOCAK TABLES t1 WRITE, t2 READ, ...;[do something with tables t1 and here];COMMIT;
UNLOCK TABLES;

Deadlock:

We said that deadlock will not occur in MyISAM, because MyISAM is always once Sex gets all the locks it needs, either all satisfied or all waiting. In InnoDB, locks are acquired gradually, causing the possibility of deadlock.

After a deadlock occurs, InnoDB can generally detect it and make one transaction release the lock and roll back, and another acquire the lock to complete the transaction. However, InnoDB cannot completely automatically detect deadlocks when external locks or locks are involved. This needs to be solved by setting the lock wait timeout parameter innodb_lock_wait_timeout. It should be noted that this parameter is not only used to solve the deadlock problem. When concurrent access is relatively high, if a large number of transactions are suspended because they cannot obtain the required locks immediately, it will occupy a large amount of computer resources and cause serious performance problems. problems and even bring down the database. We can avoid this situation by setting an appropriate lock wait timeout threshold.

There are many ways to avoid deadlocks. Here are three common ones:

  1. If different programs will access multiple tables concurrently, try to agree to access the tables in the same order. , which can greatly reduce the chance of deadlock. If two sessions access the two tables in a different order, the chance of deadlock is very high! But if accesses are performed in the same order, deadlock may be avoided.

  2. In the same transaction, try to lock all the resources required at once to reduce the probability of deadlock.

  3. For business parts that are very prone to deadlocks, you can try to upgrade the locking granularity and use table-level locking to reduce the possibility of deadlocks.

  4. When the program processes data in batches, if the data is sorted in advance to ensure that each thread processes records in a fixed order, the possibility of deadlock can also be greatly reduced.

  5. Under the REPEATEABLE-READ isolation level, if two threads use SELECT...ROR UPDATE on the same condition record at the same time to add an exclusive lock, if there is no record that matches the condition , both threads will lock successfully. The program discovers that the record does not yet exist and attempts to insert a new record. If both threads do this, a deadlock will occur. In this case, changing the isolation level to READ COMMITTED can avoid the problem.

  6. When the isolation level is READ COMMITED, if both threads execute SELECT...FOR UPDATE first, determine whether there are records that meet the conditions, and if not, insert the records. At this time, only one thread can insert successfully, and the other thread will wait for a lock. When the first thread submits, the second thread will make an error due to the primary key again, but although this thread makes an error, it will obtain an exclusive lock! At this time, if a third thread applies for an exclusive lock, a deadlock will also occur. In this case, you can directly perform the insertion operation and then catch the primary key duplicate exception, or when encountering the primary key duplicate error, always execute ROLLBACK to release the acquired exclusive lock

## 

ps: If a deadlock occurs, you can use the SHOW INNODB STATUS command to determine the cause of the last deadlock and improvement measures.

Summary:

For the

InnoDB table, there are mainly the following points

(1) InnoDB marketing is based on indexes. If data is accessed without an index, InnoDB uses table locks.

(2) InnoDB gap lock mechanism, and the reasons why InnoDB uses gap locks.

(3) Under different isolation levels, InnoDB’s locking mechanism and consistent read strategy are different.

(4) MySQL recovery and replication also have a great impact on the InnoDB lock mechanism and consistent read strategy.

(5) Lock conflicts and even deadlocks are difficult to completely avoid.

After understanding the lock characteristics of InnoDB, users can reduce lock conflicts and deadlocks through design and SQL adjustments, including:

  • Try to use lower isolation Level

  • Carefully design the index, and try to use the index to access data to make locking more accurate, thereby reducing the chance of lock conflicts.

  • Choose a reasonable transaction size, and the probability of lock conflicts for small transactions is smaller.

  • When display locking the record set, it is best to request a sufficient level of lock at one time. For example, if you want to modify data, it is best to apply for an exclusive lock directly instead of applying for a shared lock first and then request an exclusive lock when modifying, which can easily lead to deadlock.

  • When different programs access a group of tables, they should try to agree to access each table in the same order. For a table, try to access the rows in the table in a fixed order. . This can greatly reduce the chance of deadlock.

  • Try to use equal conditions to access data, so as to avoid the impact of gap locks on concurrent insertion.

  • Do not apply for a lock level that exceeds the actual need; unless necessary, do not display locking when querying.

  • For some specific transactions, table locks can be used to increase processing speed or reduce the possibility of deadlock.

The above is the detailed content of Introduction to innoDB lock in mysql. 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
MySQL's Place: Databases and ProgrammingMySQL's Place: Databases and ProgrammingApr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL: From Small Businesses to Large EnterprisesMySQL: From Small Businesses to Large EnterprisesApr 13, 2025 am 12:17 AM

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?Apr 13, 2025 am 12:16 AM

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.

MySQL: Not a Programming Language, But...MySQL: Not a Programming Language, But...Apr 13, 2025 am 12:03 AM

MySQL is not a programming language, but its query language SQL has the characteristics of a programming language: 1. SQL supports conditional judgment, loops and variable operations; 2. Through stored procedures, triggers and functions, users can perform complex logical operations in the database.

MySQL: An Introduction to the World's Most Popular DatabaseMySQL: An Introduction to the World's Most Popular DatabaseApr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

The Importance of MySQL: Data Storage and ManagementThe Importance of MySQL: Data Storage and ManagementApr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system suitable for data storage, management, query and security. 1. It supports a variety of operating systems and is widely used in Web applications and other fields. 2. Through the client-server architecture and different storage engines, MySQL processes data efficiently. 3. Basic usage includes creating databases and tables, inserting, querying and updating data. 4. Advanced usage involves complex queries and stored procedures. 5. Common errors can be debugged through the EXPLAIN statement. 6. Performance optimization includes the rational use of indexes and optimized query statements.

Why Use MySQL? Benefits and AdvantagesWhy Use MySQL? Benefits and AdvantagesApr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Describe InnoDB locking mechanisms (shared locks, exclusive locks, intention locks, record locks, gap locks, next-key locks).Describe InnoDB locking mechanisms (shared locks, exclusive locks, intention locks, record locks, gap locks, next-key locks).Apr 12, 2025 am 12:16 AM

InnoDB's lock mechanisms include shared locks, exclusive locks, intention locks, record locks, gap locks and next key locks. 1. Shared lock allows transactions to read data without preventing other transactions from reading. 2. Exclusive lock prevents other transactions from reading and modifying data. 3. Intention lock optimizes lock efficiency. 4. Record lock lock index record. 5. Gap lock locks index recording gap. 6. The next key lock is a combination of record lock and gap lock to ensure data consistency.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools