Getting Started with MySQL from source code analysis deadlock

This article is mainly about how to debug MySQL source code, SQL will actually know what a lock to take no longer catch shrimp, or guessing He Dengcheng Great God never wrote a scene not know how to deal with the

A good number of the night by stepping hard, finally found an ideal break, you can see much of the process to obtain the lock

The code in lock0lock.cthe static enum db_err lock_rec_lock()function, the function will be displayed, the process of acquiring the lock, and when acquiring a lock success

He Dengcheng before the content for the god inside the blog ( hedengcheng.com/?p=771 ), we do experiments verified one by one (described below are experiments in RC isolation level)

Scenario 1: delete by primary key

Table Structure

CREATE TABLE `t1` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(10) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;

delete from t1 where id = 10;
复制代码

You can see, the index PRIMARY lock, mode = 1027,1027 What does it mean? 1027 = LOCK_REC_NOT_GAP + LOCK_X (non-record gap and the lock is locked X)

Follows

Conclusion: The case where the primary key to delete the data id, and no other indexes, the SQL requires an increase of only the primary key index X lock on this record id = 10 to

Scenario 2: delete through a unique index

Table structure made fine-tuning, increasing the name of a unique index

构造数据
CREATE TABLE `t2` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(10) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_name` (`name`)
) ;
INSERT INTO `t2` (`id`, `name`) VALUES 
	(1,'M'),
	(2,'Y'),
	(3,'S'),
	(4,'Q'),
	(5,'L');
	
测试sql语句
delete from t2 where name = "Y"
复制代码

Look at the actual source code debugging result of the first step:

Step two:
Conclusion: This process is the first to add a unique key uk_name X lock, then again clustered index (primary key index) plus X lock

Follows

Scene 3: Delete by ordinary index

构造数据
CREATE TABLE `t3` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(10) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`),
  KEY `idx_name` (`name`) 
);
INSERT INTO `t3` (`id`, `name`) VALUES 
	(1,'N'),
	(2,'G'),
	(3,'I'),
	(4,'N'),
	(5,'X');
	
测试语句:
delete from t3 where name = "N";
复制代码

Debugging process is shown:

Conclusion: The index is updated by ordinary, would meet all conditions 普通索引plus X locks, and have relevant 主键索引add X lock

Follows

Scenario 4: Do not take the index to delete

CREATE TABLE `t4` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(10) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
)

INSERT INTO `t4` (`id`, `name`) VALUES 
	(1,'M'),
	(2,'Y'),
	(3,'S'),
	(4,'Q'),
	(5,'L');
	
delete from t4 where name = "S";
复制代码

A total of 5 X lock, and the remaining three not one impress

Conclusion: do not take the index is updated, sql will go 聚簇索引(primary key index) for a full table scan, so each record, regardless of whether the condition will be combined with X locks. Not finished ... but for efficiency considerations, MySQL is optimized for recording conditions are not satisfied, it will lock in place after the judgment, held the final, the lock on the record satisfies the criteria, but the record does not satisfy the conditions of the locking / release operation of the lock will not be omitted.

Follows

Guess you like

Origin juejin.im/post/5ce287326fb9a07ea8039d70