iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >mysql InnoDB锁等待的查看以及分析
  • 877
分享到

mysql InnoDB锁等待的查看以及分析

2024-04-02 19:04:59 877人浏览 独家记忆
摘要

本篇内容主要讲解“Mysql InnoDB锁等待的查看以及分析”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“mysql InnoDB锁等待的查看以及分析”吧!在

本篇内容主要讲解“Mysql InnoDB等待的查看以及分析”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习mysql InnoDB锁等待的查看以及分析”吧!

在InnoDB Plugin之前,一般通过show full processlist和show engine innodb status命令查看当前的数据库请求,然后再判断当前事务中锁的情况。随着mysql的发展,已经提供更加便捷的方法来监控数据库中的锁等待现象了。

在infORMation_schema下面有三张表:INNODB_TRX、INNODB_LOCKS、INNODB_LOCK_WaiTS,通过这三张表,可以更简单地监控当前的事务并分析可能存在的问题。

  • INNODB_TRX表及结构

Column nameDescription
TRX_IDUnique transaction ID number, internal to InnoDB. (Starting in MySQL 5.6, these IDs are not created for transactions that are read-only and non-locking. See Optimizing InnoDB Read-Only Transactions for details.)
TRX_WEIGHTThe weight of a transaction, reflecting (but not necessarily the exact count of) the number of rows altered and the number of rows locked by the transaction. To resolve a deadlock, InnoDB selects the transaction with the smallest weight as the “victim” to rollback. Transactions that have changed non-transactional tables are considered heavier than others, regardless of the number of altered and locked rows.
TRX_STATETransaction execution state. One of RUNNING, LOCK WAIT, ROLLING BACK or COMMITTING.
TRX_STARTEDTransaction start time.
TRX_REQUESTED_LOCK_IDID of the lock the transaction is currently waiting for (if TRX_STATE is LOCK WAIT, otherwise NULL). Details about the lock can be found by joining with INNODB_LOCKS on LOCK_ID.
TRX_WAIT_STARTEDTime when the transaction started waiting on the lock (if TRX_STATE is LOCK WAIT, otherwise NULL).
TRX_MYSQL_THREAD_IDMySQL thread ID. Can be used for joining with PROCESSLIST on ID. See Section 14.17.2.3.1, “Potential Inconsistency with PROCESSLIST Data”.
TRX_QUERYThe SQL query that is being executed by the transaction.
TRX_OPERATION_STATEThe transaction's current operation, or NULL.
TRX_TABLES_IN_USEThe number of InnoDB tables used while processing the current SQL statement of this transaction.
TRX_TABLES_LOCKEDNumber of InnoDB tables that the current SQL statement has row locks on. (Because these are row locks, not table locks, the tables can usually still be read from and written to by multiple transactions, despite some rows being locked.)
TRX_LOCK_STRUCTSThe number of locks reserved by the transaction.
TRX_LOCK_MEMORY_BYTESTotal size taken up by the lock structures of this transaction in memory.
TRX_ROWS_LOCKEDApproximate number or rows locked by this transaction. The value might include delete-marked rows that are physically present but not visible to the transaction.
TRX_ROWS_MODIFIEDThe number of modified and inserted rows in this transaction.
TRX_CONCURRENCY_TICKETSA value indicating how much work the current transaction can do before being swapped out, as specified by the innodb_concurrency_tickets option.
TRX_ISOLATION_LEVELThe isolation level of the current transaction.
TRX_UNIQUE_CHECKSWhether unique checks are turned on or off for the current transaction. (They might be turned off during a bulk data load, for example.)
TRX_FOREIGN_KEY_CHECKSWhether foreign key checks are turned on or off for the current transaction. (They might be turned off during a bulk data load, for example.)
TRX_LAST_FOREIGN_KEY_ERRORDetailed error message for last FK error, or NULL.
TRX_ADAPTIVE_HASH_LATCHEDWhether 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_TIMEOUTWhether 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.
TRX_IS_READ_ONLYA value of 1 indicates the transaction is read-only. (5.6.4 and up.)
TRX_AUTOCOMMIT_NON_LOCKINGA value of 1 indicates the transaction is a SELECT statement that does not use the FOR UPDATE or LOCK IN SHARED MODE clauses, and is executing with the autocommit setting turned on so that the transaction will only contain this one statement. (5.6.4 and up.) When this column and TRX_IS_READ_ONLY are both 1, InnoDB optimizes the transaction to reduce the overhead associated with transactions that change table data.

比较常用的列:

trx_id:InnoDB存储引擎内部唯一的事物ID
trx_status:当前事务的状态
trx_status:事务的开始时间
trx_requested_lock_id:等待事务的锁ID
trx_wait_started:事务等待的开始时间
trx_weight:事务的权重,反应一个事务修改和锁定的行数,当发现死锁需要回滚时,权重越小的值被回滚
trx_mysql_thread_id:MySQL中的进程ID,与show processlist中的ID值相对应
trx_query:事务运行的SQL语句

  • INNODB_LOCKS

Column nameDescription
LOCK_IDUnique lock ID number, internal to InnoDB. Treat it as an opaque string. Although LOCK_ID currently contains TRX_ID, the format of the data in LOCK_ID is not guaranteed to remain the same in future releases. Do not write programs that parse the LOCK_ID value.
LOCK_TRX_IDID of the transaction holding this lock. Details about the transaction can be found by joining with INNODB_TRX on TRX_ID.
LOCK_MODEMode of the lock. One of S, X, IS, IX, S_GAP, X_GAP, IS_GAP, IX_GAP, or AUTO_INC for shared, exclusive, intention shared, intention exclusive row locks, shared and exclusive gap locks, intention shared and intention exclusive gap locks, and auto-increment table level lock, respectively. Refer to the sections Section 14.5.3, “InnoDB Lock Modes” and Section 14.5.2, “The InnoDB Transaction Model and Locking” for information on InnoDB locking.
LOCK_TYPEType of the lock. One of RECORD or TABLE for record (row) level or table level locks, respectively.
LOCK_TABLEName of the table that has been locked or contains locked records.
LOCK_INDEXName of the index if LOCK_TYPE='RECORD', otherwise NULL.
LOCK_SPACETablespace ID of the locked record if LOCK_TYPE='RECORD', otherwise NULL.
LOCK_PAGEPage number of the locked record if LOCK_TYPE='RECORD', otherwise NULL.
LOCK_RECHeap number of the locked record within the page if LOCK_TYPE='RECORD', otherwise NULL.
LOCK_DATAPrimary key value(s) of the locked record if LOCK_TYPE='RECORD', otherwise NULL. This column contains the value(s) of the primary key column(s) in the locked row, formatted as a valid SQL string (ready to be copied to SQL commands). If there is no primary key then the InnoDB internal unique row ID number is used. If a gap lock is taken for key values or ranges above the largest value in the index, LOCK_DATA reports “supremum pseudo-record”. When the page containing the locked record is not in the buffer pool (in the case that it was paged out to disk while the lock was held), InnoDB does not fetch the page from disk, to avoid unnecessary disk operations. Instead, LOCK_DATA is set to NULL.
  • INNODB_LOCK_WAITS

Column nameDescription
REQUESTING_TRX_IDID of the requesting transaction.
REQUESTED_LOCK_IDID of the lock for which a transaction is waiting. Details about the lock can be found by joining with INNODB_LOCKS on LOCK_ID.
BLOCKING_TRX_IDID of the blocking transaction.
BLOCKING_LOCK_IDID of a lock held by a transaction blocking another transaction from proceeding. Details about the lock can be found by joining with INNODB_LOCKS on LOCK_ID.

以上这些表,其实只要知道其中比较常用的字段,就差不多能够满足日常的工作需求了,下面通过测试进行演示;

一、准备工作

1、在test下面随便创建一张表john,并取消自动commit操作,脚本如下:

mysql> use information_schema
Database changed

mysql> select count(*) from tables;
+----------+
| count(*) |
+----------+
|       81 |
+----------+
1 row in set (0.06 sec)

mysql> create table test.john as select * from tables;
Query OK, 82 rows affected (0.29 sec)
Records: 82  Duplicates: 0  Warnings: 0

 

mysql> insert into john select * from john;
Query OK, 671744 rows affected (2 min 19.03 sec)
Records: 671744  Duplicates: 0  Warnings: 0

(经过几次插入后john表的数据671744行)

 

mysql> set @@autocommit=0;
Query OK, 0 rows affected (0.00 sec)

(取消数据库的自动commit)

二、进行表john加锁操作,脚本如下:

mysql> select count(*) from john for update;
+----------+
| count(*) |
+----------+
|  2686976 |
+----------+
1 row in set (8.19 sec)

在另外一个窗口中监控innodb锁的状态;

mysql> SELECT  * FROM INNODB_TRX\G;
*************************** 1. row ***************************
                    trx_id: B14                                                /请记住该trx_id/
                 trx_state: RUNNING                                       /当前状态/
               trx_started: 2014-11-29 14:07:51
     trx_requested_lock_id: NULL
          trx_wait_started: NULL
                trx_weight: 15905
       trx_mysql_thread_id: 10                                          /在process 里面的id值/
                 trx_query: select count(*) from john for update;    /当前执行的语句/
       trx_operation_state: fetching rows
         trx_tables_in_use: 1
         trx_tables_locked: 1
          trx_lock_structs: 15905
     trx_lock_memory_bytes: 1554872
           trx_rows_locked: 1360743
         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
1 row in set (0.02 sec)

trx_id: B14 只是持有锁,但并没有产生锁等待;

三、模拟锁等待

3.1 在另外一个窗口中,执行语句:

mysql> select count(*) from john where table_name='CHARACTER_SETS' for update;

3.2 查看当前锁等待的情况

INNODB_TRX的锁情况:

mysql> SELECT  * FROM INNODB_TRX\G;
*************************** 1. row ***************************
                    trx_id: B15                      
                 trx_state: LOCK WAIT                       //状态为锁等待//
               trx_started: 2014-11-29 14:12:28
     trx_requested_lock_id: B15:0:32777:2
          trx_wait_started: 2014-11-29 14:12:28
                trx_weight: 2
       trx_mysql_thread_id: 10                           //在process里面可以看到相应的状态// 
                 trx_query: select count(*) from john where table_name='CHARACTER_SETS' for update                //锁等待的语句//
       trx_operation_state: starting index read
         trx_tables_in_use: 1
         trx_tables_locked: 1
          trx_lock_structs: 2
     trx_lock_memory_bytes: 376
           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
*************************** 2. row ***************************
                    trx_id: B14                                   
                 trx_state: RUNNING
               trx_started: 2014-11-29 14:07:51
     trx_requested_lock_id: NULL
          trx_wait_started: NULL
                trx_weight: 31777
       trx_mysql_thread_id: 8
                 trx_query: NULL
       trx_operation_state: NULL
         trx_tables_in_use: 0
         trx_tables_locked: 0
          trx_lock_structs: 31777
     trx_lock_memory_bytes: 3094968
           trx_rows_locked: 2718752
         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
2 rows in set (0.02 sec)

请注意:因为我们只有模拟两个session,所以这边只有两个会话。(因此一个处于锁等待,另外一个必然就是持有锁的对象。实际的生产环境中可能这边会出现很多列,所以需要用下面的语句才能判断:锁等待和持有锁对象的匹配关系)

3.3 锁等待和持有锁的相互关系

mysql> SELECT * FROM INNODB_LOCK_WAITS\G;
*************************** 1. row ***************************
requesting_trx_id: B15
requested_lock_id: B15:0:32777:2
  blocking_trx_id: B14
 blocking_lock_id: B14:0:32777:2
1 row in set (0.03 sec)

ERROR: 
No query specified

通过视图INNODB_LOCK_WAITS可以清晰的看到B14持有锁,而B15处于锁等待;

3.4 锁等待的原因

mysql> SELECT * FROM INNODB_LOCKS\G;
*************************** 1. row ***************************
    lock_id: B15:0:32777:2
lock_trx_id: B15
  lock_mode: X
  lock_type: RECORD
 lock_table: `test`.`john`
 lock_index: `GEN_CLUST_INDEX`
 lock_space: 0
  lock_page: 32777
   lock_rec: 2
  lock_data: 0x000000640000
*************************** 2. row ***************************
    lock_id: B14:0:32777:2
lock_trx_id: B14
  lock_mode: X
  lock_type: RECORD
 lock_table: `test`.`john`
 lock_index: `GEN_CLUST_INDEX`
 lock_space: 0
  lock_page: 32777
   lock_rec: 2
  lock_data: 0x000000640000
2 rows in set (0.01 sec)

可以看到持有锁的模式、对象

3.5 在进程里面查看状态

mysql InnoDB锁等待的查看以及分析

Id值为8的进程,Info显示为NULL值,可以推断当前的session由于未进行commit导致锁未释放的;

总结:通过以上几个视图,就可以很快速的判断出锁等待的对象及原因了,从这上面也可以看出mysql管理更加便捷和容易了;

到此,相信大家对“mysql InnoDB锁等待的查看以及分析”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

您可能感兴趣的文档:

--结束END--

本文标题: mysql InnoDB锁等待的查看以及分析

本文链接: https://www.lsjlt.com/news/59985.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • mysql InnoDB锁等待的查看以及分析
    本篇内容主要讲解“mysql InnoDB锁等待的查看以及分析”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“mysql InnoDB锁等待的查看以及分析”吧!在...
    99+
    2024-04-02
  • mysql锁等待查询分析
    mysql锁等待分析 1、简单说明 使用innodb存储引擎后,mysql有三张表来分析锁及阻塞的问题,在information_schema下面有三张表:INNODB_TRX、INNODB_LO...
    99+
    2024-04-02
  • 如何查看MySQL锁等待的原因
    这篇文章给大家分享的是有关如何查看MySQL锁等待的原因的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。--sys库的介绍    mysql...
    99+
    2024-04-02
  • mysql锁以及锁信息查看
    mysql中有以下几类锁: 共享锁/排他锁 意向锁,该锁是表级别的 记录锁 Gap 锁 Next-Key锁 自增锁 地理索引的Predicate锁。 mysql 中有几张系统表是关于锁信息的: INFO...
    99+
    2024-04-02
  • Mysql查询正在执行的事务以及等待锁的操作方式
    使用navicat测试学习: 首先使用set autocommit = 0;(取消自动提交,则当执行语句commit或者rollback执行提交事务或者回滚) 在打开一个执行update 查...
    99+
    2024-04-02
  • 如何分辨MySQL中的死锁和锁等待
    这篇文章给大家介绍如何分辨MySQL中的死锁和锁等待,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。【数据库版本】MySQL5.7程序报错1205 Lock wat timeout ex...
    99+
    2024-04-02
  • Mysql-InnoDB锁的示例分析
    小编给大家分享一下Mysql-InnoDB锁的示例分析,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!锁类型行级锁锁模式只有LOCK_S 和LOCK_X,其他的 FLAG 用于锁的描述,如前述 ...
    99+
    2024-04-02
  • innodb查询锁的示例分析
    这篇文章主要为大家展示了“innodb查询锁的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“innodb查询锁的示例分析”这篇文章吧。 ...
    99+
    2024-04-02
  • mysql innodb的行锁举例分析
    这篇文章主要讲解了“mysql innodb的行锁举例分析”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“mysql innodb的行锁举例分析”吧! ...
    99+
    2024-04-02
  • MySQL之InnoDB中锁的情况分析
    这篇文章主要讲解了“MySQL之InnoDB中锁的情况分析”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“MySQL之InnoDB中锁的情况分析”吧!mysq...
    99+
    2024-04-02
  • MySQL的innoDB锁机制以及死锁的处理方法
    本篇内容主要讲解“MySQL的innoDB锁机制以及死锁的处理方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“MySQL的innoDB锁机制以及死锁的处理方法...
    99+
    2024-04-02
  • jQuery中EasyUI页面加载等待及页面等待层的示例分析
    这篇文章主要为大家展示了“jQuery中EasyUI页面加载等待及页面等待层的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“jQuery中EasyUI页...
    99+
    2024-04-02
  • MySQL InnoDB锁介绍及不同SQL语句分别加什么样的锁的示例分析
    今天就跟大家聊聊有关MySQL InnoDB锁介绍及不同SQL语句分别加什么样的锁的示例分析,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。文章正文开...
    99+
    2024-04-02
  • oracle如何查看被锁的表以及解锁方法
    这篇文章给大家分享的是有关oracle如何查看被锁的表以及解锁方法的内容。小编觉得挺实用的,因此分享给大家做个参考。一起跟随小编过来看看吧。--以下几个为相关表SELECT * FROM v$loc...
    99+
    2024-04-02
  • 分析Oracle关闭以及job查看与停止
    这篇文章主要讲解了“分析Oracle关闭以及job查看与停止”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“分析Oracle关闭以及job查看与停止”吧!Or...
    99+
    2024-04-02
  • MySQL中InnoDB事务锁阅读锁信息状态的示例分析
    这篇文章主要介绍了MySQL中InnoDB事务锁阅读锁信息状态的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。 ...
    99+
    2024-04-02
  • Linux服务器高I/O等待延迟问题查找的示例分析
    这篇文章主要介绍Linux服务器高I/O等待延迟问题查找的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!0. 首先是top查看一下系统状况 发现两个参数异常,一是平均负载高,一是cpu %wa一直在50%以上...
    99+
    2023-06-16
  • MYSQL innodb中的只读事物以及事物id的分配方式
    原创水平有限,如果有误请指出 一、只读事物 也许有人要问一个select算不算一个事物。其实在innodb中一个innodb的select是一个事物,他有trx_t结构体,并且放到了mysql_trx_...
    99+
    2024-04-02
  • 分析MySQL数据库Innodb中的事务隔离级别和锁的关系
    本篇内容主要讲解“分析MySQL数据库Innodb中的事务隔离级别和锁的关系”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“分析MySQL数据库Innodb中的事...
    99+
    2024-04-02
  • MySQL死锁问题的分析及解决方法
    这篇文章主要讲解了“MySQL死锁问题的分析及解决方法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“MySQL死锁问题的分析及解决方法”吧!MySQL死锁问...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作