The information_schema in MySQL briefly introduces the role of each table in the metadata information base in Mysql. From this wiki, you can roughly understand the role of each table. Here we mainly introduce three tables related to Innodb transaction locks: INNODB_TRX table, INNODB_LOCKS table, INNODB_LOCK_WAITS table. By looking at the status of transaction locking and transaction lock waiting for these three tables, it is easier to monitor the current transaction and analyze possible lock problems, such as analyzing deadlocks.
The following first introduces the role of the next three tables and the meaning of each field.
1. INNODB_TRX table
The INNODB_TRX
table contains information about every transaction currently executing inside InnoDB
, including whether the transaction is waiting for a lock, when the transaction started, and the SQL statement the transaction is executing, if any.
The INNODB_TRX table mainly contains information about all transactions being executed in the InnoDB engine, including waiting for a lock and running transactions
Fields of the INNODB_TRX table
trx_id | Unique transaction ID within the InnoDB storage engine |
trx_state | The status of the current transaction: RUNNING , LOCK WAIT , ROLLING BACK or COMMITTING . |
trx_started | transaction start time |
trx_requested_lock_id | The ID of the lock that the transaction is waiting for (if the transaction status is not LOCK WAIT, this field is NULL). For detailed lock information, you can check the INNODB_LOCKS table. |
trx_wait_started | The time the transaction waited to start (this field is NULL if the transaction state is not LOCK WAIT) |
trx_weight | The weight of a transaction reflects the number of rows modified and locked by a transaction. When a deadlock rollback occurs, the rollback with the smallest value is preferred. |
trx_mysql_thread_id | Thread ID in Mysql, the result displayed by show processlist |
trx_query | The sql statement that the transaction runs |
trx_operation_state | The type of transaction when the operation is updating or deleting, starting index read, etc. |
trx_tables_in_use | The number of tables used by the query |
trx_tables_locked | Query the number of tables with row locks |
trx_lock_structs | The number of locks reserved by the transaction |
trx_lock_memory_bytes | The size of the space occupied by the lock in memory |
trx_rows_locked | The number of rows locked by the transaction (not an exact number) |
trx_rows_modified | The number of rows inserted or modified by the transaction |
trx_concurrency_tickets | A value indicating how much work the current transaction can do before being swapped out, as specified by the innodb_concurrency_tickets option. |
trx_isolation_level | isolation level |
trx_unique_checks | Unique key detection is enabled |
trx_foreign_key_checks | Whether foreign key detection is enabled |
trx_last_foreign_key_error | Detailed error message for last FK error, or NULL . |
trx_adaptive_hash_latched | Whether or not the adaptive hash index is locked by the current transaction. (Only a single transaction at a time can modify the adaptive hash index.) |
trx_adaptive_hash_timeout | Whether to relinquish the search latch immediately for the adaptive hash index, or reserve it across calls from MySQL. When there is no AHI contention, this value remains zero and statements reserve the latch until they finish. During times of contention, it counts down to zero, and statements release the latch immediately after each row lookup. |
2、INNODB_LOCKS表
The INNODB_LOCKS
table contains information about each lock that an InnoDB
transaction has requested but not yet acquired, and each lock that a transaction holds that is blocking another transaction.
INNODB_LOCKS表主要包含了InnoDB事务锁的具体情况,包括事务正在申请加的锁和事务加上的锁。
INNODB_LOCKS表的各个字段
lock_id | 锁ID |
lock_trx_id | 事务ID, 可以连INNODB_TRX表查事务详情 |
lock_mode | 锁的模式: S , X , IS , IX , S_GAP , X_GAP , IS_GAP , IX_GAP , or AUTO_INC |
lock_type | 锁的类型,行级锁 或者表级锁 |
lock_table | 加锁的表 |
lock_index | 如果是lock_type='RECORD' 行级锁 ,为锁住的索引,如果是表锁为null |
lock_space | 如果是lock_type='RECORD' 行级锁 ,为锁住对象的Tablespace ID,如果是表锁为null |
lock_page | 如果是lock_type='RECORD' 行级锁 ,为锁住页号,如果是表锁为null |
lock_rec | 如果是lock_type='RECORD' 行级锁 ,为锁住页号,如果是表锁为null |
lock_data | 事务锁住的主键值,若是表锁,则该值为null |
3、INNODB_LOCK_WAITS表
The INNODB_LOCK_WAITS
table contains one or more rows for each blocked InnoDB
transaction, indicating the lock it has requested and any locks that are blocking that request.
INNODB_LOCK_WAITS表包含了blocked的事务的锁等待的状态
requesting_trx_id | 申请锁资源的事务ID |
requesting_lock_id | 申请的锁的ID |
blocking_trx_id | 租塞的事务ID |
blocking_lock_id | 租塞的锁的ID |
insert into test(test_id) values(5);
select * from test where test_id =5 for update;