当前位置:网站首页>How to check whether a table is locked in mysql
How to check whether a table is locked in mysql
2022-08-02 03:34:00 【asdfadafd】
How to see if a deadlock occurs
在使用mysql的时候,How to check whether the table is locked?
查看表被锁状态和结束死锁步骤:
1.在mysql命令行执行sql语句
use dbName; // Switch to a specific database
show engine innodb status; // 查询db是否发生死锁
2.View the data table is locked status
show OPEN TABLES where In_use > 0;
This statement can query the status of the current lock table
3.分析锁表的SQL
通过sql日志,分析相应SQL,给表加索引,常用字段加索引,Table associated fields plus indexes, etc. are pairedsql进行优化.
4.查看正在锁的事务
SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCKS;
在5.5中,information_schema 库中增加了三个关于锁的表(innoDB引擎):
innodb_trx ## 当前运行的所有事务
innodb_locks ## 当前出现的锁
innodb_lock_waits ## 锁等待的对应关系
先来看一下这三张表结构:
[email protected] : information_schema 13:28:38> desc innodb_locks;
+————-+———————+——+—–+———+——-+
| Field | Type | Null | Key | Default | Extra |
+————-+———————+——+—–+———+——-+
| lock_id | varchar(81) | NO | | | |#锁ID
| lock_trx_id | varchar(18) | NO | | | |#拥有锁的事务ID
| lock_mode | varchar(32) | NO | | | |#锁模式
| lock_type | varchar(32) | NO | | | |#锁类型
| lock_table | varchar(1024) | NO | | | |#被锁的表
| lock_index | varchar(1024) | YES | | NULL | |#被锁的索引
| lock_space | bigint(21) unsigned | YES | | NULL | |#被锁的表空间号
| lock_page | bigint(21) unsigned | YES | | NULL | |#被锁的页号
| lock_rec | bigint(21) unsigned | YES | | NULL | |#被锁的记录号
| lock_data | varchar(8192) | YES | | NULL | |#被锁的数据
+————-+———————+——+—–+———+——-+
10 rows in set (0.00 sec)
[email protected] : information_schema 13:28:56> desc innodb_lock_waits;
+——————-+————-+——+—–+———+——-+
| Field | Type | Null | Key | Default | Extra |
+——————-+————-+——+—–+———+——-+
| requesting_trx_id | varchar(18) | NO | | | |#请求锁的事务ID
| requested_lock_id | varchar(81) | NO | | | |#请求锁的锁ID
| blocking_trx_id | varchar(18) | NO | | | |#当前拥有锁的事务ID
| blocking_lock_id | varchar(81) | NO | | | |#当前拥有锁的锁ID
+——————-+————-+——+—–+———+——-+
4 rows in set (0.00 sec)
[email protected] : information_schema 13:29:05> desc innodb_trx ;
+—————————-+———————+——+—–+———————+——-+
| Field | Type | Null | Key | Default | Extra |
+—————————-+———————+——+—–+———————+——-+
| trx_id | varchar(18) | NO | | | |#事务ID
| trx_state | varchar(13) | NO | | | |#事务状态:
| trx_started | datetime | NO | | 0000-00-00 00:00:00 | |#事务开始时间;
| trx_requested_lock_id | varchar(81) | YES | | NULL | |#innodb_locks.lock_id
| trx_wait_started | datetime | YES | | NULL | |#事务开始等待的时间
| trx_weight | bigint(21) unsigned | NO | | 0 | |#
| trx_mysql_thread_id | bigint(21) unsigned | NO | | 0 | |#事务线程ID
| trx_query | varchar(1024) | YES | | NULL | |#具体SQL语句
| trx_operation_state | varchar(64) | YES | | NULL | |#事务当前操作状态
| trx_tables_in_use | bigint(21) unsigned | NO | | 0 | |#事务中有多少个表被使用
| trx_tables_locked | bigint(21) unsigned | NO | | 0 | |#事务拥有多少个锁
| trx_lock_structs | bigint(21) unsigned | NO | | 0 | |#
| trx_lock_memory_bytes | bigint(21) unsigned | NO | | 0 | |#事务锁住的内存大小(B)
| trx_rows_locked | bigint(21) unsigned | NO | | 0 | |#事务锁住的行数
| trx_rows_modified | bigint(21) unsigned | NO | | 0 | |#事务更改的行数
| trx_concurrency_tickets | bigint(21) unsigned | NO | | 0 | |#事务并发票数
| trx_isolation_level | varchar(16) | NO | | | |#事务隔离级别
| trx_unique_checks | int(1) | NO | | 0 | |#是否唯一性检查
| trx_foreign_key_checks | int(1) | NO | | 0 | |#是否外键检查
| trx_last_foreign_key_error | varchar(256) | YES | | NULL | |#最后的外键错误
| trx_adaptive_hash_latched | int(1) | NO | | 0 | |#
| trx_adaptive_hash_timeout | bigint(21) unsigned | NO | | 0 | |#
+—————————-+———————+——+—–+———————+——-+
22 rows in set (0.01 sec)
5.查看等待锁的事务
SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCK_WAITS.
6.View the type and status of the lock:
show status like '%lock%';
7.查询进程
show processlist
8.Kill the deadlocked process
如果发生死锁,Through this statement, you can query the process for which the table is locked,然后通过kill命令杀掉该进程.
Others look at table locks:
# 查看表锁的情况:
mysql> show status like 'table%';
+----------------------------+---------+
| Variable_name | Value |
+----------------------------+---------+
| Table_locks_immediate | 100 |
| Table_locks_waited | 11 |
+----------------------------+---------+
# 查看InnoDB_row_lock状态变量来分析系统上的行锁的争夺情况:
mysql> show status like 'InnoDB_row_lock%';
+-------------------------------+--------+
| Variable_name | Value |
+-------------------------------+--------+
| Innodb_row_lock_current_waits | 0 |
| Innodb_row_lock_time | 159372 |
| Innodb_row_lock_time_avg | 39843 |
| Innodb_row_lock_time_max | 51154 |
| Innodb_row_lock_waits | 4 |
+-------------------------------+--------+
5 rows in set (0.01 sec)
mysql>
在分析innodb中锁阻塞时,几种方法的对比情况:
(1)使用show processlist查看不靠谱;
(2)直接使用show engine innodb status查看,无法判断到问题的根因;
(3)使用mysqladmin debug查看,能看到所有产生锁的线程,但无法判断哪个才是根因;
(4)开启innodb_lock_monitor后,再使用show engine innodb status查看,能够找到锁阻塞的根因.
Deadlock occurrence and cause
产生原因
所谓死锁:是指两个或两个以上的进程在执行过程中,因争夺资源而造成的一种互相等待的现象.若无外力作用,它们都将无法推进下去.此时称系统处于死锁状态或系统产生了死锁,这些永远在互相等待的进程称为死锁进程.表级锁不会产生死锁.所以解决死锁主要还是针对于最常用的InnoDB.
死锁的关键在于:两个(或以上)的Session加锁的顺序不一致.
那么对应的解决死锁问题的关键就是:让不同的session加锁有次序
Several situations in which deadlock occurs
There are several cases of deadlocks
We have two tables with the same structure,分别为t1和t2:
id:integer
token:varchar
message:varchar
其中id是主键(自增),token是非聚集索引,message没有索引.
1、A table with two rows of records crosses to apply for a mutual exclusion lock
A执行到第二步时,等待BRelease the lock on the first step,而BThe lock needs to be released after the second step is completed to end the transaction;
B执行到第二步时,等待ARelease the speed of the first step,这样A和B都无法进行下去,A deadlock phenomenon occurs.
2、Two tables and two rows of records intersect to apply for a mutual exclusion lock
这种情况与1中的类似.
3、A clustered index conflicts with a nonclustered index
This does not necessarily result in a deadlock,It doesn't even appear on the surface.
假设AThe locking sequence of records that meet the conditions in is (5,4,3,2,1),BThe order of locking in is (1,2,3,4,5),The ordering here is correspondencerecord的主键;
(InnoDBThe lock is acquired incrementally,Instead of acquiring all the locks needed at once.)
有可能A加锁了5和4,B加锁了1、2、3,If it goes further, there will be a situation where they wait for each other and become deadlocked,就是死锁.
4、Clustered index conflict
这种情况与3中的类似.
5、Gap lock conflict
这种情况是因为AThe first step uses a gap lock,在A释放锁之前BThe second step could not be completed,也会形成死锁.
innodb提供了wait-for graph算法来主动进行死锁检测,每当加锁请求无法立即满足需要并进入等待时,wait-for graph算法都会被触发,Check if a waiting loop occurs.当检测到死锁时,InnoDBA transaction with a lower cost will be selected for rollback.
参考
mysql死锁
MySQLHow to analyze deadlock problems&After locking the table, see deadlocks and quick solutions to remove deadlocks
mysqlHow to check if a table is locked
mysql 查看死锁和去除死锁
MySQL 死锁产生原因及解决方法
mysqlUnder what circumstances can a deadlock occur
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在.深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小.自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前.因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担.添加下方名片,即可获取全套学习资料哦
边栏推荐
- OD-Model [4]: SSD
- 磷脂-聚乙二醇-巯基,DSPE-PEG-Thiol,DSPE-PEG-SH,MW:5000
- 二维数组实战项目--------《三子棋》
- (Repost) HashCode Summary (1)
- 黑马案例--实现 clock 时钟的web服务器
- LeetCode:1374. 生成每种字符都是奇数个的字符串【签到题】
- getattr()函数解析
- 基于libmodbus库实现modbus TCP/RTU通信
- MySQL删除表数据 MySQL清空表命令 3种方法
- AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘
猜你喜欢
Knowledge Engineering Assignment 2: Introduction to Knowledge Engineering Related Fields
MySQL分区表详解
JSP WebSehll backdoor script
【深度学习】从LeNet-5识别手写数字入门深度学习
oracle inner join and outer join
parser = argparse.ArgumentParser() parsing
Mysql8创建用户以及赋权操作
day11--shell脚本
线性代数学习笔记3-1:矩阵与线性变换、常见矩阵(逆矩阵、伴随矩阵、正交矩阵等)
Redis的集群模式
随机推荐
SSM integration
MySQL占用CPU过高,排查原因及解决的多种方式法
MySQL8.0与MySQL5.7差异分析
知识问答与知识会话的区别
(Repost) HashCode Summary (1)
mysql8.0安装教程与配置(最详细)操作简单
删库后!除了跑路还能干什么?
【博学谷学习记录】超强总结,用心分享 | 软件测试 接口测试基础
Cloud server installation and deployment of Nacos 2.0.4 version
MySQL分页查询的5种方法
Day34 LeetCode
【面试】失败的一次面试
AttributeError: Can't get attribute 'SPPF' on
redis进行持久化时,有新的写操作将如何解决——写时复制
线性代数学习笔记3-3:逆矩阵的理解
JSP WebSehll backdoor script
Deveco studio 鸿蒙app访问网络详细过程(js)
[Remote Control Development Basic Tutorial 3] Crazy Shell Open Source Formation UAV-ADC (Joystick Control)
UserWarning:火炬。meshgrid:在以后的版本中,它将被要求通过索引ing argu
Week 7 Review