個別時候可能需要查看當前最新的事務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
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是一樣的,並沒有發生變化。大家也可以在自己的環境下試試。