在数据库中,排查除传统的情况计算资源(CPU、RAM、排查IO)的情况争用以外,数据也是排查一种供许多用户共享的资源。如何保证数据并发访问的情况一致性、有效性是排查所有数据库必须解决的一个问题,锁冲突也是情况影响数据库并发访问性能的一个重要因素。从这个角度来说,排查锁对教据库而言显得尤其重要,情况也更加复杂。
本文将通过实验介绍MySQL8.0版锁该如何排查,以及找到阻塞的sql语句,实验的MySQL版本为8.0.26,隔离级别为RR。
information_schema.innodb_trx ##正在运行的事务信息。
sys.innodb_lock_waits ##处于锁等待的关联事务信息。
performance_schema.threads ##SQL线程及线程号、进程号、OS线程号等信息
# 确认有没有锁等待:
show status like 'innodb_row_lock%';
select * from information_schema.innodb_trx;
# 查询锁等待详细信息
select * from sys.innodb_lock_waits; ----> blocking_pid(锁源的连接线程)
# 通过连接线程ID找SQL线程语句
select * from performance_schema.threads;
# 通过SQL线程找到SQL语句
select * from performance_schema.events_statements_history;
mysql> use world;
mysql> show tables;
+-----------------+
| Tables_in_world |
+-----------------+
| city |
| country |
| countrylanguage |
+-----------------+
3 rows in set (0.00 sec)
s1:
# 加排他锁
mysql> begin;
mysql> select * from world.city where id=1 for update;
s2:
# 加排他锁
mysql> begin;
mysql> update city set name='girl' where id=1;
执行完处于夯住状态,默认50秒会超时回滚。
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
mysql> set innodb_lock_wait_timeout=5000; ##锁等待超时参数,这里设置为5000便于测试.
mysql> update city set name='girl' where id=1;
mysql> use information_schema;
mysql> select trx_id,trx_state,trx_started,trx_tables_locked,trx_rows_locked from innodb_trx\G
(责任编辑:百科)
金富科技(003018.SZ)2020年度净利润降14.99% 基本每股收益0.44元
一加10 Pro为啥能成为手游比赛指定用机?可不光是性能 -
光正眼科(002524.SZ):3月18日首次回购103.5万元股份 最高成交价为10.35元/股
新增多项功能!OpenAI 将对 ChatGPT 进行更新 -