首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >MySQL如何查看未提交的事务SQL

MySQL如何查看未提交的事务SQL

作者头像
俊才
发布于 2023-01-16 06:22:20
发布于 2023-01-16 06:22:20
3.3K00
代码可运行
举报
文章被收录于专栏:数据库干货铺数据库干货铺
运行总次数:0
代码可运行

MySQL中经常遇到事务中的SQL正在执行或执行完成后未提交,如何找出对应的SQL?

1. 查看正在执行的SQL

查看事务中正在执行的SQL方式有多种,例如

1.1 通过processlist查看

会话1:执行1个SQL

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制

mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> select sleep(20),now() ,id from test1;

会话2:开启另一个会话,查看对应的SQL

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制

mysql> select  id ,info  from information_schema.processlist where info is not null;
+----+------------------------------------------------------------------------------+
| id | info                                                                         |
+----+------------------------------------------------------------------------------+
| 36 | select sleep(20),now() ,id from test1                                        |
| 37 | select  id ,info  from information_schema.processlist where info is not null |
+----+------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

可以看到正在执行的SQL,包括自己的SQL的id及内容

1.2 通过events_statements_current查看

会话1:执行1个SQL

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制

mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> select sleep(20),now() ,id from test1;

会话2:查看对应的SQL

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
mysql> select a.id,a.info, b.thread_id, c.sql_text  from information_schema.processlist  a, performance_schema.threads b, performance_schema.events_statements_current c  where  a.id=b.processlist_id  and b.thread_id = c.thread_id\G
*************************** 1. row ***************************
       id: 36
     info: select sleep(20),now() ,id from test1
thread_id: 76
 sql_text: select sleep(20),now() ,id from test1
*************************** 2. row ***************************
       id: 37
     info: select a.id,a.info, b.thread_id, c.sql_text  from information_schema.processlist  a, performance_schema.threads b, performance_schema.events_statements_current c  where  a.id=b.processlist_id  and b.thread_id = c.thread_id
thread_id: 77
 sql_text: select a.id,a.info, b.thread_id, c.sql_text  from information_schema.processlist  a, performance_schema.threads b, performance_schema.events_statements_current c  where  a.id=b.processlist_id  and b.thread_id = c.thread_id
2 rows in set (0.01 sec)

2. 方式对比

通过processlist和通过events_statements_current区别在于,processlist中能查到的SQL是正在运行的SQL,而运行结束的SQL是看不到的。

会话1:执行1个SQL

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制

mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> select sleep(2),now() ,id from test1;
+----------+---------------------+----+
| sleep(2) | now()               | id |
+----------+---------------------+----+
|        0 | 2023-01-03 22:01:09 |  1 |
+----------+---------------------+----+
1 row in set (2.00 sec)

此时查看事务情况

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制

mysql> select  * from information_schema.innodb_trx\G
*************************** 1. row ***************************
                    trx_id: 421227264232664
                 trx_state: RUNNING
               trx_started: 2023-01-03 22:01:09
     trx_requested_lock_id: NULL
          trx_wait_started: NULL
                trx_weight: 0
       trx_mysql_thread_id: 36
                 trx_query: NULL
       trx_operation_state: NULL
         trx_tables_in_use: 0
         trx_tables_locked: 0
          trx_lock_structs: 0
     trx_lock_memory_bytes: 1128
           trx_rows_locked: 0
         trx_rows_modified: 0
   trx_concurrency_tickets: 0
       trx_isolation_level: REPEATABLE READ
         trx_unique_checks: 1
    trx_foreign_key_checks: 1
trx_last_foreign_key_error: NULL
 trx_adaptive_hash_latched: 0
 trx_adaptive_hash_timeout: 0
          trx_is_read_only: 0
trx_autocommit_non_locking: 0
       trx_schedule_weight: NULL
1 row in set (0.00 sec)

其中trx_mysql_thread_id=36的会话正是我们会话1的线程id,但是我们看不到具体的SQL

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制

mysql> select *  from information_schema.processlist where id=36;
+----+------+-----------+--------+---------+------+-------+------+
| ID | USER | HOST      | DB     | COMMAND | TIME | STATE | INFO |
+----+------+-----------+--------+---------+------+-------+------+
| 36 | root | localhost | testdb | Sleep   |  177 |       | NULL |
+----+------+-----------+--------+---------+------+-------+------+
1 row in set (0.00 sec)

但是此时通过方式2就可以查到

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制

mysql> select a.id,a.info, b.thread_id, c.sql_text  from information_schema.processlist  a, performance_schema.threads b, performance_schema.events_statements_current c  where  a.id=b.processlist_id  and b.thread_id = c.thread_id\G
*************************** 1. row ***************************
       id: 36
     info: NULL
thread_id: 76
 sql_text: select sleep(2),now() ,id from test1
*************************** 2. row ***************************
       id: 37
     info: select a.id,a.info, b.thread_id, c.sql_text  from information_schema.processlist  a, performance_schema.threads b, performance_schema.events_statements_current c  where  a.id=b.processlist_id  and b.thread_id = c.thread_id
thread_id: 77
 sql_text: select a.id,a.info, b.thread_id, c.sql_text  from information_schema.processlist  a, performance_schema.threads b, performance_schema.events_statements_current c  where  a.id=b.processlist_id  and b.thread_id = c.thread_id
2 rows in set (0.00 sec)

注意:此时只能查到一个事务中的多条SQL的最后一个

例如:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制

mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> select sleep(2),now() ,id from test1;
+----------+---------------------+----+
| sleep(2) | now()               | id |
+----------+---------------------+----+
|        0 | 2023-01-03 22:01:09 |  1 |
+----------+---------------------+----+
1 row in set (2.00 sec)

mysql> select sleep(1),now() ,id from test1;
+----------+---------------------+----+
| sleep(1) | now()               | id |
+----------+---------------------+----+
|        0 | 2023-01-03 22:06:35 |  1 |
+----------+---------------------+----+

会话2查看结果

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制

mysql> select a.id,a.info, b.thread_id, c.sql_text  from information_schema.processlist  a, performance_schema.threads b, performance_schema.events_statements_current c  where  a.id=b.processlist_id  and b.thread_id = c.thread_id\G
*************************** 1. row ***************************
       id: 36
     info: NULL
thread_id: 76
 sql_text: select sleep(1),now() ,id from test1
*************************** 2. row ***************************
       id: 37
     info: select a.id,a.info, b.thread_id, c.sql_text  from information_schema.processlist  a, performance_schema.threads b, performance_schema.events_statements_current c  where  a.id=b.processlist_id  and b.thread_id = c.thread_id
thread_id: 77
 sql_text: select a.id,a.info, b.thread_id, c.sql_text  from information_schema.processlist  a, performance_schema.threads b, performance_schema.events_statements_current c  where  a.id=b.processlist_id  and b.thread_id = c.thread_id
2 rows in set (0.00 sec)

可见,查到的是最后一个SQL了,如果事务手动commit提交了,则显示的是commit

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2023-01-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 数据库干货铺 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
MySQL找出未提交事务的信息
我们经常会碰到这样的情况,某个事务执行完了未提交,后续再来一个DDL和DML操作,导致后面的session要么处于waiting for metadata lock,要么是锁等待超时。这时我们往往只能找到这个未提交的事务的事务id和session id,但是一般都处于sleep状态,不好分析事务内容到底是什么,所以通常都是粗鲁地kill这个session后解决问题,但是应用层的研发人员往往找不到到底是哪个事务引起的,后面再出现问题时还要重复kill。
用户1148526
2020/03/31
5.3K0
Mysql长事务总结
在结果中idletime是计算产生的,也是事务的持续时间。但事务的trxquery是NUL,这并不是说事务什么也没执行,一个事务可能包含多个SQL,如果SQL执行完毕就不再显示了。当前事务正在执行,innodb也不知道这个事务后续还有没有sql,啥时候会commit。 因此trx_query不能提供有意义的信息。
mingjie
2022/05/12
9310
Mysql长事务总结
MySQL-长事务详解
『入门MySQL』系列文章已经完结,今后我的文章还是会以MySQL为主,主要记录下近期工作及学习遇到的场景或者自己的感悟想法,可能后续的文章不是那么连贯,但还是希望大家多多支持。言归正传,本篇文章主要介绍MySQL长事务相关内容,比如说我们开启的一个事务,一直没提交或回滚会怎样呢,出现事务等待情况应该如何处理,本篇文章将给你答案。
MySQL技术
2019/09/23
3.5K0
为什么要避免大事务以及大事务如何解决?
https://juejin.im/post/5d837d2c6fb9a06b05182737
lyb-geek
2022/03/04
7620
【MySQL】metadata lock问题
MySQL使用DML来管理对数据库对象的并发访问,并确保数据一致性。DML不仅适用于表,还适用于模式和存储程序(过程、函数、触发器和计划的事件)
用户5522200
2020/06/11
1.6K0
MySQL 5.6中如何定位DDL被阻塞的问题
在上一篇文章《MySQL 5.7中如何定位DDL被阻塞的问题》中,对于DDL被阻塞问题的定位,我们主要是基于MySQL 5.7新引入的performance_schema.metadata_locks表。提出的定位方法,颇有种"锦上添花"的意味,而且,也只适用于MySQL 5.7开始的版本。
星哥玩云
2022/08/17
4890
为什么MySQL没有负载,但交易却跑不动?
在MySQL的数据库中,我们有时会发现MySQL数据库明明没有负载,CPU、硬盘、内存和网络等资源都很空闲,但很多SQL都pending在哪儿,MySQL数据库无法处理交易。这是怎么回事呢?
姚远OracleACE
2023/11/01
3630
为什么MySQL没有负载,但交易却跑不动?
什么是大事务?以及大事务产生的问题
我这里按公司实际场景,规定了,每次操作/获取数据量应该少于5000条,结果集应该小于2M
终有救赎
2023/12/14
7720
MySQL8.0锁情况排查
在数据库中,除传统的计算资源(CPU、RAM、IO)的争用以外,数据也是一种供许多用户共享的资源。如何保证数据并发访问的一致性、有效性是所有数据库必须解决的一个问题,锁冲突也是影响数据库并发访问性能的一个重要因素。从这个角度来说,锁对教据库而言显得尤其重要,也更加复杂。
GreatSQL社区
2023/02/22
2.3K0
应用示例荟萃 | performance_schema全方位介绍(中)
表级锁对应的instruments(wait/lock/table/sql/handler)默认启用,对应的consumers表为performance_schema.table_handles在setup_consumers只受全局配置项global_instrumentation控制,默认启用。所以,默认情况下只需要设置系统配置参数performance_schema=ON即可,下面我们通过一个示例演示如何找出谁持有表级锁
老叶茶馆
2020/11/26
4350
MySQL数据库故障分析-锁等待(一)
有一张权限表,同时执行了delete和truncate操作,并且长时间没有提交,导致metadata lock无法释放,应用登录时无法正常读取权限表,导致应用无法登录。
PHP开发工程师
2022/06/17
1.2K0
20个MySQL运维案例,请查收!
墨墨导读:日常MySQL运维中,会遇到各种各样的问题,下面分享二十个MySQL运维案例,附有问题的分析和解决办法,希望你遇到同样的问题的时候,可以淡定地处理。
数据和云
2020/10/27
1.4K0
20个MySQL运维案例,请查收!
MySQL5.7+查看Waiting for table metadata lock 锁情况
Waiting for table metadata lock 这个mdl锁,我们最常见,这篇先拿它开刀。
保持热爱奔赴山海
2020/07/27
3.7K0
MySQL5.7+查看Waiting for table metadata lock  锁情况
MySQL 5.7中如何定位DDL被阻塞的问题
在上篇文章《MySQL表结构变更,不可不知的Metadata Lock》中,我们介绍了MDL引入的背景,及基本概念,从“道”的层面知道了什么是MDL。下面就从“术”的层面看看如何定位MDL的相关问题。
星哥玩云
2022/08/17
6960
MySQL 5.7中如何定位DDL被阻塞的问题
MOP 系列|MOP 三种主流数据库常用 SQL(二)
MOP 不用多说,指的就是 MySQL、Oracle、PostgreSQL 三种目前最主流的数据库,MOP 系列打算更新 MOP 三种数据库的索引知识、高可用架构及常用 SQL 语句等等,上面已经更新了 MOP 索引相关的文章,今天打算整理一下这三种数据库的常用 SQL 知识,由于文章过长,今天更新中间的一篇之 MySQL 篇。第一篇 Oracle 相关的详见下方链接:MOP 系列|MOP 三种主流数据库常用 SQL(一)。
JiekeXu之路
2024/05/28
1360
MOP 系列|MOP 三种主流数据库常用 SQL(二)
生产运维脚本引发的 MDL 锁故障排查之旅
作者:何文超,分享 MySQL 和 OceanBase 相关技术博文。 个人博客【CSDN | 雅俗数据库】
爱可生开源社区
2025/05/21
1070
生产运维脚本引发的 MDL 锁故障排查之旅
找出未提交的MySQL线程/事务
SELECT * from information_schema.processlist;   这个能看到上面哪个SQL线程ID(下图的378号线程就是造成MDL锁的罪魁祸首)
保持热爱奔赴山海
2019/09/18
2.6K0
找出未提交的MySQL线程/事务
故障分析 | 有效解决 MySQL 行锁等待超时问题【建议收藏】
上述这个错误,接触 MySQL 的同学或多或少应该都遇到过,专业一点来说,这个报错我们称之为锁等待超时。
爱可生开源社区
2020/04/27
4.1K0
[MYSQL] show engine innodb status中的死锁 分析
很久以前(也才2年)写过一个解析innodb_status的脚本. 看起来像那么回事, 其实就是做了个翻译和总结.
大大刺猬
2024/08/26
9740
[MYSQL] show engine innodb status中的死锁 分析
配置查询与线程追踪函数|全方位认识 sys 系统库
不知不觉中,我们的"全方位认识 sys 系统库" 系列文章已经接近尾声了,在上一篇《字符串与数字转换函数|全方位认识 sys 系统库》中,我们介绍了sys 系统库中用于字符串和数字格式化转换的函数,本期的内容给大家介绍 sys 系统库中的剩余函数,这也是本系列文章的最后一篇。
老叶茶馆
2020/12/15
1.9K0
相关推荐
MySQL找出未提交事务的信息
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档