Home Database Mysql Tutorial MySQL中InnoDB的间隙锁问题_MySQL

MySQL中InnoDB的间隙锁问题_MySQL

Jun 01, 2016 pm 12:59 PM
innodb Lock

 在为一个客户排除死锁问题时我遇到了一个有趣的包括InnoDB间隙锁的情形。对于一个WHERE子句不匹配任何行的非插入的写操作中,我预期事务应该不会有锁,但我错了。让我们看一下这张表及示例UPDATE。
 

mysql> SHOW CREATE TABLE preferences \G
*************************** 1. row ***************************
    Table: preferences
Create Table: CREATE TABLE `preferences` (
 `numericId` int(10) unsigned NOT NULL,
 `receiveNotifications` tinyint(1) DEFAULT NULL,
 PRIMARY KEY (`numericId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
mysql> BEGIN;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT COUNT(*) FROM preferences;
+----------+
| COUNT(*) |
+----------+
|    0 |
+----------+
1 row in set (0.01 sec)
mysql> UPDATE preferences SET receiveNotifications='1' WHERE numericId = '2';
Query OK, 0 rows affected (0.01 sec)
Rows matched: 0 Changed: 0 Warnings: 0

Copy after login

InnoDB状态显示这个UPDATE在主索引记录上持有了一个X锁:

---TRANSACTION 4A18101, ACTIVE 12 sec
2 lock struct(s), heap size 376, 1 row lock(s)
MySQL thread id 3, OS thread handle 0x7ff2200cd700, query id 35 localhost msandbox
Trx read view will not see trx with id >= 4A18102, sees < 4A18102
TABLE LOCK table `test`.`preferences` trx id 4A18101 lock mode IX
RECORD LOCKS space id 31766 page no 3 n bits 72 index `PRIMARY` of table `test`.`preferences` trx id 4A18101 lock_mode X
Copy after login


这是为什么呢,Heikki在其bug报告中做了解释,这很有意义,我知道修复起来很困难,但略带厌恶地我又希望它能被差异化处理。为完成这篇文章,让我证明下上面说到的死锁情况,下面中mysql1是第一个会话,mysql2是另一个,查询的顺序如下:

mysql1> BEGIN;
Query OK, 0 rows affected (0.00 sec)
mysql1> UPDATE preferences SET receiveNotifications='1' WHERE numericId = '1';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0
mysql2> BEGIN;
Query OK, 0 rows affected (0.00 sec)
mysql2> UPDATE preferences SET receiveNotifications='1' WHERE numericId = '2';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0
mysql1> INSERT INTO preferences (numericId, receiveNotifications) VALUES ('1', '1'); -- This one goes into LOCK WAIT
mysql2> INSERT INTO preferences (numericId, receiveNotifications) VALUES ('2', '1');
ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction
Copy after login

现在你看到导致死锁是多么的容易,因此一定要避免这种情况——如果来自于事务的INSERT部分导致非插入的写操作可能不匹配任何行的话,不要这样做,使用REPLACE INTO或使用READ-COMMITTED事务隔离。

Statement of this Website
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

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 ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the difference between locked and unlocked iPhones? Detailed introduction: Comparison of the differences between locked and unlocked iPhones What is the difference between locked and unlocked iPhones? Detailed introduction: Comparison of the differences between locked and unlocked iPhones Mar 28, 2024 pm 03:10 PM

Apple mobile phones are the most widely chosen mobile phones recently, but we often see people discussing the difference between locked and unlocked Apple mobile phones online, and they are entangled in which one to buy. Today, Chen Siqi will share with you the differences between locked and unlocked iPhones and help you solve problems. In fact, there is not much difference between the two in appearance and function. The key lies in the price and use. What is a locked version and an unlocked version? An iPhone without locking restrictions means that it is not restricted by the operator, and the SIM card of any operator can be used normally. A locked version means that it has a network lock and can only use SIM cards provided by the designated operator and cannot use others. In fact, unlocked Apple phones can use mobile,

what is mysql innodb what is mysql innodb Apr 14, 2023 am 10:19 AM

InnoDB is one of the database engines of MySQL. It is now the default storage engine of MySQL and one of the standards for binary releases by MySQL AB. InnoDB adopts a dual-track authorization system, one is GPL authorization and the other is proprietary software authorization. InnoDB is the preferred engine for transactional databases and supports transaction security tables (ACID); InnoDB supports row-level locks, which can support concurrency to the greatest extent. Row-level locks are implemented by the storage engine layer.

How MySQL sees InnoDB row format from binary content How MySQL sees InnoDB row format from binary content Jun 03, 2023 am 09:55 AM

InnoDB is a storage engine that stores data in tables on disk, so our data will still exist even after shutdown and restart. The actual process of processing data occurs in memory, so the data in the disk needs to be loaded into the memory. If it is processing a write or modification request, the contents in the memory also need to be refreshed to the disk. And we know that the speed of reading and writing to disk is very slow, which is several orders of magnitude different from reading and writing in memory. So when we want to get certain records from the table, does the InnoDB storage engine need to read the records from the disk one by one? The method adopted by InnoDB is to divide the data into several pages, and use pages as the basic unit of interaction between disk and memory. The size of a page in InnoDB is generally 16

How to use Oracle to query whether a table is locked? How to use Oracle to query whether a table is locked? Mar 06, 2024 am 11:54 AM

Title: How to use Oracle to query whether a table is locked? In Oracle database, table lock means that when a transaction is performing a write operation on the table, other transactions will be blocked when they want to perform write operations on the table or make structural changes to the table (such as adding columns, deleting rows, etc.). In the actual development process, we often need to query whether the table is locked in order to better troubleshoot and deal with related problems. This article will introduce how to use Oracle statements to query whether a table is locked, and give specific code examples. To check whether the table is locked, we

Python GIL (Global Interpreter Lock): Uncovering the principles and performance impact behind it Python GIL (Global Interpreter Lock): Uncovering the principles and performance impact behind it Feb 27, 2024 am 09:00 AM

pythonGIL (Global Interpreter Lock) is an important mechanism in Python. It limits that only one thread can execute Python bytecode at the same time. This is mainly to ensure the stability of the Python interpreter, because Python's memory management and garbage collection mechanisms are single-threaded. If multiple threads are allowed to execute Python bytecode at the same time, memory corruption or other unpredictable errors may result. The principle of GIL is relatively simple. It is a lock maintained by the Python interpreter, and when a thread executes Python bytecode, it acquires the GIL. If other threads want to execute Python bytecode, they must wait for the GIL to be released. When the GIL is released, other

How is the lock in golang function implemented? How is the lock in golang function implemented? Jun 05, 2024 pm 12:39 PM

Locks in the Go language implement synchronized concurrent code to prevent data competition: Mutex: Mutex lock, which ensures that only one goroutine acquires the lock at the same time and is used for critical section control. RWMutex: Read-write lock, which allows multiple goroutines to read data at the same time, but only one goroutine can write data at the same time. It is suitable for scenarios that require frequent reading and writing of shared data.

Performance comparison of distributed locks implemented by Redis Performance comparison of distributed locks implemented by Redis Jun 20, 2023 pm 05:46 PM

As Internet applications become larger and larger, distributed systems become more and more common. In these systems, distributed locks are an essential feature. Due to the strong demand for distributed locks, there are various implementation methods. Among them, Redis is a popular tool that is widely used in distributed lock implementation. In this article, we will explore the performance comparison of distributed locks implemented by Redis. 1. Basic Concepts of Redis Before discussing the distributed lock performance of Redis, we need to understand some basic concepts of Redis.

How to handle mysql innodb exception How to handle mysql innodb exception Apr 17, 2023 pm 09:01 PM

1. Roll back and reinstall mysql. In order to avoid the trouble of importing this data from other places, first make a backup of the database file of the current library (/var/lib/mysql/location). Next, I uninstalled the Perconaserver 5.7 package, reinstalled the original 5.1.71 package, started the mysql service, and it prompted Unknown/unsupportedtabletype:innodb and could not start normally. 11050912:04:27InnoDB:Initializingbufferpool,size=384.0M11050912:04:27InnoDB:Complete

See all articles