MySQL 待解决死锁
官方文档:https://dev.mysql.com/doc/refman/5.6/en/innodb-locks-set.html
线上出现一个死锁现象,信息显示的是两条对同一个表的不同记录的update操作,表上只有一个主键索引,更新的条件上无索引,时间地段显示两个update只相差1ms
业务场景是同时一个事务中先是insert 再update新插入的行,存在并发;数据库环境是5.6,事务隔离级别RC,auto_increment_increment=1
通过定时任务实现两个会话同时对一个表先进行insert,然后update
#表结构
mysql> show create table test.t3;
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t3 | CREATE TABLE `t3` (
`id` int() NOT NULL AUTO_INCREMENT,
`name` varchar() DEFAULT NULL,
`col` varchar() DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT= DEFAULT CHARSET=utf8 |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
row in set (0.00 sec)
#定时任务
* * * for i in `seq `;do /usr/local/mysql/bin/mysql -uroot -S /tmp/mysql_3309.sock -e "begin;insert into test.t3 values(null,'aa','$i');update test.t3 set name='aa$i' where col='$i';commit;";done
* * * for i in `seq `;do /usr/local/mysql/bin/mysql -uroot -S /tmp/mysql_3309.sock -e "begin;insert into test.t3 values(null,'aa','$i');update test.t3 set name='aa$i' where col='$i';commit;";done
捕获到的死锁信息
------------------------
LATEST DETECTED DEADLOCK
------------------------
-- :: 0x7f5277ba6700
*** () TRANSACTION:
TRANSACTION , ACTIVE sec fetching rows
mysql tables in use , locked
LOCK WAIT lock struct(s), heap size , row lock(s), undo log entries
MySQL thread id , OS thread handle , query id localhost root updating
update test.t3 set name='aa26' where col=''
*** () WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id page no n bits index PRIMARY of table `test`.`t3` trx id lock_mode X locks rec but not gap waiting
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex 8000004c; asc L;;
: len ; hex 0000000aa0be; asc ;;
: len ; hex b6000001910110; asc ;;
: len ; hex ; asc aa;;
: len ; hex ; asc ;; *** () TRANSACTION:
TRANSACTION , ACTIVE sec fetching rows
mysql tables in use , locked
lock struct(s), heap size , row lock(s), undo log entries
MySQL thread id , OS thread handle , query id localhost root updating
update test.t3 set name='aa126' where col=''
*** () HOLDS THE LOCK(S):
RECORD LOCKS space id page no n bits index PRIMARY of table `test`.`t3` trx id lock_mode X locks rec but not gap
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex 8000004c; asc L;;
: len ; hex 0000000aa0be; asc ;;
: len ; hex b6000001910110; asc ;;
: len ; hex ; asc aa;;
: len ; hex ; asc ;; *** () WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id page no n bits index PRIMARY of table `test`.`t3` trx id lock_mode X locks rec but not gap waiting
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex 8000004b; asc K;;
: len ; hex 0000000aa0bd; asc ;;
: len ; hex 3500000142133d; asc B =;;
: len ; hex ; asc aa26;;
: len ; hex ; asc ;; *** WE ROLL BACK TRANSACTION ()
------------
解决
mysql> explain update test.t3 set name='aa126' where col=''
-> ;
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| 1 | UPDATE | t3 | NULL | index | NULL | PRIMARY | 4 | NULL | 382 | 100.00 | Using where |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
1 row in set (0.00 sec)
更新操作会对主键索引进行全索引扫描,我的理解为会一行行的处理先在innodb层在主键上加X锁,然后再server层通过where条件进行过滤,释放不符合条件的记录上的锁
在where条件字段上加索引,避免全索引扫描
测试发现通过在where条件上添加索引可以解决问题,但是还是无法解释这一现象,因为单独将事务拿出来重现是不会产生阻塞的,只有高并发下才会产生。。。。有知道的朋友请留言。
MySQL 待解决死锁的更多相关文章
- 巧用MySQL InnoDB引擎锁机制解决死锁问题(转)
该文会通过一个实际例子中的死锁问题的解决过程,进一步解释innodb的行锁机制 最近,在项目开发过程中,碰到了数据库死锁问题,在解决问题的过程中,笔者对MySQL InnoDB引擎锁机制的理解逐步加深 ...
- MySQL锁解决并发问题详解
文章分为以下几个要点 问题描述以及解决过程 MySQL锁机制 数据库加锁分析 下面讨论的都是基于MySQL的InnoDB. 0. 问题描述以及解决过程 因为涉及到公司利益问题,所以下面很多代码和数据库 ...
- MySql一个生产死锁案例分析
接到上级一个生产环境MySQL死锁日志信息文件,需要找出原因并解决问题.我将死锁日志部分贴出如下: 在mysql中使用命令:SHOW ENGINE INNODB STATUS;总能获取到最近一些问题信 ...
- 解决死锁之路3 - 常见 SQL 语句的加锁分析 (转)
出处:https://www.aneasystone.com/archives/2017/12/solving-dead-locks-three.html 这篇博客将对一些常见的 SQL 语句进行加锁 ...
- SQL Server中解决死锁
SQL Server中解决死锁的新方法介绍 数据库操作的死锁是不可避免的,本文并不打算讨论死锁如何产生,重点在于解决死锁,通过SQL Server 2005, 现在似乎有了一种新的解决办法. 将下面的 ...
- CentOS下php使用127.0.0.1不能连接mysql的解决方法
这篇文章主要介绍了CentOS下php使用127.0.0.1不能连接mysql的解决方法,本文原因是SELINUX导致的连接失败,需要的朋友可以参考下 php代码很简单: 复制代码代码如下: $ser ...
- vs连接mysql出错解决方法
vs连接mysql出错解决方法 先按以下的步骤配置一下: **- (1)打开VC6.0 工具栏Tools菜单下的Options选项.在Directories的标签页中右边的"Show dir ...
- SQLServer查看和解决死锁的方法
http://luohonghong.blog.163.com/blog/static/78312058201142411533316/ SQLServer查看和解决死锁的方法 2011-05-24 ...
- paip.将数据导入到在英语语音数据库mysql道路解决空原则问题
paip.将数据导入到在英语语音数据库mysql道路解决空原则问题 #---原因:mysql 导入工具bug #---解决:不要使用双引号括注音. 笔者 老哇爪 Attilax 艾龙. EMAIL: ...
随机推荐
- MySql中查询语句实现分页功能
import java.util.*;import java.sql.*; public class FruitDao { private Connection conn; private ...
- Vue.js - day7
使用mui的tab-top-webview-main完成分类滑动栏 兼容问题 和 App.vue 中的 router-link 身上的类名 mui-tab-item 存在兼容性问题,导致tab栏失效, ...
- GoAccess参数选项
GoAccess - 1.2 Usage: goaccess [filename] [ options ... ] [-c][-M][-H][-q][-d][...]The following opt ...
- HDOJ1195 双向BFS //单向也可以过 没想清
#include<cstdio> #include<map> #include<vector> #include<stack> #include< ...
- jni log 使用
1. 在源文件中添加头文件 #include <android/log.h> #define LOG_TAG "System.out.c" #define LOGD(. ...
- MINST手写数字识别(三)—— 使用antirectifier替换ReLU激活函数
这是一个来自官网的示例:https://github.com/keras-team/keras/blob/master/examples/antirectifier.py 与之前的MINST手写数字识 ...
- 爬虫4_python2
import urllib2 response = urllib2.urlopen("https://www.baidu.com") print response.read() 构 ...
- Swift 编程思想 Part 4:map all the things!
Swift 编程思想 Part 4:map all the things! 2015-10-22 837 文章目录 1. 数组 vs. 可选类型 2. 作用在可选类型上的 map() 3. 回到我们 ...
- centOS下jenkins
转:centos7搭建jenkins小记 转自:https://segmentfault.com/a/1190000007086764 安装java环境 1.查看服务器版本 centos7,继续. c ...
- RN服务
https://facebook.github.io/react-native/docs/headless-js-android.html 当app在 后台运行 时,我们可以使用RN服务来同时地刷新数 ...