首页 > 数据库 >MYSQL死锁分析案例二(高并发增删改同一条记录)

MYSQL死锁分析案例二(高并发增删改同一条记录)

时间:2024-08-07 11:21:27浏览次数:15  
标签:locked 改同 age t1 trx 死锁 MYSQL NULL blocking

1、建表

CREATE TABLE `t1` (
  `id` int NOT NULL,
  `name` varchar(200) DEFAULT NULL,
  `age` int DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx111` (`name`),
  KEY `idx_age` (`age`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

2、数据

mysql> select * from t1;
+----+------+------+
| id | name | age  |
+----+------+------+
|  1 | a    |  100 |
|  2 | b    |  200 |
|  3 | c    |  300 |
+----+------+------+
3 rows in set (0.00 sec)

3、复现

  session1 session2 session3 session4

时间1

begin;

insert into t1 select 4,'e',5;

 

 

 

 
时间2  

begin;

update t1 set age=1000 where name='e';  --锁等待状态

   
时间3    

begin;

delete from t1 where age=5;   --锁等待状态

 
时间4       查询锁等待信息
时间5 commit; ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction Query OK, 1 row affected (6.21 sec)  

 

4、在时间4查询锁等待信息

mysql> select *from data_lock_waits;
+--------+-------------------------------+----------------------------------+----------------------+---------------------+----------------------------------+-------------------------------+--------------------------------+--------------------+-------------------+--------------------------------+
| ENGINE | REQUESTING_ENGINE_LOCK_ID     | REQUESTING_ENGINE_TRANSACTION_ID | REQUESTING_THREAD_ID | REQUESTING_EVENT_ID | REQUESTING_OBJECT_INSTANCE_BEGIN | BLOCKING_ENGINE_LOCK_ID       | BLOCKING_ENGINE_TRANSACTION_ID | BLOCKING_THREAD_ID | BLOCKING_EVENT_ID | BLOCKING_OBJECT_INSTANCE_BEGIN |
+--------+-------------------------------+----------------------------------+----------------------+---------------------+----------------------------------+-------------------------------+--------------------------------+--------------------+-------------------+--------------------------------+
| INNODB | 5795977440:419:6:4:5671150960 |                           139150 |                   62 |                  66 |                       5671150960 | 5795975856:419:6:4:5671141744 |                         139144 |                 62 |                65 |                     5671141744 |
| INNODB | 5795976648:419:5:5:5671146352 |                           139149 |                   61 |                  64 |                       5671146352 | 5795975856:419:5:5:5671141400 |                         139144 |                 61 |                63 |                     5671141400 |
+--------+-------------------------------+----------------------------------+----------------------+---------------------+----------------------------------+-------------------------------+--------------------------------+--------------------+-------------------+--------------------------------+
2 rows in set (0.00 sec)

mysql>
mysql>
mysql>
mysql> select * from performance_schema.data_locks;
+--------+-------------------------------+-----------------------+-----------+----------+---------------+-------------+----------------+-------------------+------------+-----------------------+-----------+---------------+-------------+-----------+
| ENGINE | ENGINE_LOCK_ID                | ENGINE_TRANSACTION_ID | THREAD_ID | EVENT_ID | OBJECT_SCHEMA | OBJECT_NAME | PARTITION_NAME | SUBPARTITION_NAME | INDEX_NAME | OBJECT_INSTANCE_BEGIN | LOCK_TYPE | LOCK_MODE     | LOCK_STATUS | LOCK_DATA |
+--------+-------------------------------+-----------------------+-----------+----------+---------------+-------------+----------------+-------------------+------------+-----------------------+-----------+---------------+-------------+-----------+
| INNODB | 5795977440:1590:5668664920    |                139150 |        62 |       65 | ceshi         | t1          | NULL           | NULL              | NULL       |            5668664920 | TABLE     | IX            | GRANTED     | NULL      |
| INNODB | 5795977440:419:6:4:5671150960 |                139150 |        62 |       66 | ceshi         | t1          | NULL           | NULL              | idx_age    |            5671150960 | RECORD    | X,REC_NOT_GAP | WAITING     | 5, 4      |
| INNODB | 5795976648:1590:5668663896    |                139149 |        61 |       63 | ceshi         | t1          | NULL           | NULL              | NULL       |            5668663896 | TABLE     | IX            | GRANTED     | NULL      |
| INNODB | 5795976648:419:5:5:5671146352 |                139149 |        61 |       64 | ceshi         | t1          | NULL           | NULL              | idx111     |            5671146352 | RECORD    | X,REC_NOT_GAP | WAITING     | 'e', 4    |
| INNODB | 5795975856:1590:5668662872    |                139144 |        60 |       94 | ceshi         | t1          | NULL           | NULL              | NULL       |            5668662872 | TABLE     | IX            | GRANTED     | NULL      |
| INNODB | 5795975856:419:5:5:5671141400 |                139144 |        61 |       63 | ceshi         | t1          | NULL           | NULL              | idx111     |            5671141400 | RECORD    | X,REC_NOT_GAP | GRANTED     | 'e', 4    |
| INNODB | 5795975856:419:6:4:5671141744 |                139144 |        62 |       65 | ceshi         | t1          | NULL           | NULL              | idx_age    |            5671141744 | RECORD    | X,REC_NOT_GAP | GRANTED     | 5, 4      |
+--------+-------------------------------+-----------------------+-----------+----------+---------------+-------------+----------------+-------------------+------------+-----------------------+-----------+---------------+-------------+-----------+
7 rows in set (0.00 sec)

mysql> select * from information_schema.innodb_trx;
+--------+-----------+---------------------+-------------------------------+---------------------+------------+---------------------+---------------------------------------+---------------------+-------------------+-------------------+------------------+-----------------------+-----------------+-------------------+-------------------------+---------------------+-------------------+------------------------+----------------------------+---------------------------+---------------------------+------------------+----------------------------+---------------------+
| trx_id | trx_state | trx_started         | trx_requested_lock_id         | trx_wait_started    | trx_weight | trx_mysql_thread_id | trx_query                             | trx_operation_state | trx_tables_in_use | trx_tables_locked | trx_lock_structs | trx_lock_memory_bytes | trx_rows_locked | trx_rows_modified | trx_concurrency_tickets | trx_isolation_level | trx_unique_checks | trx_foreign_key_checks | trx_last_foreign_key_error | trx_adaptive_hash_latched | trx_adaptive_hash_timeout | trx_is_read_only | trx_autocommit_non_locking | trx_schedule_weight |
+--------+-----------+---------------------+-------------------------------+---------------------+------------+---------------------+---------------------------------------+---------------------+-------------------+-------------------+------------------+-----------------------+-----------------+-------------------+-------------------------+---------------------+-------------------+------------------------+----------------------------+---------------------------+---------------------------+------------------+----------------------------+---------------------+
| 139150 | LOCK WAIT | 2024-08-06 17:42:03 | 5795977440:419:6:4:5671150960 | 2024-08-06 17:43:24 |          2 |                  26 | delete from t1 where age=5            | starting index read |                 1 |                 1 |                2 |                  1128 |               2 |                 0 |                       0 | READ COMMITTED      |                 1 |                      1 | NULL                       |                         0 |                         0 |                0 |                          0 |                   1 |
| 139149 | LOCK WAIT | 2024-08-06 17:41:52 | 5795976648:419:5:5:5671146352 | 2024-08-06 17:43:23 |          2 |                  25 | update t1 set age=1000 where name='e' | starting index read |                 1 |                 1 |                2 |                  1128 |               2 |                 0 |                       0 | READ COMMITTED      |                 1 |                      1 | NULL                       |                         0 |                         0 |                0 |                          0 |                   1 |
| 139144 | RUNNING   | 2024-08-06 17:41:42 | NULL                          | NULL                |          4 |                  24 | NULL                                  | NULL                |                 0 |                 1 |                3 |                  1128 |               2 |                 1 |                       0 | READ COMMITTED      |                 1 |                      1 | NULL                       |                         0 |                         0 |                0 |                          0 |                NULL |
+--------+-----------+---------------------+-------------------------------+---------------------+------------+---------------------+---------------------------------------+---------------------+-------------------+-------------------+------------------+-----------------------+-----------------+-------------------+-------------------------+---------------------+-------------------+------------------------+----------------------------+---------------------------+---------------------------+------------------+----------------------------+---------------------+
3 rows in set (0.00 sec)


mysql> select* from x$innodb_lock_waits\G
*************************** 1. row ***************************
wait_started: 2024-08-06 17:45:07
wait_age: 00:00:16
wait_age_secs: 16
locked_table: `ceshi`.`t1`
locked_table_schema: ceshi
locked_table_name: t1
locked_table_partition: NULL
locked_table_subpartition: NULL
locked_index: idx111
locked_type: RECORD
waiting_trx_id: 139149
waiting_trx_started: 2024-08-06 17:41:52
waiting_trx_age: 00:03:31
waiting_trx_rows_locked: 3
waiting_trx_rows_modified: 0
waiting_pid: 25
waiting_query: update t1 set age=1000 where name='e'
waiting_lock_id: 5795976648:419:5:5:5671146696
waiting_lock_mode: X,REC_NOT_GAP
blocking_trx_id: 139144
blocking_pid: 24
blocking_query: NULL
blocking_lock_id: 5795975856:419:5:5:5671141400
blocking_lock_mode: X,REC_NOT_GAP
blocking_trx_started: 2024-08-06 17:41:42
blocking_trx_age: 00:03:41
blocking_trx_rows_locked: 2
blocking_trx_rows_modified: 1
sql_kill_blocking_query: KILL QUERY 24
sql_kill_blocking_connection: KILL 24
*************************** 2. row ***************************
wait_started: 2024-08-06 17:45:08
wait_age: 00:00:15
wait_age_secs: 15
locked_table: `ceshi`.`t1`
locked_table_schema: ceshi
locked_table_name: t1
locked_table_partition: NULL
locked_table_subpartition: NULL
locked_index: idx_age
locked_type: RECORD
waiting_trx_id: 139150
waiting_trx_started: 2024-08-06 17:42:03
waiting_trx_age: 00:03:20
waiting_trx_rows_locked: 3
waiting_trx_rows_modified: 0
waiting_pid: 26
waiting_query: delete from t1 where age=5
waiting_lock_id: 5795977440:419:6:4:5671151304
waiting_lock_mode: X,REC_NOT_GAP
blocking_trx_id: 139144
blocking_pid: 24
blocking_query: NULL
blocking_lock_id: 5795975856:419:6:4:5671141744
blocking_lock_mode: X,REC_NOT_GAP
blocking_trx_started: 2024-08-06 17:41:42
blocking_trx_age: 00:03:41
blocking_trx_rows_locked: 2
blocking_trx_rows_modified: 1
sql_kill_blocking_query: KILL QUERY 24
sql_kill_blocking_connection: KILL 24
2 rows in set (0.00 sec)

mysql>

 

标签:locked,改同,age,t1,trx,死锁,MYSQL,NULL,blocking
From: https://www.cnblogs.com/nanxiang/p/18346665

相关文章

  • Windows环境下MySql8.0安装手册
    一、下载安装包登录官网https://www.mysql.com/downloads/点击MySql社区(GPL)下载  点击MySQLInstallerforWinsows  选择离线安装程序点击下载  点击开始下载   二、安装Mysql双击安装包  选择自定义安装  选择产品  一路下一步......
  • 乐观锁与悲观锁在MySQL中的应用
    本文由ChatMoney团队出品在数据库管理系统中,锁机制是保证数据一致性和并发控制的重要手段。MySQL,作为广泛使用的数据库系统之一,提供了多种锁策略来处理并发访问时可能引发的数据不一致性问题。其中,乐观锁和悲观锁是两种截然不同但又互补的并发控制策略,它们在不同的应用场景下......
  • 计算机毕业设计必看必学! ! 79197 基于ssm+mysql的学生心理健康在线咨询平台,原创定制
    摘要:在社会快速发展的影响下,教育业继续发展,大大增加了学生心理健康在线咨询平台的数量、多样性、质量等等的要求,使学生心理健康在线咨询平台的管理和运营比过去十年更加困难。依照这一现实为基础,设计一个快捷而又方便的学生心理健康在线咨询平台是一项十分重要并且有价值的事......
  • MySQL UDF 提权初探
    MySQLUDF提权初探对MySQLUDF提权做一次探究,什么情况下可以提权,提取的主机权限是否跟mysqld进程启动的主机账号有关数据库信息MySQL数据库版本:5.7.21UDFUDF:(UserDefinedFunction)用户自定义函数,MySQL数据库的初衷是用于方便用户进行自定义函数,方便查询一些复杂的数据......
  • pthread_once导致死锁
    在一个pthread_once方法内又再次调用了这个pthread_once导致死锁。分析下这个pthread_once的源码:可以看到这个pthread_once_t结构体就是一个整形数字加自旋锁。int___pthread_once(pthread_once_t*once_control,void(*init_routine)(void)){/*Fastpath.See__......
  • MySQL安装
    目录前言MySQL5.7的安裝一、在线安装1.下载yumRepository2.安装yumRepository3.安装mysql5.7的服务4.开机自启动5.启动mysql6.查看状态7.获取临时密码8.登录mysql9.关闭密码复杂验证10.设置密码11.修改权限二、离线安装1、卸载已有的MySQL文件2、安装mysql......
  • 最全MySQL面试20题和答案(一)
    数据库基础知识为什么要使用数据库?数据保存在内存优点:存取速度快缺点:数据不能永久保存数据保存在文件优点:数据永久保存缺点:速度比内存操作慢,频繁的IO操作。查询数据不方便数据保存在数据库数据永久保存使用SQL语句,查询方便效率高。管理数据方便什......
  • 【MySQL】索引和事务
    秋招秋招中最经典,最高频的面试题文章目录索引index操作索引的SQL查看索引创建索引删除索引事务操作四个核心特性索引index在数据库中建立一个特殊的“目录“(一系列特定的数据结构),为了加快查询速度select查询都是遍历查询,比较复杂O(N)级别复杂度嵌......
  • 【多线程-从零开始-肆】线程安全、加锁和死锁
    进程状态进程状态:就绪:正在CPU上执行,或者随时可以去CPU上执行阻塞:暂时不能参与CPU的执行Java的线程,对应状态做了更详细的区分,不仅仅是就绪和阻塞了六种状态:NEW当前Thread对象虽然有了,但是内核的线程还没有(还没调用过start)TERMINATE当前Thread对......
  • Mysql事务四大特性的介绍
    1数据库事务        数据库的事务(Transaction)是一种机制、一个操作序列,包含了一组数据库操作命令。事务把所有的命令作为一个整体一起向系统提交或撤销操作请求,即这一组数据库命令要么都执行,要么都不执行,因此事务是一个不可分割的工作逻辑单元。在数据库系统上执行并......