MySQL如何查看未提交的事务SQL
数据库干货铺
共 6948字,需浏览 14分钟
· 2023-01-07
MySQL中经常遇到事务中的SQL正在执行或执行完成后未提交,如何找出对应的SQL?
1. 查看正在执行的SQL
查看事务中正在执行的SQL方式有多种,例如
1.1 通过processlist查看
会话1:执行1个SQL
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> select sleep(20),now() ,id from test1;
会话2:开启另一个会话,查看对应的SQL
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
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> select sleep(20),now() ,id from test1;
会话2:查看对应的SQL
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
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)
此时查看事务情况
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)
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就可以查到
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的最后一个
例如:
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查看结果
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
2. mysql8.0新增用户及加密规则修改的那些事
3. 比hive快10倍的大数据查询利器-- presto
4. 监控利器出鞘:Prometheus+Grafana监控MySQL、Redis数据库
5. PostgreSQL主从复制--物理复制
6. MySQL传统点位复制在线转为GTID模式复制
评论
了解加密货币到加密货币的互换
1、什么是加密货币互换?加密货币到加密货币的互换是指以现行市场汇率将一种加密货币直接兑换为另一种加密货币。与需要法定货币存款和较长流程的传统交易所不同,加密货币到加密货币的互换可以无缝地促进交换。掉期在提高加密货币的流动性和效率方面发挥着重要作用。该功能使用户能够将他们的加密货币与钱包中的其他代币进
区块链头条
0
李彦宏:开源大模型不如闭源,后者会持续领先;周鸿祎:“开源不如闭源” 的言论是胡说八道
架构师大咖
架构师大咖,打造有价值的架构师交流平台。分享架构师干货、教程、课程、资讯。架构师大咖,每日推送。
公众号该公众号已被封禁0、李彦宏:开源大模型不如闭源,后者会持续领先当今
源码共读
0
【第129期】程序员的新宠:三款终端工具,让你告别Xshell!
概述 WindTerm:跨平台的SSH利器 首先介绍的是WindTerm,这是一款使用C语言开发的跨平台SSH客户端。它不仅完全免费,而且没有商业使用的限制。WindTerm支持SSH v2、Telnet、Raw Tcp等协议,而且性能出色,甚至超过了FinalShell和Electerm。功能
前端微服务
0
字节员工:35岁以后被裁员的,后来都走了哪条路?现在2-2,要不要利用最后一年拼命上个岸。
架构师大咖
架构师大咖,打造有价值的架构师交流平台。分享架构师干货、教程、课程、资讯。架构师大咖,每日推送。
公众号该公众号已被封禁在当今竞争激烈的职场环境中,年龄并不总是一个决定性
源码共读
0
上班的时候,有一群摸鱼搭子非常重要...
上班的时候,有一群摸鱼搭子非常重要!一到上班时间,他们就从四面八方涌进群里冒泡...从八卦聊到股市、从职场聊到乌X兰局势,偶尔还会复读、相亲、battle...然后,下午6点钟准时消失不见...所以你要不要加入我们一起摸鱼?我们有北京、上海、深圳、广州、杭州、武汉、成都、南京等8个城市的摸鱼群,还有
产品经理日记
0
周四002 瑞超:同样落寞的境遇——北雪平vs埃尔夫斯堡
上赛季最终排名联赛第9的北雪平本赛季伊始表现不佳,4轮战罢他们仅以1胜1平2负的战绩排在倒数第三,这支历史上曾夺得13次联赛冠军、6次杯赛冠军老牌劲旅,正如英格兰赛场上的一众百年俱乐部,在低谷中不断探索着出路。球队主教练安德烈亚斯·阿尔姆曾是AIK索尔纳及赫根队的主教练,他于今年年初刚刚拿起球队教鞭
产品与体验
0
雷军辟谣了!不是高考状元,卡里也没有冰冷的 40 亿
架构师大咖
架构师大咖,打造有价值的架构师交流平台。分享架构师干货、教程、课程、资讯。架构师大咖,每日推送。
公众号该公众号已被封禁最近很火的雷军简历,听说落魄时卡里只有冰冷的 40
源码共读
0
日本影山优佳最新杂志照,展现充满透明感的美丽
今天的图文分享的是影山优佳的杂志写真。元日向坂46的影山优佳,登上了写真杂志《周刊FLASH》5/7和5/14合并号的封面。影山优佳是日本艺人、女演员、前偶像。身高155厘米。2001年5月8日出生于东京都。2023年7月从组合日向坂46毕业,之后作为演员活跃的影山优佳,在《周刊FLAS
python教程
0