当前位置: 移动技术网 > IT编程>数据库>Mysql > MySQL 如何查询当前最新事务ID

MySQL 如何查询当前最新事务ID

2020年08月19日  | 移动技术网IT编程  | 我要评论
写在前面:在个别时候可能需要查看当前最新的事务 id,以便做一些业务逻辑上的判断(例如利用事务 id 变化以及前后时差,统计每次事务的响应时长等用途)。通常地,我们有两种方法可以查看当前的事务 id:

写在前面:在个别时候可能需要查看当前最新的事务 id,以便做一些业务逻辑上的判断(例如利用事务 id 变化以及前后时差,统计每次事务的响应时长等用途)。

通常地,我们有两种方法可以查看当前的事务 id:

1、执行 show engine innodb status,查看事务相关信息

=====================================
150303 17:16:11 innodb monitor output
=====================================
per second averages calculated from the last 15 seconds
...
------------
transactions
trx id counter 3359877657 -- 当前最大事务 id
purge done for trx's n:o < 3359877468 undo n:o < 0 state: running
history list length 324
list of transactions for each session:
---transaction 0, not started -- 该会话中执行 show engine innodb status,不会产生事务,所以事务 id 为 0
mysql thread id 4692367, os thread handle 0x51103940, query id 677284426 xx.173ops.com 10.x.x.x yejr init
show /*!50000 engine*/ innodb status
---transaction 3359877640, not started --非活跃事务,还未开始
mysql tables in use 1, locked 0
mysql thread id 4678384, os thread handle 0x41a57940, query id 677284427 xx.173ops.com 10.x.x.x yejr system lock
select polinfo0_.fid as fid39_0_, ...

---transaction 3359877652, not started
mysql thread id 4678383, os thread handle 0x50866940, query id 677284420 xx.173ops.com 10.x.x.x yejr cleaning up

---transaction 3359877635, active 1358 sec, thread declared inside innodb 5000 --活跃长事务,运行了 1358 秒还未结束,要引起注意,可能会导致大量锁等待发生
mysql tables in use 1, locked 1
1 lock struct(s), heap size 376, 0 row lock(s), undo log entries 1
mysql thread id 3120717, os thread handle 0x529b4940, query id 677284351 xx.173ops.com 10.x.x.x yejr query end
insert into t_live_room ...

2、查看 information_schema.innodb_trx、innodb_locks、innodb_lock_waits 三个表,通过这些信息能快速发现哪些事务在阻塞其他事务

先查询 innodb_trx 表,看看都有哪些事务

mysql> select * from information_schema.innodb_trx\g
*************************** 1. row ***************************
 trx_id: 17778 -- 当前事务 id
 trx_state: lock wait -- 处于锁等待状态,也就是等待其他会话释放锁资源
 trx_started: 2015-03-04 10:40:26
 trx_requested_lock_id: 17778:82:3:6 -- 欲请求的锁
 trx_wait_started: 2015-03-04 10:40:26
 trx_weight: 2 -- 大意是该锁影响了 2 行记录
 trx_mysql_thread_id: 657 -- processlist 中的线程 id
 trx_query: update trx_fee set fee=rand()*1000 where id= 4
 trx_operation_state: starting index read
 trx_tables_in_use: 1
 trx_tables_locked: 1
 trx_lock_structs: 2
 trx_lock_memory_bytes: 360
 trx_rows_locked: 1
 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: 10000
 trx_is_read_only: 0
 trx_autocommit_non_locking: 0
 *************************** 2. row ***************************
 trx_id: 17773
 trx_state: running
 trx_started: 2015-03-04 10:40:23
 trx_requested_lock_id: null
 trx_wait_started: null
 trx_weight: 10
 trx_mysql_thread_id: 656
 trx_query: null
 trx_operation_state: null
 trx_tables_in_use: 0
 trx_tables_locked: 0
 trx_lock_structs: 2
 trx_lock_memory_bytes: 360
 trx_rows_locked: 9
 trx_rows_modified: 8
 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: 10000
 trx_is_read_only: 0
 trx_autocommit_non_locking: 0

再看 innodb_locks 表,看看都有什么锁

mysql> select * from information_schema.innodb_locks\g
*************************** 1. row ***************************
lock_id: 17778:82:3:6 --当前锁 id
lock_trx_id: 17778 --该锁对应的事务 id
lock_mode: x -- 锁类型,排它锁 x
lock_type: record --锁范围,记录锁:record lock,其他锁范围:间隙锁:gap lock,或者 next-key lock(记录锁+间隙锁)
lock_table: `test`.`trx_fee`
lock_index: primary --加载在哪个索引上的锁
lock_space: 82
lock_page: 3
lock_rec: 6
lock_data: 4
*************************** 2. row ***************************
lock_id: 17773:82:3:6
lock_trx_id: 17773
lock_mode: x
lock_type: record
lock_table: `test`.`trx_fee`
lock_index: primary
lock_space: 82
lock_page: 3
lock_rec: 6
lock_data: 4

最后看 innodb_lock_waits 表,看看当前都有哪些锁等待

mysql> select * from information_schema.innodb_lock_waits\g
*************************** 1. row ***************************
requesting_trx_id: 17778 --请求锁的事务 id(等待方)
requested_lock_id: 17778:82:3:6 -- 请求锁 id
blocking_trx_id: 17773 -- 阻塞该锁的事务 id(当前持有方,待释放)
blocking_lock_id: 17773:82:3:6 -- 持有的锁 id

关于 information_schema 中和 innodb 有关的表用途描述,可以查看手册:21.29 information_schema tables for innodb

3、利用 percona 分支的特性,查看当前最新事务 id,该特性从 5.6.11-60.3 版本开始引入,执行下面的 2 个命令即可查看

mysqladmin ext | grep innodb_max_trx_id
或者
mysql> show global status like 'innodb_max_trx_id';

最后,交代下问题的来源其实是这样的,有位朋友和我讨论问题,说在 java 连接池中,发现 2 个事务的事务 id 是一样的,测试的 sql 代码:

begin;update trx set un=rand() where id=round(rand()*10)+1;select * from information_schema.innodb_trx; commit;select sleep(0.01);begin;update trx set un=rand() where id=round(rand()*10)+1;select * from information_schema.innodb_trx;commit;

这串代码不能折行,中间的 sleep 停留 不能太大,也就是模拟足够快的情况下,检查 2 次事务的 id 是否有变化。可以发现,时间足够短的话,2 次查询到的事务 id 是一样的,并没有发生变化。大家也可以在自己的环境下试试。

以上就是mysql 如何查询当前最新事务id的详细内容,更多关于mysql查询事务id的资料请关注移动技术网其它相关文章!

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

  • Ubuntu上Vim安装NERDTree插件的详细操作步骤

    Ubuntu上Vim安装NERDTree插件的详细操作步骤

    nerdtree是vim的文件系统浏览器,使用此插件,用户可以直观地浏览复杂的目录层次结构,快速打开文件以进行读取或编辑,以及执行基本的文件系统操作。nerdt... [阅读全文]
  • MySQL 4种常用的主从复制架构

    MySQL 4种常用的主从复制架构

    一主多从复制架构在主库读取请求压力非常大的场景下,可以通过配置一主多从复制架构实现读写分离,把大量的对实时性要求不是特别高的读请求通过负载均衡分部到多个从库上(... [阅读全文]
  • 浅析MySQL 备份与恢复

    1、简介数据无价,mysql作为一个数据库系统,其备份自然也是非常重要且有必要去做。备份的理由千千万,预防故障,安全需求,回滚,审计,删了又改的需求等等,备份的... [阅读全文]
  • 保障MySQL数据安全的一些建议

    数据是企业核心资产,数据对企业而言是最重要的工作之一。稍有不慎,极有可能发生数据无意泄露,甚至被黑客恶意窃取的风险。每年业界都会传出几起大事件,某知名或不知名的... [阅读全文]
  • MySQL如何快速修改表的表结构

    快速修改mysql某张表的表结构--摘录自《mysql管理之道》alter table 表名 modify 列名 数据类型; 这个命令可以修改表结构此外,也可以... [阅读全文]
  • MySQL 行锁和表锁的含义及区别详解

    一、前言对于行锁和表锁的含义区别,在面试中应该是高频出现的,我们应该对mysql中的锁有一个系统的认识,更详细的需要自行查阅资料,本篇为概括性的总结回答。mys... [阅读全文]
  • MySQL 如何查询当前最新事务ID

    写在前面:在个别时候可能需要查看当前最新的事务 id,以便做一些业务逻辑上的判断(例如利用事务 id 变化以及前后时差,统计每次事务的响应时长等用途)。通常地,... [阅读全文]
  • 如何优雅、安全的关闭MySQL进程

    前言本文分析了 mysqld 进程关闭的过程,以及如何安全、缓和地关闭 mysql 实例,对这个过程不甚清楚的同学可以参考下。关闭过程1、发起 shutdown... [阅读全文]
  • 详解MySQL8.0&#8203; 字典表增强

    详解MySQL8.0&#8203; 字典表增强

    mysql中数据字典是数据库重要的组成部分之一,information_schema首次引入于mysql 5.0,作为一种从正在运行的mysql服务器检索元数据... [阅读全文]
  • 简述MySQL InnoDB存储引擎

    前言:存储引擎是数据库的核心,对于 mysql 来说,存储引擎是以插件的形式运行的。虽然 mysql 支持种类繁多的存储引擎,但最常用的当属 innodb 了,... [阅读全文]
验证码:
移动技术网