本文转自:
观察innodb的锁时间,需要关注:
mysqladmin extended-status -r -i 1 -uroot | grep "Innodb_row_lock_time"
Innodb_row_lock_current_waits:当前正在等待锁定的数量;
Innodb_row_lock_time :从系统启动到现在锁定的总时间长度,单位ms;
Innodb_row_lock_time_avg :每次等待所花平均时间;
Innodb_row_lock_time_max:从系统启动到现在等待最长的一次所花的时间;
Innodb_row_lock_waits :从系统启动到现在总共等待的次数。
如我的压测语句:
insert into i0( id, type, num, iid, uid, iid, sid,bid, cid, q, gmt_create, gmt_modified, status, version, time_out, time_number, o_gmt_create,aid, ff, flag, code, cache, sq, rq) values( :v_id, '1',:v_opt_num, '1352', 1640350003, 16441266176, 0, '1370761289618','19101339145067259', '2', now(), now(), '1', '0', null, '0', now(), '0', null, '2', 'C', 1, null, null); update q0 set q=q-0, v=v+1, gmt_modified=now(), rq = CASE WHEN ((rq + 2 ) >= 0 ) then rq + 2 ELSE 0 END where id =6060 and iid = 16441266176 and sid = 0 and (q - rq - 2) >= 0 and q-0>=0;
压测结果:
Summary: SQL01 exec=11756, rows=11756=100/e, avg=518 usSummary: SQL02 exec=12800, rows=12800=100/e, avg=14483 usSummary: exec=8488/s, qtps=16343/s
观察到的锁情况:
| Innodb_row_lock_time | 868 || Innodb_row_lock_time_avg | 0 || Innodb_row_lock_time_max | 0 || Innodb_row_lock_time | 114932 || Innodb_row_lock_time_avg | 0 || Innodb_row_lock_time_max | 0 || Innodb_row_lock_time | 61775 || Innodb_row_lock_time_avg | 0 || Innodb_row_lock_time_max | 0 || Innodb_row_lock_time | 0 |
从占有锁到释放锁一共消耗:114932ms,不可能是一条记录的时间,再根据实时状态:
-------- -----load-avg---- ---cpu-usage--- ---swap--- -QPS- -TPS- -Hit%- ---innodb rows status--- time | 1m 5m 15m |usr sys idl iow| si so| ins upd del sel iud| lor hit| ins upd del read|19:47:20| 1.26 1.21 1.20| 5 1 93 0| 0 0| 0 0 0 0 0| 0 100.00| 0 0 0 0|19:47:21| 1.26 1.21 1.20| 1 1 98 0| 0 0| 0 1 0 5 1| 9 100.00| 0 1 0 1|19:47:22| 1.24 1.21 1.20| 0 0 100 0| 0 0| 0 0 0 6 0| 1 100.00| 0 0 0 0|19:47:23| 1.24 1.21 1.20| 7 3 90 0| 0 0| 2932 2930 0 6 5862| 100502 99.91| 2712 2809 0 2809|19:47:24| 1.24 1.21 1.20| 16 4 79 0| 0 0| 7939 7940 0 6 15879| 293693 99.91| 7257 7954 0 7954|19:47:25| 1.24 1.21 1.20| 5 2 93 0| 0 0| 1929 1932 0 6 3861| 101527 99.94| 1762 2039 0 2039|
实际每秒update 7940条,平均每条消耗:
root@(none) 07:47:33>select 114932/7940ms;+-------------+| 114932/7940 |+-------------+| 14.4751 |+-------------+
平均每条消耗14.4751ms和压测结果avg=14483 us吻合,对于单条语句的更新来说这个锁占用的时间还是挺长的。
innodb 获取行锁争用情况
本文转自:
可以通过检查InnoDB_row_lock状态变量来分析系统上的行锁的争夺情况:
mysql> show status like 'innodb_row_lock%';
+-------------------------------+-------+
| Variable_name | Value |
+-------------------------------+-------+
| InnoDB_row_lock_current_waits | 0 |
| InnoDB_row_lock_time | 0 |
| InnoDB_row_lock_time_avg | 0 |
| InnoDB_row_lock_time_max | 0 |
| InnoDB_row_lock_waits | 0 |
+-------------------------------+-------+
5 rows in set (0.01 sec)
如果发现锁争用比较严重,如InnoDB_row_lock_waits和InnoDB_row_lock_time_avg的值比较高,还可以通过设置InnoDB Monitors来进一步观察发生锁冲突的表、数据行等,并分析锁争用的原因。
具体方法如下:
mysql> CREATE TABLE innodb_monitor(a INT) ENGINE=INNODB;
Query OK, 0 rows affected (0.14 sec)
然后就可以用下面的语句来进行查看:
mysql> Show innodb status\G;
*************************** 1. row ***************************
Type: InnoDB
Name:
Status:
…
…
------------
TRANSACTIONS
------------
Trx id counter 0 117472192
Purge done for trx's n:o < 0 117472190 undo n:o < 0 0
History list length 17
Total number of lock structs in row lock hash table 0
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 0 117472185, not started, process no 11052, OS thread id 1158191456
MySQL thread id 200610, query id 291197 localhost root
---TRANSACTION 0 117472183, not started, process no 11052, OS thread id 1158723936
MySQL thread id 199285, query id 291199 localhost root
Show innodb status
…
监视器可以通过发出下列语句来停止查看:
mysql> DROP TABLE innodb_monitor;
Query OK, 0 rows affected (0.05 sec)
设置监视器后,在SHOW INNODB STATUS的显示内容中,会有详细的当前锁等待的信息,包括表名、锁类型、锁定记录的情况等,便于进行进一步的分析和问题的确定。打开监视器以后,默认情况下每15秒会向日志中记录监控的内容,如果长时间打开会导致.err文件变得非常的巨大,所以用户在确认问题原因之后,要记得删除监控表以关闭监视器,或者通过使用“--console”选项来启动服务器以关闭写日志文件。
--------------------- 作者:cocoyan123 来源:CSDN 原文:https://blog.csdn.net/xuanxuan_good/article/details/53730842 版权声明:本文为博主原创文章,转载请附上博文链接!