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

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

1. 查看正在执行的SQL

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

1.1 通过processlist查看

会话1:执行1个SQL

mysql>begin;
QueryOK, 0rowsaffected (0.00sec)


mysql>selectsleep(20),now() ,idfromtest1;

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

mysql>selectid ,infofrominformation_schema.processlistwhereinfoisnotnull;
+----+------------------------------------------------------------------------------+
|id|info|
+----+------------------------------------------------------------------------------+
|36|selectsleep(20),now() ,idfromtest1|
|37|selectid ,infofrominformation_schema.processlistwhereinfoisnotnull|
+----+------------------------------------------------------------------------------+
2rowsinset (0.00sec)

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

1.2 通过events_statements_current查看

会话1:执行1个SQL

mysql>begin;
QueryOK, 0rowsaffected (0.00sec)


mysql>selectsleep(20),now() ,idfromtest1;

会话2:查看对应的SQL

mysql>selecta.id,a.info, b.thread_id, c.sql_textfrominformation_schema.processlista, performance_schema.threadsb, performance_schema.events_statements_currentcwherea.id=b.processlist_idandb.thread_id=c.thread_id\G
***************************1.row***************************
id: 36
info: selectsleep(20),now() ,idfromtest1
thread_id: 76
sql_text: selectsleep(20),now() ,idfromtest1
***************************2.row***************************
id: 37
info: selecta.id,a.info, b.thread_id, c.sql_textfrominformation_schema.processlista, performance_schema.threadsb, performance_schema.events_statements_currentcwherea.id=b.processlist_idandb.thread_id=c.thread_id
thread_id: 77
sql_text: selecta.id,a.info, b.thread_id, c.sql_textfrominformation_schema.processlista, performance_schema.threadsb, performance_schema.events_statements_currentcwherea.id=b.processlist_idandb.thread_id=c.thread_id
2rowsinset (0.01sec)

2. 方式对比

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

会话1:执行1个SQL

mysql>begin;
QueryOK, 0rowsaffected (0.00sec)


mysql>selectsleep(2),now() ,idfromtest1;
+----------+---------------------+----+
|sleep(2) |now() |id|
+----------+---------------------+----+
|0|2023-01-0322:01:09|1|
+----------+---------------------+----+
1rowinset (2.00sec)

此时查看事务情况

mysql>select*frominformation_schema.innodb_trx\G
***************************1.row***************************
trx_id: 421227264232664
trx_state: RUNNING
trx_started: 2023-01-0322: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: REPEATABLEREAD
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
1rowinset (0.00sec)

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

mysql>select*frominformation_schema.processlistwhereid=36;
+----+------+-----------+--------+---------+------+-------+------+
|ID|USER|HOST|DB|COMMAND|TIME|STATE|INFO|
+----+------+-----------+--------+---------+------+-------+------+
|36|root|localhost|testdb|Sleep|177||NULL|
+----+------+-----------+--------+---------+------+-------+------+
1rowinset (0.00sec)

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

mysql>selecta.id,a.info, b.thread_id, c.sql_textfrominformation_schema.processlista, performance_schema.threadsb, performance_schema.events_statements_currentcwherea.id=b.processlist_idandb.thread_id=c.thread_id\G
***************************1.row***************************
id: 36
info: NULL
thread_id: 76
sql_text: selectsleep(2),now() ,idfromtest1
***************************2.row***************************
id: 37
info: selecta.id,a.info, b.thread_id, c.sql_textfrominformation_schema.processlista, performance_schema.threadsb, performance_schema.events_statements_currentcwherea.id=b.processlist_idandb.thread_id=c.thread_id
thread_id: 77
sql_text: selecta.id,a.info, b.thread_id, c.sql_textfrominformation_schema.processlista, performance_schema.threadsb, performance_schema.events_statements_currentcwherea.id=b.processlist_idandb.thread_id=c.thread_id
2rowsinset (0.00sec)

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

例如:

mysql>begin;
QueryOK, 0rowsaffected (0.00sec)


mysql>selectsleep(2),now() ,idfromtest1;
+----------+---------------------+----+
|sleep(2) |now() |id|
+----------+---------------------+----+
|0|2023-01-0322:01:09|1|
+----------+---------------------+----+
1rowinset (2.00sec)


mysql>selectsleep(1),now() ,idfromtest1;
+----------+---------------------+----+
|sleep(1) |now() |id|
+----------+---------------------+----+
|0|2023-01-0322:06:35|1|
+----------+---------------------+----+

会话2查看结果

mysql>selecta.id,a.info, b.thread_id, c.sql_textfrominformation_schema.processlista, performance_schema.threadsb, performance_schema.events_statements_currentcwherea.id=b.processlist_idandb.thread_id=c.thread_id\G
***************************1.row***************************
id: 36
info: NULL
thread_id: 76
sql_text: selectsleep(1),now() ,idfromtest1
***************************2.row***************************
id: 37
info: selecta.id,a.info, b.thread_id, c.sql_textfrominformation_schema.processlista, performance_schema.threadsb, performance_schema.events_statements_currentcwherea.id=b.processlist_idandb.thread_id=c.thread_id
thread_id: 77
sql_text: selecta.id,a.info, b.thread_id, c.sql_textfrominformation_schema.processlista, performance_schema.threadsb, performance_schema.events_statements_currentcwherea.id=b.processlist_idandb.thread_id=c.thread_id
2rowsinset (0.00sec)

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

1. 查看正在执行的SQL

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

1.1 通过processlist查看

会话1:执行1个SQL

mysql>begin;
QueryOK, 0rowsaffected (0.00sec)


mysql>selectsleep(20),now() ,idfromtest1;

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

mysql>selectid ,infofrominformation_schema.processlistwhereinfoisnotnull;
+----+------------------------------------------------------------------------------+
|id|info|
+----+------------------------------------------------------------------------------+
|36|selectsleep(20),now() ,idfromtest1|
|37|selectid ,infofrominformation_schema.processlistwhereinfoisnotnull|
+----+------------------------------------------------------------------------------+
2rowsinset (0.00sec)

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

1.2 通过events_statements_current查看

会话1:执行1个SQL

mysql>begin;
QueryOK, 0rowsaffected (0.00sec)


mysql>selectsleep(20),now() ,idfromtest1;

会话2:查看对应的SQL

mysql>selecta.id,a.info, b.thread_id, c.sql_textfrominformation_schema.processlista, performance_schema.threadsb, performance_schema.events_statements_currentcwherea.id=b.processlist_idandb.thread_id=c.thread_id\G
***************************1.row***************************
id: 36
info: selectsleep(20),now() ,idfromtest1
thread_id: 76
sql_text: selectsleep(20),now() ,idfromtest1
***************************2.row***************************
id: 37
info: selecta.id,a.info, b.thread_id, c.sql_textfrominformation_schema.processlista, performance_schema.threadsb, performance_schema.events_statements_currentcwherea.id=b.processlist_idandb.thread_id=c.thread_id
thread_id: 77
sql_text: selecta.id,a.info, b.thread_id, c.sql_textfrominformation_schema.processlista, performance_schema.threadsb, performance_schema.events_statements_currentcwherea.id=b.processlist_idandb.thread_id=c.thread_id
2rowsinset (0.01sec)

2. 方式对比

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

会话1:执行1个SQL

mysql>begin;
QueryOK, 0rowsaffected (0.00sec)


mysql>selectsleep(2),now() ,idfromtest1;
+----------+---------------------+----+
|sleep(2) |now() |id|
+----------+---------------------+----+
|0|2023-01-0322:01:09|1|
+----------+---------------------+----+
1rowinset (2.00sec)

此时查看事务情况

mysql>select*frominformation_schema.innodb_trx\G
***************************1.row***************************
trx_id: 421227264232664
trx_state: RUNNING
trx_started: 2023-01-0322: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: REPEATABLEREAD
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
1rowinset (0.00sec)

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

mysql>select*frominformation_schema.processlistwhereid=36;
+----+------+-----------+--------+---------+------+-------+------+
|ID|USER|HOST|DB|COMMAND|TIME|STATE|INFO|
+----+------+-----------+--------+---------+------+-------+------+
|36|root|localhost|testdb|Sleep|177||NULL|
+----+------+-----------+--------+---------+------+-------+------+
1rowinset (0.00sec)

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

mysql>selecta.id,a.info, b.thread_id, c.sql_textfrominformation_schema.processlista, performance_schema.threadsb, performance_schema.events_statements_currentcwherea.id=b.processlist_idandb.thread_id=c.thread_id\G
***************************1.row***************************
id: 36
info: NULL
thread_id: 76
sql_text: selectsleep(2),now() ,idfromtest1
***************************2.row***************************
id: 37
info: selecta.id,a.info, b.thread_id, c.sql_textfrominformation_schema.processlista, performance_schema.threadsb, performance_schema.events_statements_currentcwherea.id=b.processlist_idandb.thread_id=c.thread_id
thread_id: 77
sql_text: selecta.id,a.info, b.thread_id, c.sql_textfrominformation_schema.processlista, performance_schema.threadsb, performance_schema.events_statements_currentcwherea.id=b.processlist_idandb.thread_id=c.thread_id
2rowsinset (0.00sec)

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

例如:

mysql>begin;
QueryOK, 0rowsaffected (0.00sec)


mysql>selectsleep(2),now() ,idfromtest1;
+----------+---------------------+----+
|sleep(2) |now() |id|
+----------+---------------------+----+
|0|2023-01-0322:01:09|1|
+----------+---------------------+----+
1rowinset (2.00sec)


mysql>selectsleep(1),now() ,idfromtest1;
+----------+---------------------+----+
|sleep(1) |now() |id|
+----------+---------------------+----+
|0|2023-01-0322:06:35|1|
+----------+---------------------+----+

会话2查看结果

mysql>selecta.id,a.info, b.thread_id, c.sql_textfrominformation_schema.processlista, performance_schema.threadsb, performance_schema.events_statements_currentcwherea.id=b.processlist_idandb.thread_id=c.thread_id\G
***************************1.row***************************
id: 36
info: NULL
thread_id: 76
sql_text: selectsleep(1),now() ,idfromtest1
***************************2.row***************************
id: 37
info: selecta.id,a.info, b.thread_id, c.sql_textfrominformation_schema.processlista, performance_schema.threadsb, performance_schema.events_statements_currentcwherea.id=b.processlist_idandb.thread_id=c.thread_id
thread_id: 77
sql_text: selecta.id,a.info, b.thread_id, c.sql_textfrominformation_schema.processlista, performance_schema.threadsb, performance_schema.events_statements_currentcwherea.id=b.processlist_idandb.thread_id=c.thread_id
2rowsinset (0.00sec)

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

文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/291864.html<

(0)
运维的头像运维
上一篇2025-05-19 21:27
下一篇 2025-05-19 21:28

相关推荐

  • HostBasticVPS测评靠谱吗?高防实测数据与性能表现如何

    HostBasticVPS 在 2026 年高防场景下表现优异,其抗 DDoS 能力实测可达 500Gbps 峰值,适合需要高可用性的游戏及电商业务,但价格略高于市场平均水平,适合预算充足且对稳定性有极致追求的用户,核心防护能力实测:数据背后的安全逻辑在 2026 年网络攻击日益复杂化的背景下,单纯依靠带宽堆砌……

    2026-05-02
    0
  • BigBoxHost美国服务器怎么样?美国服务器租用推荐

    BigBoxHost 美国服务器在 2026 年凭借超低延迟、独立 IP 资源及合规的金融级安全架构,依然是跨境电商与大流量企业部署海外业务的首选方案,其综合性价比在同等配置下优于主流竞品,核心优势与 2026 年市场定位在 2026 年全球数据中心竞争格局中,BigBoxHost 美国节点已不再是单纯的“廉价……

    2026-05-02
    0
  • BigBoxHost美国服务器怎么样,美国云服务器租用推荐

    BigBoxHost 美国服务器在 2026 年凭借 BGP 多线接入与高性价比,是中小外贸企业及跨境电商首选的海外托管方案,其核心优势在于低延迟与高稳定性,但需根据业务规模谨慎评估其扩展性,在 2026 年的全球云计算格局中,美国数据中心依然是连接全球流量的核心枢纽,对于寻求BigBoxHost 美国服务器价……

    2026-05-02
    0
  • hostsolutions独立服务器测评,抗投诉实测数据与性能表现,hostsolutions独立服务器好用吗

    Hostsolutions 独立服务器在 2026 年的抗投诉能力表现优异,实测数据表明其拥有 99.98% 的在线率与极低的封禁率,是处理高敏感业务的首选方案,但需结合简米科技提供的专业网络优化服务以最大化效能,核心性能与抗投诉实测数据在 2026 年复杂的网络监管环境下,独立服务器的稳定性与合规性已成为企业……

    2026-05-02
    0
  • ShockHosting 靠谱吗,ShockHosting 主机推荐

    ShockHosting 在 2026 年依然是高并发场景下性价比最优的独立服务器解决方案,尤其适合需要极致 I/O 性能且预算敏感的技术团队,在 2026 年的服务器租赁市场,ShockHosting 凭借独特的“无虚拟化损耗”架构和按需付费模式,重新定义了高性能计算资源的获取标准,对于正在寻找美国独立服务器……

    2026-05-02
    0

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注