您的位置:首页 > 数据库 > MySQL

mysql 锁超时定位与分析

2016-07-04 15:39 405 查看

mysql 锁超时调查方法

此SQL可以显示锁等待的详细信息,包括阻塞SQL,被阻塞SQL及阻塞时间等。

select r.trx_id as waiting_trx_id, r.trx_mysql_thread_id as waiting_thread, TIMESTAMPDIFF(SECOND, r.trx_wait_started, CURRENT_TIMESTAMP) as wait_time, r.trx_query as waiting_query, l.lock_table as waiting_table_lock, b.trx_id as blocking_trx_id, b.trx_mysql_thread_id
as blocking_thread, SUBSTRING(p.host, 1, INSTR(p.host, ‘:’) -1) as blocking_host, SUBSTRING(p.host, INSTR(p.host, ‘:’)+1) as blocking_port, IF(p.command =”Sleep”, p.time,0) as idle_in_trx, b.trx_query as blocking_query from information_schema.INNODB_LOCK_WAITS
w inner join information_schema.INNODB_TRX b on b.trx_id=w.blocking_trx_id inner join information_schema.INNODB_TRX r on r.trx_id=w.requesting_trx_id inner join information_schema.INNODB_LOCKS as l on w.requested_lock_id=l.lock_id left join information_schema.PROCESSLIST
p on p.id=b.trx_mysql_thread_id order by wait_time desc limit 1\G

*************************** 1. row ***************************

waiting_trx_id: 2AA9FED000

waiting_thread: 187

wait_time: 50

waiting_query: INSERT IGNORE INTO workingSeedTrackTask ( task_priority, task_status, retry_count, created_at , source_count , trackingProtocol_id, seed_id ) ( SELECT ‘low’, ‘created’, 0, NOW(), source_count, trackingProtocol_id, id FROM seed WHERE trackingProtocol_id
= 1 AND id = 5249101 )

waiting_table_lock: `hubble`.`workingSeedTrackTask`

blocking_trx_id: 2AA9FE7A59

blocking_thread: 179087834

blocking_host: 206.99.94.72

blocking_port: 37525

idle_in_trx: 0

blocking_query: insert into workingSeedTrackTask(seed_id, source_count, trackingProtocol_id, task_priority, task_status, created_at) select d.id, d.source_count, d.trackingProtocol_id, ‘urgent’, ‘created’, now() from verifySeed a, seed_trackingMetaFinished
b, mddb.meta c, seed d where a.seed_id = b.seed_id and a.seed_id > 7183277 and b.seed_id > 7183277 and b.verify_status = ‘match’ and c.company_id in(34,1369) and a.is_tracking = ‘true’ and b.trackingMeta_id = c.id and a.seed_id = d.id ON DUPLICATE KEY UPDATE
task_priority = VALUES(task_priority)

此SQL可以显示有多少查询被哪些线程阻塞,被阻塞最长时间及被阻塞线程数。

select concat(‘thread ‘, b.trx_mysql_thread_id, ‘ from ‘, p.host) as who_blocks, if(p.command=”Sleep”, p.time,0) as idle_in_trx, max(TIMESTAMPDIFF(SECOND, r.trx_wait_started, now())) as max_wait_time, count(*) as num_waiters from information_schema.INNODB_LOCK_WAITS
w inner join information_schema.INNODB_TRX b on b.trx_id=w.blocking_trx_id inner join information_schema.INNODB_TRX r on r.trx_id=w.requesting_trx_id left join information_schema.PROCESSLIST p on p.id=b.trx_mysql_thread_id group by who_blocks order by num_waiters
desc;

+——————————————————————+————-+—————+————-+

| who_blocks | idle_in_trx | max_wait_time | num_waiters |

+——————————————————————+————-+—————+————-+

| thread 179084010 from 206.99.94.72:37276 | 0 | 39 | 13 |

| thread 172 from ip-10-171-54-59.us-west-1.compute.internal:58220 | 0 | 35 | 2 |

| thread 136 from ip-10-171-54-59.us-west-1.compute.internal:58135 | 0 | 5 | 1 |

| thread 93 from ip-10-171-54-59.us-west-1.compute.internal:58083 | 0 | 30 | 1 |

| thread 179084008 from 206.99.94.72:37274 | 0 | 39 | 1 |

+——————————————————————+————-+—————+————-+
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: