MySQL 5.7中如菠菜源码出售何定位DDL被阻塞的问题

在MySQL 5.7中,针对MDL,菠菜源码出售(www.1159880099.com)QQ1159880099 引入了一张新表performance_schema.metadata_locks,该表可对外展示MDL的相关信息,包括其作用对象,类型及持有等待情况。

开启MDL的instrument

但是相关instrument并没有开启(MySQL 8.0是默认开启的),其可通过如下两种方式开启,

临时生效

修改performance_schema.setup_instrume nts表,但实例重启后,又会恢复为默认值。

UPDATE performance_schema.setup_instruments SET ENABLED = ‘YES‘, TIMED = ‘YES‘
WHERE NAME = ‘wait/lock/metadata/sql/mdl‘;

永久生效
在配置文件中设置
[mysqld]
performance-schema-instrument=‘wait/lock/metadata/sql/mdl=ON‘

测试场景
下面结合一个简单的Demo,来看看在MySQL 5.7中如何定位DDL操作的阻塞问题。

复制代码
session1> begin;
Query OK, 0 rows affected (0.00 sec)

session1> delete from slowtech.t1 where id=2;
Query OK, 1 row affected (0.00 sec)

session1> select * from slowtech.t1;
+------+------+
| id | name |
+------+------+
| 1 | a |
+------+------+
1 row in set (0.00 sec)

session1> update slowtech.t1 set name=‘c‘ where id=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

session2> alter table slowtech.t1 add c1 int; ##被阻塞

session3> show processlist;
+----+------+-----------+------+---------+------+---------------------------------+------------------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+---------------------------------+------------------------------------+
| 2 | root | localhost | NULL | Sleep | 51 | | NULL |
| 3 | root | localhost | NULL | Query | 0 | starting | show processlist |
| 4 | root | localhost | NULL | Query | 9 | Waiting for table metadata lock | alter table slowtech.t1 add c1 int |
+----+------+-----------+------+---------+------+---------------------------------+------------------------------------+
3 rows in set (0.00 sec)

session3> select object_type,object_schema,object_name,lock_type,lock_duration,lock_status,owner_thread_id from performance_schema.metadata_locks;
+-------------+--------------------+----------------+---------------------+---------------+-------------+-----------------+
| object_type | object_schema | object_name | lock_type | lock_duration | lock_status | owner_thread_id |
+-------------+--------------------+----------------+---------------------+---------------+-------------+-----------------+
| TABLE | slowtech | t1 | SHARED_WRITE | TRANSACTION | GRANTED | 27 |
| GLOBAL | NULL | NULL | INTENTION_EXCLUSIVE | STATEMENT | GRANTED | 29 |
| SCHEMA | slowtech | NULL | INTENTION_EXCLUSIVE | TRANSACTION | GRANTED | 29 |
| TABLE | slowtech | t1 | SHARED_UPGRADABLE | TRANSACTION | GRANTED | 29 |
| TABLE | slowtech | t1 | EXCLUSIVE | TRANSACTION | PENDING | 29 |
| TABLE | performance_schema | metadata_locks | SHARED_READ | TRANSACTION | GRANTED | 28 |
+-------------+--------------------+----------------+---------------------+---------------+-------------+-----------------+
6 rows in set (0.00 sec)
复制代码
这里,重点关注lock_status,"PENDING"代表线程在等待MDL,而"GRANTED"则代表线程持有MDL。

如何找出引起阻塞的会话

结合owner_thread_id,可以可到,是29号线程在等待27号线程的MDL,此时,可kill掉52号线程。

但需要注意的是,owner_thread_id给出的只是线程ID,并不是show processlist中的ID。如果要查找线程对应的processlist id,需查询performance_schema.threads表。

复制代码
session3> select * from performance_schema.threads where thread_id in (27,29)\G
1. row
THREAD_ID: 27
NAME: thread/sql/one_connection
TYPE: FOREGROUND
PROCESSLIST_ID: 2
PROCESSLIST_USER: root
PROCESSLIST_HOST: localhost
PROCESSLIST_DB: NULL
PROCESSLIST_COMMAND: Sleep
PROCESSLIST_TIME: 214
PROCESSLIST_STATE: NULL
PROCESSLIST_INFO: NULL
PARENT_THREAD_ID: 1
ROLE: NULL
INSTRUMENTED: YES
HISTORY: YES
CONNECTION_TYPE: Socket
THREAD_OS_ID: 9800
2. row
THREAD_ID: 29
NAME: thread/sql/one_connection
TYPE: FOREGROUND
PROCESSLIST_ID: 4
PROCESSLIST_USER: root
PROCESSLIST_HOST: localhost
PROCESSLIST_DB: NULL
PROCESSLIST_COMMAND: Query
PROCESSLIST_TIME: 172
PROCESSLIST_STATE: Waiting for table metadata lock
PROCESSLIST_INFO: alter table slowtech.t1 add c1 int
PARENT_THREAD_ID: 1
ROLE: NULL
INSTRUMENTED: YES
HISTORY: YES
CONNECTION_TYPE: Socket
THREAD_OS_ID: 9907
2 rows in set (0.00 sec)
复制代码
将这两张表结合,借鉴sys.innodb_lock _waits的输出,实际上我们也可以直观地呈现MDL的等待关系。

复制代码
SELECT
a.OBJECT_SCHEMA AS locked_schema,
a.OBJECT_NAME AS locked_table,
"Metadata Lock" AS locked_type,
c.PROCESSLIST_ID AS waiting_processlist_id,
c.PROCESSLIST_TIME AS waiting_age,
c.PROCESSLIST_INFO AS waiting_query,
c.PROCESSLIST_STATE AS waiting_state,
d.PROCESSLIST_ID AS blocking_processlist_id,
d.PROCESSLIST_TIME AS blocking_age,
d.PROCESSLIST_INFO AS blocking_query,
concat(‘KILL ‘, d.PROCESSLIST_ID) AS sql_kill_blocking_connection
FROM
performance_schema.metadata_locks a
JOIN performance_schema.metadata_locks b ON a.OBJECT_SCHEMA = b.OBJECT_SCHEMA
AND a.OBJECT_NAME = b.OBJECT_NAME
AND a.lock_status = ‘PENDING‘
AND b.lock_status = ‘GRANTED‘
AND a.OWNER_THREAD_ID <> b.OWNER_THREAD_ID
AND a.lock_type = ‘EXCLUSIVE‘
JOIN performance_schema.threads c ON a.OWNER_THREAD_ID = c.THREAD_ID
JOIN performance_schema.threads d ON b.OWNER_THREAD_ID = d.THREAD_ID\G

1. row
locked_schema: slowtech
locked_table: t1
locked_type: Metadata Lock
waiting_processlist_id: 4
waiting_age: 259
waiting_query: alter table slowtech.t1 add c1 int
waiting_state: Waiting for table metadata lock
blocking_processlist_id: 2
blocking_age: 301
blocking_query: NULL
sql_kill_blocking_connection: KILL 2
1 row in set (0.00 sec)
复制代码
输出一目了然,DDL操作如果要获得MDL,执行kill 2即可。

官方的sys.schematablelock_waits
实际上,MySQL 5.7在sys库中也集成了类似功能,同样的场景,其输出如下,

复制代码
mysql> select * from sys.schema_table_lock_waits\G
1. row
object_schema: slowtech
object_name: t1
waiting_thread_id: 29
waiting_pid: 4
waiting_account: [email protected]
waiting_lock_type: EXCLUSIVE
waiting_lock_duration: TRANSACTION
waiting_query: alter table slowtech.t1 add c1 int
waiting_query_secs: 446
waiting_query_rows_affected: 0
waiting_query_rows_examined: 0
blocking_thread_id: 27
blocking_pid: 2
blocking_account: [email protected]
blocking_lock_type: SHARED_READ
blocking_lock_duration: TRANSACTION
sql_kill_blocking_query: KILL QUERY 2
sql_kill_blocking_connection: KILL 2
2. row
object_schema: slowtech
object_name: t1
waiting_thread_id: 29
waiting_pid: 4
waiting_account: [email protected]
waiting_lock_type: EXCLUSIVE
waiting_lock_duration: TRANSACTION
waiting_query: alter table slowtech.t1 add c1 int
waiting_query_secs: 446
waiting_query_rows_affected: 0
waiting_query_rows_examined: 0
blocking_thread_id: 29
blocking_pid: 4
blocking_account: [email protected]
blocking_lock_type: SHARED_UPGRADABLE
blocking_lock_duration: TRANSACTION
sql_kill_blocking_query: KILL QUERY 4
sql_kill_blocking_connection: KILL 4
2 rows in set (0.00 sec)
复制代码
具体分析下官方的输出,

只有一个alter table操作,却产生了两条记录,而且两条记录的kill对象竟然还不一样,对表结构不熟悉及不仔细看记录内容的话,难免会kill错对象。

不仅如此,如果有N个查询被DDL操作堵塞,则会产生N2条记录。在阻塞操作较多的情况下,这N2条记录完全是个噪音。

而之前的SQL,无论有多少操作被阻塞,一个alter table操作,就只会输出一条记录。

如何查看阻塞会话已经执行过的操作
但上面这个SQL也有遗憾,其blocking_query为NULL,而在会话1中,其明明已经执行了三个SQL。

这个与performance_schema.threads(类似于show processlist)有关,其只会输出当前正在运行的SQL,对于已经执行过的,实际上是没办法看到。

但在线上,kill是一个需要谨慎的操作,毕竟你很难知道kill的是不是业务关键操作?又或者,是个批量update操作?那么,有没有办法抓到该事务之前的操作呢?

答案,有。

即Performance Schema中记录Statement Event(操作事件)的表,具体包括events_statements_current,events_statements_history,events_statements_history_long,prepared_statements_instances。

常用的是前面三个。

三者的表结构完全一致,其中,events_statements_history又包含了events_statements_current的操作,所以我们这里会使用events_statements_history。

终极SQL如下,

复制代码
SELECT
locked_schema,
locked_table,
locked_type,
waiting_processlist_id,
waiting_age,
waiting_query,
waiting_state,
blocking_processlist_id,
blocking_age,
substring_index(sql_text,"transaction_begin;" ,-1) AS blocking_query,
sql_kill_blocking_connection
FROM
(
SELECT
b.OWNER_THREAD_ID AS granted_thread_id,
a.OBJECT_SCHEMA AS locked_schema,
a.OBJECT_NAME AS locked_table,
"Metadata Lock" AS locked_type,
c.PROCESSLIST_ID AS waiting_processlist_id,
c.PROCESSLIST_TIME AS waiting_age,
c.PROCESSLIST_INFO AS waiting_query,
c.PROCESSLIST_STATE AS waiting_state,
d.PROCESSLIST_ID AS blocking_processlist_id,
d.PROCESSLIST_TIME AS blocking_age,
d.PROCESSLIST_INFO AS blocking_query,
concat(‘KILL ‘, d.PROCESSLIST_ID) AS sql_kill_blocking_connection
FROM
performance_schema.metadata_locks a
JOIN performance_schema.metadata_locks b ON a.OBJECT_SCHEMA = b.OBJECT_SCHEMA
AND a.OBJECT_NAME = b.OBJECT_NAME
AND a.lock_status = ‘PENDING‘
AND b.lock_status = ‘GRANTED‘
AND a.OWNER_THREAD_ID <> b.OWNER_THREAD_ID
AND a.lock_type = ‘EXCLUSIVE‘
JOIN performance_schema.threads c ON a.OWNER_THREAD_ID = c.THREAD_ID
JOIN performance_schema.threads d ON b.OWNER_THREAD_ID = d.THREAD_ID
) t1,
(
SELECT
thread_id,
group_concat( CASE WHEN EVENT_NAME = ‘statement/sql/begin‘ THEN "transaction_begin" ELSE sql_text END ORDER BY event_id SEPARATOR ";" ) AS sql_text
FROM
performance_schema.events_statements_history
GROUP BY thread_id
) t2
WHERE
t1.granted_thread_id = t2.thread_id \G

1. row
locked_schema: slowtech
locked_table: t1
locked_type: Metadata Lock
waiting_processlist_id: 4
waiting_age: 294
waiting_query: alter table slowtech.t1 add c1 int
waiting_state: Waiting for table metadata lock
blocking_processlist_id: 2
blocking_age: 336
blocking_query: delete from slowtech.t1 where id=2;select * from slowtech.t1;update slowtech.t1 set name=‘c‘ where id=1
sql_kill_blocking_connection: KILL 2
1 row in set, 1 warning (0.00 sec)
复制代码
从上面的输出可以看到,blocking_query中包含了会话1中当前事务的所有操作,按执行的先后顺序输出。

需要注意的是,默认情况下,events_statements_history只会保留每个线程最近的10个操作,如果事务中进行的操作较多,实际上也是没办法抓全的。

原文地址:http://blog.51cto.com/13924318/2160231

时间: 2024-10-07 11:29:36

MySQL 5.7中如菠菜源码出售何定位DDL被阻塞的问题的相关文章

PHP+MySQL数据库编程与极速飞艇源码出售步骤

第一步:PHP连接MySQL服务器第二步:极速飞艇源码出售[企鹅21717-93408]第三步:设置请求或返回的数据的字符集第四步:执行各种SQL语句.PHP连接MySQL服务器1.mysql_connect()描述:PHP连接MySQL服务器.语法: resource $link = mysql_connect($hostname,$username,$password)//$hostname:是MySQL服务器的域名或IP地址.也可以加端口号(3306).如:localhost:3306//

JAVA中获取ylc源码出售当前系统时间

一. 获取当前系统时间和日期并格式化输出:ylc源码出售[企鹅21717-93408] import java.util.Date;import java.text.SimpleDateFormat;public class NowString {public static void main(String[] args) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式System

菠菜源码下载与优化sql方案

为什么使用索引:1.索引大大减少了存储引擎需要的扫描数据量?2.帮助我们进行排序避免使用的临时表?3.把随机IO变为顺序IO 菠菜源码搭建QQ:2152876294 网址diguaym.com索引是不是越多越好:1.索引增加写入的成本?2.太多索引会增加查询优化器的选择时间索引是在存储引擎层的作用:B-tree索引的特点:1.常见索引,默认的索引,叶子键遍历?2.以B+树的结构存储数据?3.能加快数据的查询速度?4.适合进行范围查找什么情况下可以使用B树索引:1.全值匹配的查询?2.匹配最左前缀

MongoDB Connector For BI配置使用与菠菜源码下载

MongoDB Connector For BI 允许你以关系型数据库连接mongodb,菠菜源码下载(企 娥:217 1793 408)供BI分析之类的工具读取分析数据,它并不存储数据,仅仅只做为一个数据库查询的转换桥梁 新版本的mongo-bi从原来的4个组件减少到了2个组件: mongodrdl 生成 DRDL文件, 用于映射collection的表结构mongosqld 实际的转换器,启动时至少需要一个DRDL文件(或者指定DRDL所在的文件夹).新版本兼容SQL-99 SELECT查询

菠菜源码搭建与微信小程序Demo

demo 的界面设计以及交互设计 工具 菠菜源码搭建QQ:2152876294 网址diguaym.com已经全面对非邀请内测用户开放, 且在持续更新(我码代码的过程中就更新了两版, 所以开发时 IDE 版本不唯一) 不过其实忍受了半个小时微信的开发者工具之后, 我就改在 webstorm 中编辑了, 微信工具成了运行预览的工具, 不过听说IDE 中预览的效果, 也不能保证与真机一样哦~ 设计和功能: 知乎安卓版本 非常之简易版数据: 毕竟是知乎, 为了防止版权问题, fake 的数据使用的是我

菠菜源码搭建与java基础概念

1.java jvm的功能:通过 ClassLoader 寻找和装载 class 文件?? ??? ??? ?? 解释字节码成为指令并执行,提供 class 文件的运行环境?? ??? ??? ?? 进行运行期间垃圾回收 菠菜源码搭建QQ:2152876294 网址diguaym.com?? ??? ??? ?? 提供与硬件交互的平台?? ?2.运算符优先级:单目 >运算>移位>比较>按位>逻辑>三目 >赋值?? ??? ??? ?单目:单目运算符+ –(负数)

Jenkis pipeline构建菠菜源码搭建出售项目实践

在完成前文的jenkins server 在k8s环境部署 之后,本文我们来测试在k8s集群环境中的jenkins pipeline构建项目和更新,具体环境要求如下:1.jenkins pipeline插件安装成功2.要更新的应用已提前部署3.Jenkins slave中需要有kubectl.svn.mvn客户端且环境变量设置准确4.Jenkis slave需要能和master的api-server进行正常通信(这里为了简便,前文构建jenkins server的时候直接对defalut这个se

android适配华为菠菜源码搭建虚拟键

在做菠菜源码搭建 dsluntan.com VX:17061863513横竖屏展示时,发现网上适配虚拟键代码没有做横屏状态下适配,导致横屏状态下,底部虚拟键遮挡了布局内容. 所以横屏状态下也需要适配华为虚拟键.只需要在content布局改变时,同时记录当前可用的视图宽度,重新请求布局即可.下面是代码: import android.content.Context;import android.content.res.Resources;import android.graphics.Rect;i

信用盘源码出售实现mysql的基本操作

为了简化数据库操作,信用盘源码出售[企鹅21717-93408]我们将使用SQLAlchemy一个数据库工具(ORM,即对象关系映射).借助SQLAlchemy,你可以通过定义Python类来表示数据库里的一张表(类属性表示表中的字段/列),通过对这个类进行各种操作来代替写SQL语句.这个列我们称之为模型类,类中的属性我们将称之为字段. Flask有大量的第三方扩展,这些扩展可以简化和第三方库的集成工作. 首先我们要"初始化"操作.导入扩展类,实例化并传入Flask程序实例: from